简体   繁体   中英

type of object while implementing interface

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();
  }
}

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM