This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question on Abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on Abstract class" Watch "Question on Abstract class" New topic
Author

Question on Abstract class

karthik manick
Ranch Hand

Joined: Aug 25, 2009
Posts: 52
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

Joined: Aug 05, 2005
Posts: 3418
    
  12
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

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
. . . 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

Joined: Aug 25, 2009
Posts: 52
My referece xx is like this:
abs1 xx= method1();
Here method1 has return type of abs1.(which is abstract class)
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

Right. So which add() method it calls depends completely on the actual type (which you can get with getClass()).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Koonce
Ranch Hand

Joined: Oct 30, 2010
Posts: 36
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?


Jason K. (AC)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Yes, anonymous classes are used very frequently as event handlers. Also Runnables are frequently anonymous.
 
 
subject: Question on Abstract class
 
Similar Threads
abstract method
Can you guess the output without compiling?
an abstract class implementing an interface
static and abstract methods
abstract methods