public class InheritanceExample {
static public void main(String[] args){
Cat c = new Cat();
System.out.println(c.speak());
Dog d = new Dog();
System.out.println(d.speak());
}
}
public class Animal {
protected String sound;
public String speak(){
return sound;
}
}
public class Cat extends Animal {
protected String sound = "meow";
}
public class Dog extends Animal {
protected String sound = "woof";
}
null
null
My animals cannot speak. So sad.
The problem occurs when there exist methods with the same signature in both the superclasses and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.
Java supports only Single, Multilevel, and Hierarchical types of inheritance. Java does not support Multiple and Hybrid inheritance.
Java doesn't support multiple inheritances in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritances.
Java inheritance refers to the ability of a Java Class to inherit the properties from some other Class. Think of it like a child inheriting properties from its parents, the concept is very similar to that. In Java lingo, it is also called extend-ing a class.
Fields aren't polymorphic. You've declared three entirely distinct fields... the ones in Cat
and Dog
shadow or hide the one in Animal
.
The simplest (but not necessarily best) way of getting your current code is to remove sound
from Cat
and Dog
, and set the value of the inherited sound
field in the constructor for Cat
and Dog
.
A better approach would be to make Animal
abstract, and give it a protected constructor which takes the sound... the constructors of Cat
and Dog
would then call super("meow")
and super("woof")
respectively:
public abstract class Animal {
private final String sound;
protected Animal(String sound) {
this.sound = sound;
}
public String speak(){
return sound;
}
}
public class Cat extends Animal {
public Cat() {
super("meow");
}
}
public class Dog extends Animal {
public Dog() {
super("woof");
}
}
You cannot override class fields, only methods. The sound
field in your Dog
and Cat
classes is actually hiding the sound
field in the Animal
superclass.
You can, however, access superclass fields from subclasses, so you could do something like this:
public class Dog extends Animal {
public Dog() {
sound = "woof";
}
}
public class Cat extends Animal {
public Cat() {
sound = "meow";
}
}
Or, you can make the Animal
class abstract, and declare the speak
method abstract too, then define it in subclasses:
public abstract class Animal {
public abstract String speak();
}
public class Dog extends Animal {
public String speak {
return "woof";
}
}
public class Cat extends Animal {
public String speak {
return "meow";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With