aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Subclasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Subclasses" Watch "Subclasses" New topic
Author

Subclasses

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi guys. I have a question about subclasses. If I have a "Base" class and a subclass "Sub" extended from "Base" like this:

public class Base { .... }
public class Sub extends Base {....}

what happends in the following operation?

Base B1 = new Sub();


Thank you.
Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
-------------------------------------------
public class Base { .... }
public class Sub extends Base {....}

what happends in the following operation?

Base B1 = new Sub();
------------------------------------------

A referrence variable B1 of type Base is created with Sub object. So, the B1 can see only the variable and method defined in Base object, not Sub.

Anybody have any other explantion?
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks for the reply. I have some confusions about that explanation.
So if I created an instance of class Sub with:

Base B1 = new Sub();

Suppose I a method subMethod in class Sub, that is not in class Base, what happens when I say:

Sub.subMethod(); ???

And also, if i override a Base class method in Sub, what happens if I call that method?
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
If u are instantiating a variable as follows:

<parent> obj = new <child>()

and if u have a method in parent which is overriden in child, than obj.method will call the method of child which is overriden. However for static methods this is different, i.e., method of parent will be called

Pls find the foll example

[ May 26, 2005: Message edited by: Nischal Tanna ]

Thnx
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
This is perfectly legal and makes use of late binding in Java, one of the most compelling reasons to use OOP.

If I were to declare Sub2 which extends Base, I could have a data structure which contained both Sub and Sub2 instances, then I could just cast each element of my Enumeration to type Base and call any method that Base declares (we'll call it method1() for example). Regardless of whether Sub, Sub2 or both have overridden method1(), the proper method1() for that class will be called.

If I wanted to use methods specific to Sub or Sub2, I would have to cast B1 down the hierarchie to the proper class, which would require either foreknowledge of the runtime class or the use of the instanceof operator.

As to your second question, Sub.submethod() would have to be a static method Otherwise you would need to call (Sub) B1.subMethod(); but see above for the problems which might occur.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Subclasses