
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 and to inherit one class to another class we use extends keyword. Before making a java program to allocate and initialize super class members using constructor, let's go through some concepts we are going to use in this article.
What is Constructor?
Constructor is very similar to methods but the difference is that methods define the behavior of an object but constructor is used to initializing those objects.
We can provide any name of our choice to methods but a constructor must have same name as class name.
Also, methods can return a value but constructor does not return any value because they can't have any return type.
When user don't create any 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
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
this and super keyword
this keyword is used to differentiate local variables from instance variables of methods of same class but, a super keyword is used to differentiate members of super class from members of sub class.
this keyword is used to invoke methods, constructors and variables of current class but super keyword is used to invoke methods and constructors of base class.
Example 1
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
In the above code, we have created a parameterized constructor ?Main' along with a parameter ?name' of string type. That's why while calling the constructor we have used the argument ?Tutorialspoint'.
Example 2
The following example 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
In the above code, class ?Tutorix' is inheriting the ?Tutorialspoint' class. In ?show()' method of child class ?Tutorix' we are trying to access ?name' variable of its parent class ?Tutorialspoint' using super keyword. In the main method, we have created an object of class ?Tutorix' using new keyword. We have called the show() method using that object.
Allocatation and Initialization of super class member using constructor
Example
class P { String item_name; int rate; int quantity; P(String item_name, int rate, int quantity) { // Constructor of parent class 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) { C obj = new C("paid"); // calling the Child class constructor } }
Output
I am super class constructor I contain Milk 60 2 I am sub class constructor paid
In the above code, we have created a parent class ?P' with three variables and a constructor along with three parameters. Class ?C' is the sub class of ?P'. In ?C' we have used super keyword to allocate values to parent class ?P'.
Conclusion
In this article, we have understood how the constructor is different from a normal method and also the use of this and super keyword. At the end, we created a java program that allocates and initializes super class members using constructor.