This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Abstract class

 
karthik manick
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a abstract class abs1
i have a method add() in abs1;
class a extends abs1
class b extends abs1
the add method was overriden in both classes a and b(both have add() method);
i am creating a reference for abs1 like abs1 xx=new abs1();
if i call xx.add(), which add() method will get called?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik manick wrote:i am creating a reference for abs1 like abs1 xx=new abs1();
if i call xx.add(), which add() method will get called?

If abs1 is abstract you can't do that. You can only do
abs1 xx=new a();
or
abs1 xx=new b();
in which case the add method of the class you created will be called
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or you would have to create an object of an anonymous class, but we don't do that sort of thing on "beginning".
 
karthik manick
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My referece xx is like this:
abs1 xx= method1();
Here method1 has return type of abs1.(which is abstract class)
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method may have its return type as an abstract class or even an interface. It does not mean that it returns an instance of the abstract class or interface (none of which is possible).

In this case, the instance returned by the method is of one of the concrete child classes. For example, if your method is returning 'abs1', the actual instance returned by the method would be of class 'a' or 'b' (considering these are non-abstract classes).
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. So which add() method it calls depends completely on the actual type (which you can get with getClass()).
 
Jason Koonce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . or you would have to create an object of an anonymous class, but we don't do that sort of thing on "beginning".


I just learned anonymous inner classes with event handlers...are they used, anonymous classes that is, very often?
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, anonymous classes are used very frequently as event handlers. Also Runnables are frequently anonymous.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic