The Show
Before we learn about the Uses of super keyword
Let’s understand each of these uses. 1. Access Overridden Methods of the superclassIf methods with the same name are defined in both superclass and subclass, the method in the subclass overrides the method in the superclass. This is called method overriding. Example 1: Method overriding
Output I am a dog In this example, by making an object dog1 of Dog class, we can call its method printMessage() which then executes the Since What if the overridden method of the superclass has to be called? We use Example 2: super to Call Superclass Method
Output I am a dog I am an animal Here, how the above program works. 2. Access Attributes of the SuperclassThe superclass and subclass can have attributes with the same name. We use the Example 3: Access superclass attribute
Output: I am a mammal I am an animal In this example, we have defined the same instance field type in both the superclass Animal and the subclass Dog. We then created an object dog1 of the Dog class. Then, the Inside the
Hence, 3. Use of super() to access superclass constructorAs we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use
Example 4: Use of super()
Output I am an animal I am a dog Here, when an object dog1 of Dog class is created, it automatically calls the default or no-arg constructor of that class. Inside the subclass constructor, the The flow of the program then returns back to the subclass constructor and executes the remaining statements. Thus, I am a dog will be printed. However, using So, why use redundant code if the compiler automatically invokes super()? It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor. The parameterized Example 5: Call Parameterized Constructor Using super()
Output Type: Animal I am a dog The compiler can automatically call the no-arg constructor. However, it cannot call parameterized constructors. If a parameterized constructor has to be called, we need to explicitly define it in the subclass constructor. Note that in the
above example, we explicitly called the parameterized constructor Which reserved word must be used to call a method of a superclass?The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.
What restriction is there on using the super reference in a constructor?What restriction is there on using the super reference in a constructor? a. It can only be used in the parent's constructor.
Which statement is true about the accessibility of data in the number axles variable?Which statement is true about the accessibility of data in the numberAxles variable? It is only accessible by the Vehicle class's methods, by all of its subclasses, and by methods in classes within the same package.
Which of the following does a subclass not inherit from a superclass?Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
|