Super () Java Compiler Generators

Possible duplicate:
Using the super keyword to access undefined superclass methods

I am new to Java and have read a lot about this recently in order to gain more knowledge and experience regarding the language. I have a question about inherited methods and class extensions when the compiler inserts automatic code.

I read that if I create a class A with some methods, including a say method called checkDuePeriod() , and then create a class B that extends class A and its methods.

If I then call the checkDuePeriod() method in class B without using the super.checkDuePeriod() syntax, the compiler will turn on super. at compile time super. before checkDuePeriod() or there will be the fact that the compiler includes the super() constructor automatically when compiling the class, the super. call is implied super. methods called by class B from class A?

I am a little confused by this. Thanks in advance.

+6
source share
4 answers

The implementation of the superclass of regular methods is not automatically called in subclasses, but the form of the constructor of the superclass must be called in the constructor of the subclass.

In some cases, a super() call is implied, for example, when a superclass has a default constructor (without parameters). However, if the default constructor does not exist in the superclass, the subclass constructors must directly or indirectly reference the superclass constructor.

Example default constructor:

 public class A { public A() { // default constructor for A } } public class B extends A { public B() { super(); // this call is unnecessary; the compiler will add it implicitly } } 

A superclass without a default constructor:

 public class A { public A(int i) { // only constructor present has a single int parameter } } public class B extends A { public B() { // will not compile without direct call to super(int)! super(100); } } 
+5
source

If you call checkDuePeriod() in B without super. , so you want to call the method that belongs to this instance (represented by this inside B) B. So this is equivalent to saying this.checkDuePeriod() , so the compiler just doesn't make sense to add super. to the begining.

super. - this is what you must explicitly add in order to tell the compiler that you want to call the version of the method (this is required especially if B has redefined the implementation provided by A for the method).

+1
source

Calling super () as the default constructor (a constructor with no arguments) may be direct or indirect, but it is gallant that the fields of the extensible class are correctly initialized .

eg:

 public class A { StringBuilder sb; public A() { sb = new StringBuilder(); } } public class B extends A { public B() { //the default constructor is called automatically } public void someMethod(){ //sb was not initialized in B class, //but we can use it, because java garants that it was initialized //and has non null value sb.toString(); } } 

But in the case of methods:

Methods implement some logic. And if we need to rewrite the logic of the superclass, we use

 public class B extends A { public B() { } public boolean checkDuePeriod(){ //new check goes here } } 

and if we want to just do some extra checking using the value returned from checkDuePeriod () of the superclass, we should do something like this

 public class B extends A { public B() { } public boolean checkDuePeriod(){ if(super.checkDuePeriod()){ //extra check goes here }else{ //do something else if need } return checkResult; } } 
+1
source

First about Constructors :

- When an object class is ever created, its constructor initialized and at that time immediately its constructor s uper-class is called before the class object ,

- In this process, all instance variables are declared and initialized.

- Consider this scenario.

The dog is a sub-class Canine , and Canine is a sub-class Animal

Now, when the Dog object is initialized before the object is actually formed, the object of the Canine class must be formed, and before the Canine object can form the object of the Animal class, it must be formed, and before this object of the Object class must be shaped

Thus, the sequence of the formed object:

Object ---> Animal ---> Canine ---> Dog

So, Constructor of the Super-Class is Called before the Sub-Class .

Now with Method :

The most specific version of the method that class is called.

For instance:

 public class A{ public void go(){ } } class B extends A{ public static void main(String[] args){ new B().go(); // As B has not overridden the go() method of its super class, // so the super-class implementation of the go() will be working here } } 
0
source

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


All Articles