简体   繁体   中英

accessing superclass object by subclass reference variable

why cant we access superclass object by subclass reference variable ..i have a code discussing my problem.

class A
{ 
     void    set()    
  {
    System.out.println("i am in a");
  }
}

class B extends A
{
   void set()
  {
    System.out.println("i am in b");
  }

  public static void main(String args[])
  {  
     A instance=new B(); //line 
     instance.set();  //this would call set() in B.
     B in=new A(); //But this line gives error
  }
}

Can anyone be of help?

A subclass may have members that the superclass doesn't, therefore it is not allowed to assign a superclass instance to a subclass reference.
Imagine that B has another method, subMethod() . If you could assign an A instance to a B reference, you could write the following code:

B example = new A();
example.subMethod();

which would break, since A doesn't have the subMethod() .

The other way around is fine though, since a subclass always has everything the superclass has.

Also, note that static methods can't be overridden, only instance methods are polymorphic.

Take another example similar to your problem,

class Animal
{ 
     void set()    
  {
    System.out.println("i am in animal");
  }
}

class Dog extends Animal
{
   void set()
  {
    System.out.println("i am in dog");
  }
  void bark(){
  System.out.println("woof woof");
  }

  public static void main(String args[])
  {  
     Animal instance=new Dog(); //line 
     instance.set();  //this would call set() in B.
     Dog in=new Animal(); //But this line gives error
  }
}

Inheritance follows a is-a relationship, now we can say Dog extends Animal and Dog is-a Animal. that makes sense. But Animal is-a Dog does not make sense.

When you write Animal instance=new Dog(); , You are assigning a Dog object to a Animal reference , which sounds intuitive, also the LHS (Animal instance) is the reference variable , and the job of the reference variable is to decide what functions to call on the object(Those present in Animal like set()) , You cannot call the bark() method using instance , because it is of type Animal reference, yes but you can call set() since it is present in Animal class. Now there are two version of set() one present in class Animal and class Dog, now which one to call depends on RHS (new Dog();) , since RHS is an instance and is of type Dog() , the version of set() in Dog will be called. To summarize, see the following code and output in comments:-

Animal a1=new Animal();
a.set();// output- i am in animal

Dog d1=new Dog();
d.set(); //output- i am in dog
d.bark; //valid and output- woof woof

Animal a2=new Dog();
a2.set();//output- i am in dog
a2.bark();// compilation error, you cannot refer to this method since it is not present in Animal class.

Dog d2=new Animal(); // compilation error

Inheritance is hierarchical.

You can create a class Person and a class Karl extends Person , with sayName() method,

then when you create a Karl it will fit into Person variables, but not every Person is a Karl .

If you want to explicitly call the superclass you could create a function saySuperName() in class Karl , where you call super.sayName() .

See also : http://docs.oracle.com/javase/tutorial/java/IandI/super.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM