简体   繁体   中英

What does super(s) in Child-class constructor?

I am currently facing an understanding-problem with super keyword. I know that super is a reference to the next super class of a specific subclass. Anyway I want to show you 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 the line super(s) do in the Sub class? What will happen if I change the Father class from public to abstract?

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super .

If we are talking about constructors, then:

  • with super() , the superclass no-argument constructor is called;
  • with super(param list) , the superclass constructor with a matching parameter list is called.

In your example super(s) in the Sub class constructor will invoke an appropriate constructor from the parent class.

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

Short answer - when a class is declared abstract, it can't be instantiated:

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

What will happen if i change the Father class from public to abstract?

If u make Father class abstract,then you have the opportunity to add abstract methods to it.

Since an abstract class may or may not have abstract methods ,So in your code making Father class abstract will not give any error.

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

It calls the parametrized constructor of Father class.Even if you haven't explicitly called the constructor of Father's class, default constructor of Father class will be called.

super keyword is always used to refer the super class.

super(s) means calling super class constructor with matched argument.

So when you call super(s) it calls the super constructor

Father(String s){
       length = s.length(); 
    } 
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 father class to Abstract than also will be same Explanation: super(s) is the keyword used to call super class constructor so it just call a Father(super) class with argument constructor if you change your father class as abstract class there functionality will be same as you are not removing your 1 argument constructor so it will same way to call your abstract class constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM