Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using the new operator vs Inheritance  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is better?

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

OR

MyClass extends SomeOtherClass {
MyClass.method();
}
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author
Marshal
Posts: 23438
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also not a topic about threads ... moving to the Java in General forum...
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case while considering Inheritance one has to keep in mind the Liskov Substitution Principle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!