You are here:

Java/inheritance problem with references of superclass to subclass

Advertisement


Question
Dear Sir,
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. No 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 i print a.k it show "1". why not "8" defined in Class B.

Answer
Hi,
This is an example of 2 things: inheritence and polymorphism.
This is why you do A a = new B().
If you did B b = new B() there would be no polymorphism.

Your API is based according to the field type - A.
Your behavior is based according to the instance type - B.

B override the method show(). So calling the method show() will act according to the actual instance - in this case "new B()".

A does not have the method show1() so it cannot be called with field type A.

Let me know if this helped,
Ziv

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ziv Ben-Eliahu

Expertise

I can answer questions relating to: Java language and usage, coding standards, application modeling and design (model driven development), unit/system testing, test driven development, algorithm complexity issues, Ant and XML-technologies.

Experience

I'm in the software industry for over 4 years now. I worked mainly with Java, but also with other languages. I gave practical session for "System Programming", in university.

Organizations
Open-Source community

Publications
WikiAI08

Education/Credentials
B.Sc. in Math and Computer Science, BGU.
M.Sc. in Computer Science, BGU.

Past/Present Clients
Open source (and free) projects: D.U.C.K (source-forge), JXN for wireXN.

Organizations
Open-Source community

Publications
WikiAI08

Education/Credentials
B.Sc. in Math and Computer Science, BGU.
M.Sc. in Computer Science, BGU.

Past/Present Clients
Open source (and free) projects: D.U.C.K (source-forge), JXN for wireXN.

Publications
WikiAI08

Education/Credentials
B.Sc. in Math and Computer Science, BGU.
M.Sc. in Computer Science, BGU.

Past/Present Clients
Open source (and free) projects: D.U.C.K (source-forge), JXN for wireXN.

Education/Credentials
B.Sc. in Math and Computer Science, BGU.
M.Sc. in Computer Science, BGU.

Past/Present Clients
Open source (and free) projects: D.U.C.K (source-forge), JXN for wireXN.

Past/Present Clients
Open source (and free) projects: D.U.C.K (source-forge), JXN for wireXN.

©2016 About.com. All rights reserved.