How many objects are created due to inheritance in java?

前端 未结 14 1631
长情又很酷
长情又很酷 2020-11-29 18:48

Let\'s say I have three classes:

class A {
    A() {
        // super(); 
        System.out.println(\"class A\");
    }
}
class B extends A {
    B() {
             


        
14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 19:40

    The super keyword enables a subclass to call the methods and fields of its superclass. It is not an instance of the superclass object but a way to tell the compiler which methods or fields to reference. The effect is the same as if the subclass is calling one of its own methods. Examples:

    Consider a subclass Employee that extends its superclass Person:

    public class Employee extends Person{
    
       public Employee()
       {
         //reference the superclass constructor 
         super(); 
       }
    
       public String getName()
       {
         //reference superclass behaviors
         return super.getFirstName() + " " + super.getLastName();
       }
     } 
    

    The super keyword can be used to reference the constructer of the Person class or any of the behaviors or fields that it has access to (e.g., getFirstName() and getLastName()).

提交回复
热议问题