
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Allocate and Initialize Super Class Members Using Constructor in Java
In Java, super refers to the parent class instance, and to inherit members of the super class to the child class, we use the extends keyword. Before writing a Java program to allocate and initialize super class members using a constructor, let's go through some concepts we are going to use in this article.
What is Constructor?
A Java constructor is a class member that initializes instance variables of a class. It is very similar to a method, but the difference is that methods can return a value, but a constructor does not return any value because it can't have any return type.
Also, we can provide any name of our choice to methods, but a constructor must have the same name as the class name. When users don't create a constructor, then the Java compiler will automatically create one (we call it as default constructor).
Syntax
public class Constructor_example { Constructor_example() { // constructor // code to be executed } }
Example of Constructor
An example of the constructor is given below:
public class Cnst { Cnst(){ // constructor System.out.println("I am constructor"); } public static void main(String[] args) { Cnst obj = new Cnst(); // calling the Constructor } }
Output:
I am constructor
The this and super Keyword
The this and super keywords are used in different scenarios, which are discussed below:
-
The this keyword is used to differentiate local variables from instance variables of methods of the same class, but the super keyword is used to differentiate members of the super class from members of the subclass.
-
The this keyword is used to invoke methods, constructors, and variables of the current class, but the super keyword is used to invoke methods and constructors of the base class.
Example 1: Use of this Keyword
The following example illustrates the use of this keyword
public class Main { String name = "Your name"; Main(String name) { // Constructor this.name = name; // Represents the variable of constructor Main System.out.println(name); } public static void main(String[] args) { Main obj = new Main("Tutorialspoint"); } }
Output:
Tutorialspoint
Example 2: Use of the super Keyword
The following Java program illustrates the use of super keyword:
class Tutorialspoint { String name="Your name"; } public class Tutorix extends Tutorialspoint { String name = "My name"; public void show() { // To access name variable of class Tutorialspoint System.out.println("Accessing base class name: " + super.name); // To access local variable of class Tutorix System.out.println("Accessing child class name: " + name); } public static void main(String[] args) { Tutorix obj = new Tutorix(); obj.show(); } }
Output:
Accessing base class name: Your name Accessing child class name: My name
Allocation and Initialization of super Class Members
To allocate and initialize super class members using a constructor, the constructor of the subclass must call the constructor of the superclass using the super keyword.
Example
In the Java program given below, we have created a parent class and a constructor. Here, in the subclass, we have used the super keyword to allocate values to the parent class.
class P { String item_name; int rate; int quantity; // Constructor of parent class P(String item_name, int rate, int quantity) { this.item_name = item_name; this.rate = rate; this.quantity = quantity; System.out.println("I am super class constructor"); System.out.println("I contain " + item_name + " " + rate + " " + quantity); } } public class C extends P { // Inheriting the parent class using extends keyword C(String status) { // Constructor of child class super("Milk", 60, 2); // allocating values to parent class members System.out.println("I am sub class constructor "); System.out.println(status); } public static void main(String[] args) { // calling the Child class constructor C obj = new C("paid"); } }
Output:
I am super class constructor I contain Milk 60 2 I am sub class constructor paid