I have a class A
, then a class B
that inherits from , A
and then a class C
that inherits from B
and implements an interface C
.
It would be something like this:
public class A {}
public class B extends A {}
public class C extends B implements Cinterface{}
The interface has a method let's call it
void Coger(){}
If in the class Main
I create an object of the class B
- which inherits from the classA
B Hola = new B("Ra.one",12,93,84);
I do not understand why the object of the class that I just created does not have access to the interface B
method if it is inside the class that inherits from the class .void Coger()
Cinterface
C
B
What am I doing wrong?
First, some concept
In the object-oriented programming paradigm, specifically in inheritance and polymorphism, the verb to be is very useful to clarify the mind (and some concepts).
A class hierarchy implies that an object of a descendant class is a valid instance of the parent class, but not the other way around.
It's easier to see it with an example, only instead of giving classes totally abstract names, we'll give them names from some known hierarchy to help us understand the example.
Suppose we have the following hierarchy:
If we take an instance (object) of the Human class, that Human is in turn a mammal, it is an animal and it is a living being.
But if we take an instance of any of , that living being is
SerVivo
not necessarily an Animal, that is, it could be, but it could also be a Plant, a Bacteria or another type of living being.This is why it is valid to do assignments like these:
now to your question
That is the reason why an instance of the class
B
does not have the methods declared in the descendant classC
.Let's go back to the example.
We can imagine some basic methods of
SerVivo
, such asBy declaring them in the class
SerVivo
, all living things (instances of classes that inherit from our abstract classSerVivo
) will have those 4 methods available.Going down, we can imagine some class methods such as
The fact that some of these methods are part of an interface implementation is irrelevant. The important thing is that you can see that it makes sense that the method
hablar();
does not exist in an instance ofMamífero
, nor in an instance ofSerVivo
. This method only exists on instances of BeingHuman.Beyond that, even if you have an instance of Human, but you reference it with a variable from an ancestor class, you can't access the methods of the descendant class either, only those declared in the class you declared the variable on. You can solve this by applying type casts.
Going back to the examples:
If you want your object to have access to the Get method. You should instantiate your object to C
Your class B does not have the Get method. Then it won't work for you. Your C class has the Get method, it will work for you.
Mainly it does not have the Take method because C is the one that implements Cinterface, if you look at your code
For what you could do so that they both have the same interface is the following:
With that you make the implementation of Take in B and if you need to override the method in C you can do it without problems, being able to use it like this: