Forums Register Login

Using the new operator vs Inheritance

+Pie Number of slices to send: Send
Which is better?

SomeOtherClass soc = new SomeOtherClass();
soc.method();

OR

MyClass extends SomeOtherClass {
MyClass.method();
}
+Pie Number of slices to send: Send
Why would you want to extend a class just to invoke a method? Inheritance is for a different purpose and cannot be compared with creating new instance.

And the way you are invoking in method in MyClass is wrong? Did you try to compile a similar code?
+Pie Number of slices to send: Send
Also not a topic about threads ... moving to the Java in General forum...
+Pie Number of slices to send: Send
Not sure that we see enough information to know if the code works or not (I will clarify, the example of MyClass as literally spelled out won't work, I am referring to use of MyClass.method() itself). The method invocation from a class such as MyClass is only going to work on static methods. If it is an instance method, then you are going to need to access it from an instantiated object such as soc.method().
+Pie Number of slices to send: Send
without a doubt, if all you are doing is using an existing method, do it the first way. extending classes is for if you want to override a method(change it's behavior). although i must admit i still usually extend JFrame when i could have one as a member variable instead. keep in mind that you can only extend one class, and some classes don't need to extend anything.
1
+Pie Number of slices to send: Send
 

Faisal syed wrote:Which is better?
SomeOtherClass soc = new SomeOtherClass();
soc.method();
OR
MyClass extends SomeOtherClass {
MyClass.method();
}


I don't understand the question, but I will say this in the words of the Lord Joshua Bloch: favour composition over inheritance.

Winston
+Pie Number of slices to send: Send
 

"It's better to light a candle than curse the darkness."

 
+Pie Number of slices to send: Send
 

H Paul wrote:

"It's better to light a candle than curse the darkness."


Not quite sure what you meant there either (it's not been a good week), but just in case my answer was too cryptic:

@Faisal: Google the words in my post in italics.

Winston
+Pie Number of slices to send: Send

class should be extended for their structural behavior. i.e. if two classes have common common properties then it would be suitable to use extends.And should not be done only for code reuse.

e.g
Employee
Project Manager IS-a-Employee.
Developer IS-a-Employee.

Client-IS-Employee. //--------------not appropriate



+Pie Number of slices to send: Send
Just in case while considering Inheritance one has to keep in mind the Liskov Substitution Principle.
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1090 times.
Similar Threads
Synchronized Blocks
useBean doubt
Anonymous Inner Classes
Trying to cast sibling types and interfaces
Synchronized code blocks
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:51:35.