What is Base b2 = new Child (); mean?

class Base { public void add() { System.out.println("Base ADD"); } void subtract() { throw new UnsupportedOperationException("Not yet implemented"); } } class Child extends Base { public void add(){ System.out.println("Child ADD"); } public void subtract() { System.out.println("Child Subtract"); } } class MainClass { public static void main(String args[]) { Base b1 = new Base(); Base b2 = new Child(); Child b3 = new Child(); b1.add(); b2.subtract(); b2.add(); b3.subtract(); } } 

I am a bit confused by the code above. The line that bothers me the most

 Base b2 = new Child(); 

and

  b2.subtract(); 

I understand that when compiling the compiler, the Base weather check has a subtract() method or not, then at run time polymorphism occurs when the object is of type Child .

The question is how and where can we use this line, i.e. Base b2 = new Child();

In which scenario should we use this? Please help, it would be great!

+4
source share
2 answers

Look at the two parts of the statement:

 Base b2 

which declares a variable of type b2 type Base . You can assign a reference to this variable if the reference is null or refers to an instance of Base or a subclass of Base .

Then

 new Child() 

creates a new instance of Child . Child is a subclass of Base , so you can assign the link returned by the constructor to b2 .

Now you can only see Base members through b2 , although the actual value refers to the Child instance at run time. But any methods from Base that were overridden in Child will use the overridden version when they are called ... so when you call

 b2.subtract(); 

The JVM detects the actual type of object that b2 belongs to, and uses this implementation of the subtract class - in this case, the method that prints "Child Subtract".

EDIT: You specifically asked where you can use such things, and how that helps ...

You can use it anytime you want to declare a variable of a more general type (superclass or interface), but assign a value to it, which is a subclass or implementation of the interface. As another example:

 List<String> strings = new ArrayList<String>(); 

The main advantage of declaring a variable in a general way is that you remain flexible - later you can switch from using ArrayList<T> to some other implementation of List<T> , and the code should still work. You basically say: "I only need the members and guarantees provided by List<T> - the fact that I use ArrayList<T> is somewhat random."

A similar example is the choice of the type of the returned method - often you want to declare that you are returning a generic type (or interface), although the implementation knows which concrete type it returns. This hides implementation details that allow you to modify them later.

+9
source
 Base b2 = new Child(); 

You can use the Super link for the child object.

for b2.method ();

at compile time you should have a method (); in BaseClass.

At run time, the Object method will be called, so here the object Child from Base b2 = new Child();

So, the version of child method() will be executed.

Application:

Polymorphism is extremely useful when developing enterprise-level systems, because you can create and define several interface levels that allow third-party or external software to interact with your system in a specific way. We all know how useful it is in system integration to have a published API, but if internal changes to the system become necessary, the API may need to be reissued, and integrated systems may need to be redesigned. To avoid this, during the design process, special attention should be paid to the object APIs, as well as to the general system APIs. One way to ensure that a reliable system can deal with objects that have more complex methodologies than was originally intended is to have an object API polymorphism. Thus, the system knows the “face” of the object transferred to it, but does not need to know about changes in time.

Also see

+1
source

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


All Articles