File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which is the best way

 
amal shah
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amal shah
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tahnks Srikanth,marc......that helped.......
 
Leandro Melo
Ranch Hand
Posts: 401
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic