Skip to content
Related Articles

Related Articles

Access Super Class Methods and Instance Variables Without super Keyword in Java
  • Last Updated : 20 Nov, 2020

A class describes the property and the attributes of the object.  A class contains mainly includes the following components.

  1. Modifiers: The keywords in java which provide a set of restriction on the class and its members and methods.
  2. Class keyword: The initialization of the class is done by class reserved word and following by the name of the class.
  3. 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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Access Super Class Methods and Instance
// Variables With Super Keyword in Java
import java.io.*;
  
// super class
class helloworld {
    
    // instance variable
    String name = "helloworld is the name";
    void print()
    {
        System.out.println("This is the helloworld class");
    }
}
// derived class
class 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();
    }
}

chevron_right


Output

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Access Super Class Methods and Instance
// Variables Without Super Keyword in Java
class 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"); }
}

chevron_right


Output

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :