Understanding Inheritance and the keyword "extends"

I am new and am currently reading inheritance and polymorphism. I get some confusion in terms of the “extend” keyword and how the constructors are called. Here is the code:

public class Test { public static void main(String[] args) { new B(); } } class A { int i = 7; public A() { System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { setI(20); System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; } } 

I know that by calling B () on line 3, the constructor of class A is called, and then B is called (is that right?), And therefore it displays "i from A is 7" and then "i from B is 60". But can someone explain the importance of this? Why is int i in B completely different from i in A? Again, I am having problems after the "path" of the code after the line new B (). If someone can explain every step after calling B (), that would be much appreciated.

+7
java
source share
5 answers

I am having problems after the "path" of the code after the line new B (). If someone can explain every step after calling B (), then it would be highly appreciated.

When calling new B() stream technically enters the constructor of B() . Java designers always need (ultimately) a chain to a higher constructor (recursively until they reach the highest Object class). The chain is indicated by the super(...) or this(...) operator as the first constructor statement. If not one of them is written explicitly, the imperceptible super() assumed. So, B() really compiles, as if it were written as follows:

  public B() { super(); setI(20); System.out.println("i from B is " + i); } 

Now you can clearly see that new B() calls B() , which calls A() (which calls println and exits), then setI and finally println .

Why is int i in B completely different from i in A?

i is the exact same field. The difference comes from the fact that you called setI(20) between two printouts, thereby changing the value of i . If you delete the call on setI , you will see that the value remains 7 .

+10
source share

When you create your instance of B and you call the constructor of B (), it first calls super() , which in your case will call A() .

Will print i from A is 7 because by default you set i to.

After calling super, it goes to the next line, which is setI(20) . This line sets i to B at 60 because your setI(int i) method multiplies parameter (20) by 3, and then sets i to this value.

Then you type i from B is 60 because i now 60.

+1
source share

Java language specification says

If the constructor body does not start with an explicit constructor call and the declared constructor is not part of the primary Object class, then the constructor body implicitly begins with a call to the constructor of the superclass "super ();", calling the constructor of its direct superclass that takes no arguments.

This means that when you write new B() , the first thing that the constructor for B is to call the constructor for A After the constructor for A set i to 7, the constructor for B calls setI(20) , which changes the value of i to 60. There is only one i - it does not differ from A > in B either. He simply changed the value between one println and the next.

+1
source share

A subclass should always invoke the constructor of its superclass via super () or by calling another constructor using super and the arguments of that constructor.

Unless you add super () explicitly, Java will do this behind the scenes.

 class B extends A { } 

Same as call:

 class B extends A { public B() { super(); } } 

Knowing that it should be obvious that calling new B(); will call constructor B, which then calls constructor A until completion.

0
source share

When you call new B() , VM calls implicity super (), then new A () writes 7, but in your setI you rewrite the method because you see 60.

Using anotation @Override is always a good idea.

BR

0
source share

All Articles