Type of object when implementing the interface

Below is my code ...

It is not that difficult.

Here I want to understand that in class D, b is an interface type variable, and in this variable we store a reference to a new object of class (C) that implements this interface (B). How can we assign an object of type C to a variable of type B of type b ..? The class and the interface are of different types, which makes it special when we implement the interface on the class, and we can do this, 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(); } } 
+5
source share
5 answers

Ok lets you 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's see how you can use them

 Animal h = new Human(); Animal d = new Dog(); 

Now at some point you can change your Human to behave like a Dog.

  h =d; h.eat();// Eat bones 

Your thought became possible because they belong to the same type. Imagine there is no Animal interface and see how difficult it is to transform a person into a dog.

You see the flexibility and benefits of various types. You are allowed to do this because they are both animals and not an interface.

+2
source

It's really

 B b = new C(); 

just because C implements the B interface, so you tell the compiler:

"I need an object B that can do something, not something ...", this approach is called programming for interfaces and allows you to change the class C to class F , if F can also do something, it’s more flexible design ...

+1
source

Java hides the memory addresses of objects created on the heap. Objects are referenced. One object can have several links. Using = operator references refers to the object and using . operator references may refer to specific object behavior. Links and objects are stored in different memory cells.

If there is an objext X class of class C , then, in accordance with the specifications of the Java X language, it can have links of type C , or any superclass in the highest hierarchy or any interface implemented in C or any of the superclass in the highest hierarchy or any interface extended by any from 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 links of type A , B , IA , IB , ID , but cannot have a link of type E , IC , F as they do not belong to a higher hierarchy of leverage.

+1
source

You can use the interface as a reference type in java. It can relate only to objects of those classes that implement this interface. But remember that as a reference you can only refer to those methods that are declared in this interface. Your class may define additional methods, but this will not be accessible through a link to the interface.

In a sense, when you say that a class and an interface are of a different type, you are right, but when a class implements an interface, it provides a definition for all the methods declared in this interface, and therefore this interface can refer to an object of the implementation class.

It is like inheritance.

When a class implements an interface, it is contracted to provide an implementation for all methods. Therefore, when an interface refers to an object of a class, you can be sure that this class must implement this interface, and therefore, all declarations of your method now have definitions that can be called.

0
source

This is due to one of the design principles, the Liskov Substituion Principles, L from SOLID, if B is a subtype of P, then objects of type P can be replaced by instances of type B. For more information on Google's SOLID design principles. Object Oriented Language Follows This

0
source

Source: https://habr.com/ru/post/1269500/


All Articles