hqt
hqt

Reputation: 30284

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 ::)

Upvotes: 3

Views: 475

Answers (5)

Marcus
Marcus

Reputation: 1876

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();
  }

}

Upvotes: 3

kandarp
kandarp

Reputation: 5047

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();

Upvotes: 1

jabu.10245
jabu.10245

Reputation: 1892

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.

Upvotes: 0

bhavesh1988
bhavesh1988

Reputation: 151

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.

Upvotes: 0

jpprade
jpprade

Reputation: 3664

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.

Upvotes: 0

Related Questions