I have two example. First is true and second is wrong. It’s just different at one point: Animal class and Mammal class.
public class Inheritance {
public static class Animal {
public void Scream() {
System.out.println("I'm an animal");
}
}
public static class Mammal extends Animal{
//no Scream method
}
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(); //True
}
}
Second example:
public class Inheritance {
public static class Animal {
// no Scream method
}
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(); //false. Error
}
}
Thanks 🙂
@: I have edited . I have typed mismatch.
I tested your 2 classes and they are both working.
I wouldn’t work if you try something like :
Animal tiger = new Tiger();
on the second case, because Animal has not Scream() method
BTW: Usually, method names start with lower case 😉