What is super in a constructor of a child class?

I am currently facing an understanding problem with a keyword super

. I know what super

is a reference to the next superclass of a specific subclass. Anyway, I want to show my problem:

public class Father{
    int length; 

    Father(String s){
        length = s.length(); 
    } 
}

public class Sub extends Father {
    char c; 

    Sub(String s) {
        super(s);
        c = s.charAt(0);
    }
}

      

What does a string do super(s)

in a Sub class? What happens if I change the Father class from public to abstract?

+3


source to share


4 answers


If your method overrides one of its superclass methods, you can call the overridden method using the super keyword .

If we are talking about constructors, then:

  • c the super()

    constructor with no superclass arguments is called;
  • with the super(param list)

    superclass constructor is called with the appropriate parameter list.

In your example, the super(s)

corresponding constructor from the parent class will be called in the constructor of the Sub class.



What happens if I change the Father class from public to abstract?

The short answer is that when a class is declared abstract, it cannot be created:

Father father = new Father("Name"); // Error => 'Father' is abstract;
Father father = new Sub("Name");    // Works, if non-abstract Sub extends Father

      

+2


source


What happens if I change the Father class from public to abstract?

If you are creating an abstract father class, then you have the option to add abstract methods .

Since an abstract class may or may not have abstract methods , so in your code, the class fathers paragraph will not give any error.



What does the "super (s)" string do in the Sub class

It calls the parameterized constructor of the Father class. Even if you did not explicitly call the constructor of the Father class, the default constructor of the Father class is called .

+1


source


super

the keyword is always used to denote a superclass.

super(s)

means calling a superclassical constructor with a matched argument.

So when you call super(s)

it calls the super constructor

Father(String s){
       length = s.length(); 
    } 

      

0


source


public class Sub extends Father {
  char c; 
   Sub(String s) {
   super(s);
   c = s.charAt(0);
   System.out.println(c);
   }
}

public class Sub extends Father {
  char c; 
   Sub(String s) {
   super(s);
   c = s.charAt(0);
   System.out.println(c);
   }
}

public class MainClass {

    public static void main(String[] args) {
        Sub s = new Sub("INDIA");

    }
}

      

O / P: 5 I

if you change the father class to Abstract and also will be the same Explanation: super (s) is the keyword used to invoke the superclass class, so it just calls the Father (super) class with the arguments constructor if you change the father class as an abstract class , the functionality will be the same as you do not remove your 1 arguments constructor so it will call your abstract class constructor the same

0


source







All Articles