GeeCON Prague 2014*
The moose likes Java in General and the fly likes inheritance problem with references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "inheritance problem with references " Watch "inheritance problem with references " New topic
Author

inheritance problem with references

naval kumar
Ranch Hand

Joined: Jul 08, 2012
Posts: 37
i m confused with very simple example of Inheritance like.
class A
{
int k=1;
void show()
{
System.out.println("===inside A class= Show()=="+k);
}
}
class B extends A
{
int k=8;
void show()
{

System.out.println("===inside B class=Show()=="+k);
}
void show1()
{

System.out.println("===inside B class show1() ===");
}
}

public class Inheritance
{
public static void main(String s[])
{
A a=new B();
a.show();
//a.show1();//error
System.out.println("===="+a.k);

}
}
in the above example i m making reference of superclass to Subclass object. Now i have two Question.
1-Why we Do, means===> A a=new B(); Why not use B=new B(); or A a=New A();When We should do Like it A a=new B();
2-when i call a.show(); its print "==inside B class=Show()==" while it should show "==inside A class= Show()==" because as i know Reference Of A class know only A class member only.
because if i will a.show1(); it give Error Because show1() is function of class B. and if it show class B member also then i print a.k it show "1". why not "8" defined in Class B."
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

naval kumar wrote:i m confused with very simple example of Inheritance like.

in the above example i m making reference of superclass to Subclass object. Now i have two Question.


naval kumar wrote:
1-Why we Do, means===> A a=new B(); Why not use B=new B(); or A a=New A();When We should do Like it A a=new B();


You have to understand that this is an example program uses for learning purposes. A probable use case is a very large method is take a parameter of type A, that has been coded to work with A instances in a large program. Then one small part of the program needed to enhance A (to B). This part also needs to call the method -- which works because B IS-A A. When the method is call, this is similar to what is happening with the parameters. Of course, if that was the example, you wouldn't learn much from it.

naval kumar wrote:
2-when i call a.show(); its print "==inside B class=Show()==" while it should show "==inside A class= Show()==" because as i know Reference Of A class know only A class member only.
because if i will a.show1(); it give Error Because show1() is function of class B. and if it show class B member also then i print a.k it show "1". why not "8" defined in Class B."


Methods are polymorphic -- they always call the overridden version. And the example is an A reference pointing to a B instance. Of course, all of this is figured out at runtime -- the compiler actually creates a call to the method using a jump table (filled in at runtime) to the correct method.

As for the show1() method, the compiler only works at compile time. It doesn't know that the instance is type B at compile time, hence, it complains.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
GeeCON Prague 2014
 
subject: inheritance problem with references