Access Super Class Methods and Instance Variables Without super Keyword in Java - GeeksforGeeks

20 Nov, 2020
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.
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:
Java
// Access Super Class Methods and Instance// Variables Without Super Keyword in Java  import java.io.*;  class GFG {    // super class (parent class)    // instance variables    // class methods}  class GFGChild extends GFG {    // GFGChild child class of GFG class    // instance variables of GFGChild class    // as well as GFG class    // class methods of GFGChild class as well as GFG class}
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).
Uses:
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.
Implementation:
Java
// Access Super Class Methods and Instance// Variables With Super Keyword in Javaimport java.io.*;  // super classclass helloworld {        // instance variable    String name = "helloworld is the name";    void print()    {        System.out.println("This is the helloworld class");    }}// derived classclass GFG1 extends helloworld {        // invoking the instance variable of parent class    String name = super.name;    void print()    {        // calling the overriden method        super.print();        System.out.println("This is the GFG1 class");                // printing the name        System.out.println(name);    }}class GFG {    public static void main(String[] args)    {        // instance of the derived class        GFG1 ob = new GFG1();                // calling the unoverriden method print        ob.print();    }}
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
Implementation:
Java
// Access Super Class Methods and Instance// Variables Without Super Keyword in Javaclass Arraylist extends GFG {    void hello()    {        System.out.println("This is the Main class");    }    public static void main(String args[])    {        // calling the constructor        Arraylist ob = new Arraylist();        // calling the inherited name method        ob.name();    }}class GFG {    GFG()    { // constructor of the parent  class        System.out.println("This is the constructor");    }      void name() { System.out.println("Hello world"); }}
This is the constructor
Hello world
Java-keyword
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?