Reputation: 35
I'm learning Java and can't understand some tricks of inheritance. Example:
Animal
;Cat
, extends Animal
;Tiger
, extends Cat
;Questions:
1) Why Cat uses Tigers method? See below example of "make_a_sound" method. It's from Tiger, but Cat can use it.
2) Why Cat, can not see any properties of Tiger? Cat can use method "make_a_sound" of Tiger, but can not see its properties... quite strange.
Thanks, Gennadiy
public class Solution {
public static void main(string[] args) {
Cat cat = new Tiger();
// The result is from Tiger class: A tiger says RRRRR & new tiger property
// Why???
cat.make_a_sound();
// Only cat's and Animal's properties are visible. No Tiger's properties
System.out.println(cat.this_is_a_cat);
}
// Base class
public class Animal {
public String this_is_an_animal = "Animal";
public void make_a_sound() {
System.out.Printf("I'm an animal");
}
}
// Extends base class
public class Cat extends Animal {
public String this_is_a_Cat = "Hi, I'm a cat";
public void make_a_sound() {
System.out.Printf("A cat says meey");
}
}
// Extends prev. class
public class Tiger extends Cat {
public String this_is_a_Tiger = "Tiger";
public void make_a_sound() {
System.out.Println("A tiger says RRRRRR");
this_is_a_Tiger = "new tiger property";
System.out.println(new_tiger_property);
}
}
}
Upvotes: 0
Views: 311
Reputation: 2427
This link could give you some concept regarding Overriding in Java: https://www.geeksforgeeks.org/overriding-in-java/
For simplicity, Overriding
only take effective on methods (regarding Question 1), but not on field variables (regarding Question 2).
Upvotes: 0
Reputation: 216
Answer) Because when you say Cat cat = new Tiger() you are not saying any cat,you are saying you want sound of the Tiger and not any generic cat.Tiger is using cat's "make_a_sound" method by over riding the implementation by the Tigers sound.
if you had not overriden tigers "make_a_sound" method then the Cat's "make_a_sound" method will be called.You can try it out by saying super().make_a_sound in Tiger class or removing implementation of this method.
2) Why Cat, can not see any properties of Tiger? Cat can use method "make_a_sound" of Tiger, but can not see its properties... quite strange.
Answer) If parents classes would know everything about child class then OOPS will be broken.In OOPS each class should only know about what it does and nothing else.It does not care of other class responsibility.
Upvotes: 0
Reputation: 5601
You defined cat
as type Cat
. That means cat
can do anything that any other Cat
can do. The Animal
object does define the make_a_sound
function so it's available to all Animal
instances. When you run your code, the variable cat
points to a Tiger
instance so when make_a_sound
is called, it's the one in Tiger
that runs. That's the whole point of inheritance.
For your second question, cat
is of type Cat
so you can only do things with it that you can do with any Cat
object. Since this_is_a_Tiger
is not something every Cat
object has, it is not available to cat
even though cat
really points to a Tiger
.
But you could do this:
if (cat instanceof Tiger) {
Tiger tiger = (Tiger)cat;
System.out.println(tiger.this_is_a_Tiger);
}
Upvotes: 1