Access Super Class Methods and Instance Variables Without super Keyword in Java
A class describes the property and the attributes of the object. A class contains mainly includes the following components.
- Modifiers: The keywords in java which provide a set of restriction on the class and its members and methods.
- Class keyword: The initialization of the class is done by class reserved word and following by the name of the class.
- Class Name: The name of the class that must follow the rules as follows while creating the variables in java.
Super class : The parent class from which many subclasses can be created. All the subclasses have all the attributes and properties that have parent class.
Inheritance is the mechanism of object-oriented language by which any class can (child class) inherit other class all the properties and behaviour of the parent class.
Parent class: GFG Child class: GFGChild
Simple Format of Inheritance:
There are two methods to call the instance variables and methods of the superclass (parent class) in the child class.
1. First Method: super keyword is one of the reserved words in java. Super refers to an object of the parent class. (Refer to this article).
- We can invoke the overridden method of the parent class with the help of the super keyword.
- super() is used for executing the constructor of the parent class and should be used in the first line in the derived class constructor.
This is the helloworld class This is the GFG1 class helloworld is the name
2. Second Method: Without using the keyword super keyword after inheritance all the methods and instance variables of the parent class is inherited by the child class. So we can direct them in the child class.
GFG class: parent class Arraylist class: Derived class
This is the constructor Hello world
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.