This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes which is the best way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "which is the best way" Watch "which is the best way" New topic
Author

which is the best way

amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Hello,
say for example i have 3 classes

public class a
{
public static void main(String args[])
{
}
void printa()
{
System.out.println("done");
}
}

class b extends a
{
void printb()
{
printa();
}
}
class c
{
void printc()
{
new a().printa();
}
}

my aim is to call from class a printa() method...
i have two options of doing it by class b and class c as shown...

what would be the best way of doing it...

my call on it is doing using class c...reason--->consider if class a has n number of methods and i want to access only one method...than in such a case why should i have overhead of those n number of methods when i only want to access one....

help appreciated
cheers
amal shah
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
When you extend a class, you need not have n methods. You can directly call them printa() instead of having another method printb().
Basically going for Inheritance or not depends on your broader requirement i.e what you are going to do in Class b and its utility
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Srikanth Ramu:
... Basically going for Inheritance or not depends on your broader requirement i.e what you are going to do in Class b and its utility

To expand on the point Srikanth made, your question is about whether to use composition (HAS-A) or inheritance (IS-A). In particular, should your new class have an instance of A, or should your new class be an instance of A? To answer this, we would need to know more about what these classes are intended to represent.

In Thinking in Java, Bruce Eckel suggests...
A general guideline is "Use inheritance to express differences in behavior, and fields to express variations in state."

For a more detailed discussion of this question, see the Designing with inheritance section from TIJ's polymorphism chapter.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
tahnks Srikanth,marc......that helped.......
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

Be careful because sometimes inheritance is more attractive than composition (usually, it's easier to implement). However, it's often not the best choice. Particularly, there're two interesting quotes you should have in your mind (well... I do .

"Prefere composition to inheritance" (GoF - Gand of Four)
"Inherit, not to reuse, but to be reused." (Herb Sutter and Andrei Alexandrescu)


Leandro Melo
SCJP 1.4, SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which is the best way