I have two classes Animal and Dog. As you can quess dog extends from animal. I can write these classes with no problem but I noticed that I can create a new dog object like this:
Dog firstDog = new Dog("rocky");
It's ok, but when I try to create new instance like this:
Animal secondDog = new Dog("alex");
There isn't any error but I can't reach any fields or methods that I wrote in Dog class even if they are public so if I want to hold an object in a variable whose type is same with its super class's type, I can reach field and methods that implemented only that super class (for this example name, color, setColor() and toString()) and there isn't any way to reach fields and methods which implemented in subclass. Did I understand correctly also when I try to call toString() function of secondDog it calls the method that I wrote in Dog class, I know I override this function in Dog class but I can't reach other methods that I implemented in Dog class. How java handle these?
Animal Class:
public class Animal {
private String name;
private String color;
public Animal(String name){
this.name=name;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return"Hi, my name is " + this.name + ". I'm " + this.color;
}
}
Dog Class:
public class Dog extends Animal {
private int age;
public Dog(String name){
super(name);
setColor("gray");
this.age = 7;
}
public String speakDog(){
return"wof!";
}
public String toString(){
return super.toString() + " and I " + speakDog();
}
}
When you write this:
Animal secondDog = new Dog("alex");
You are telling Java secondDog is an Animal and that was it. The reason you are allowed to assign Dog into and Animal reference is because Dog is a subclass of Animal, hence it is allowed.
However, secondDog being explicitly an Animal will only be able to access properties and behaviours from Animal class.
To access properties and behaviours of Dog, you need to cast it:
((Dog)secondDog).speakDog();
Casting it to Dog is like telling Java: "Hey, trust me. This object is a Dog".
Only being more specific now, then you are allowed to access the properties of a Dog. (If not, it is still being treated as an Animal (higher class)).
However, if you cast it to a class which is not a subclass of Animal, you will get a ClassCastException.
In essence,
if secondDog remained as Animal (not being casted to Dog), it can only access all non private fields and methods from Animal.
if secondDog was casted as Dog, it can access all inherited fields and methods from Animal plus all non-private fields and methods from Dog.
No, it is not possible to access the properties of the Dog through the Animal class. Consider the base object "Object" from which many sub-classes flow. You can say JFrame frame = new JFrame(""); or Object objFrame = new JFrame(""); frame.??? will give you access to all the JFrame's properties objFrame.??? will only give you access to the Object's properties.
I hesitate to ask why secondDog should be created as an Animal and not a Dog:)
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