Below is my code...
Its not that much complex.
Here I want to understand that In class D, b is an interface type variable and in that variable we are storing reference to new object of class(C) which implements that interface(B). How we are able to assign object of type C to interface B type variable b..? Class and Interface both are of different types then what makes it special when we implements a interface on class and we are able to do it which is my question
public interface A {
}
public interface B {
public A methodABCD();
}
public class C implements B {
final A typeA;
public A methodABCD() {
return typeA;
}
}
public class D {
static private B b;
static public A methodABCD() {
if (b == null) {
b = new C();-------------->How..?
}
return b.methodABCD();
}
}
-
1Whatever type class extends or implements it belongs to this type. Interfaces and classes have many types.Jay Smith– Jay Smith2017年07月05日 04:52:09 +00:00Commented Jul 5, 2017 at 4:52
-
I@Jay so you mean the whole class becomes the type of what it implements..? In my code new C() is of B interface type ...thats what you mean...?LearnJava– LearnJava2017年07月05日 10:39:19 +00:00Commented Jul 5, 2017 at 10:39
-
Yes. There is nothing to learn. Better learn design patterns, oopJay Smith– Jay Smith2017年07月10日 06:52:14 +00:00Commented Jul 10, 2017 at 6:52
5 Answers 5
Ok lets take an Example and illustrate it.
interface Animal {
public void eat();
}
class Human implements Animal{
public void eat(){
// Eat cakes and Buns
}
}
class Dog implements Animal{
public void eat(){
// Eat bones
}
}
Now let see how you can use them
Animal h = new Human();
Animal d = new Dog();
Now at some point you might want to change your Human to behave like a Dog.
h =d;
h.eat();// Eat bones
Your thought became a possibility because, they belongs to the same type. Imagine that there is not Animal interface and see how difficult it is convert a Human to Dog.
You see the flexibility and the advantages in type varying. You are allowed to do that because of they both are Animal nothing but an Interface.
This is valid
B b = new C();
only because C implements the interface B, so you are telling the compiler:
"I need an object B that can do something instead of that is something...", this approach is called programming to interfaces and allows you to latter change the class C for a class F as long as F can do something too, that is a more flexible design...
-
My question is how B b and new C() are having same types...? Is its because of implementation....I want to understand this concept ...?LearnJava– LearnJava2017年07月05日 04:36:38 +00:00Commented Jul 5, 2017 at 4:36
-
is not that they are the same type... is about the reference yuo are declaring....ΦXocę 웃 Пepeúpa ツ– ΦXocę 웃 Пepeúpa ツ2017年07月05日 04:37:53 +00:00Commented Jul 5, 2017 at 4:37
-
check your object b and verify that you can not call methods from class C but only from the interfaceΦXocę 웃 Пepeúpa ツ– ΦXocę 웃 Пepeúpa ツ2017年07月05日 04:38:34 +00:00Commented Jul 5, 2017 at 4:38
-
So you mean I am storing a location of object of class C in interface type variable b..? If yes...Then It really doesnt matter that reference variable and object type should be same..?LearnJava– LearnJava2017年07月05日 04:40:57 +00:00Commented Jul 5, 2017 at 4:40
-
1If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface. It is a java language specification. The developers of Java language made it that way.hermit– hermit2017年07月05日 04:48:04 +00:00Commented Jul 5, 2017 at 4:48
Java hides the memory addresses of the objects created in Heap. Objects are accessed by the references. One object may have multiple references. Using =
operator references are made to refer to an object and using .
operator references can invoke a particular behavior of the object. References and objects are stored in different memory locations.
If there is an objext X
of class C
then as per the Java language specifications an X
can have references whose type is C
or any super class in higher hierarchy or any interface implemented by C
or any of the super class in higher hierarchy or any interface extended by any of these interfaces.
class A implements IA{}
class B extends A implements IB{}
interface IC extends IA{}
interface IB extends ID{}
class E{}
class F extends B{}
Now new B()
can have references of type A
,B
,IA
,IB
,ID
but can not have reference of type E
,IC
,F
as these do not belong to the higher lever hierarchy.
You can use interface as a reference type in java. It can only refer to objects of those classes that implement that interface. But remember with interface as reference you can access only those methods that are declared in that interface. Your class may define additional methods but that won't be accessible using the interface reference.
In a way when you say class and interface are of different type you are right but when a class implements an interface it provides definition to all the methods declared in that interface and hence that interface can refer to the implementing class object.
It is kind of like with inheritance.
When a class implements an interface it is bound under a contract to provide implementation to all the methods. So when a interface refers to a class object you can be pretty sure that that class must have implemented that interface and hence all your methods declaration now have definitions that can be called.
It is because of one of the design principles, Liskov Substituion Principles, L out of SOLID, if B is a subtype of P, then objects of type P can be replaced with instantiations of type B. Search SOLID design principles on google for more details. An object oriented language follows this