简体   繁体   中英

Java OOP: how subclass-object use method from superclass (that method has been Override)

I have this example:

public class Inheritance {

    public static class Animal {        
        public void Scream() {
            System.out.println("I'm an animal");
        }       
    }

    public  static class Mammal extends Animal{
        public void Scream(){
            System.out.println("I'm a mammal");
        }       
    }

    public  static class Tiger extends Mammal{
        public void Scream(){
            System.out.println("I'm a tiger");
        }       
    }

    public static void main (String[] args){

        Animal tiger = new Tiger();
        tiger.Scream();
    }

}

Of course, I will receive: "I'm a tiger". But, I don't know how to print "I'm a mammal" or "I'm an animal"

@: and please answer for me sub-question: in case Tiger class, What is superclass of Tiger . Mammal or Animal ?

Please help me :)

Thanks ::)

Here is code sample :

public class Inheritance {

  public static class Animal {        
      public void Scream() {
          System.out.println("I'm an animal");
      }       
  }

  public  static class Mammal extends Animal{
      public void Scream(){
          super.Scream();
          System.out.println("I'm a mammal");
      }       
  }

  public  static class Tiger extends Mammal{
      public void Scream(){
          super.Scream();
          System.out.println("I'm a tiger");
      }       
  }

  public static void main (String[] args){
      Animal tiger = new Tiger();
      tiger.Scream();
  }

}

In method overriding, java will always check whose object has been created at run time. If you wanna a print "I'm a mammal":

Animal m = new Mammal();
m.Scream();

this will print "I'm a mammal".

And if you wanna a print "I'm a animal":

Animal a = new Animal();
a.Scream();

Java is not like c++ where this could be possible.

You can call super.scream() in each scream method but it will display both :

"I'm a mammal" and "I'm an animal".

I am not sure why you would want to do that.

To print "I'm a mammal" you should make an object of Mammal class.

 public static void main (String[] args){ 

    Animal mammal = new Mammal(); 
    mammal.Scream(); 
} 

To print "I'm an animal" you should make an object of Animal class.

 public static void main (String[] args){ 

    Animal animal = new Animal(); 
    animal.Scream(); 
} 

NOTE: In both of the above cases reference variable remains Animal.

1) As the other guys already said, call super.Scream() if you want to call the overridden method from the super class. You would have the following output:

I'm an animal
I'm a mammal
I'm a tiger

2) Tiger implements Mammal, so Mammal is the superclass of Tiger, and since Animal is the superclass of Mammal, Tiger also implements Animal. In other words: Tiger's superclasses are Tiger and Animal.

// all of this is valid:
Tiger tiger = new Tiger();
Mammal mammal = tiger;
Animal animal = tiger;

If you call animal.Scream() the virtual machine knows that anmial is of type Tiger and calls the scream method declared in the Tiger class. See Polymorphism .

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