- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Role of super Keyword in Java Inheritance
Parent class objects can be referred to using the super keyword in Java. It is usually used in the context of inheritance. A program that demonstrates the super keyword in Java is given as follows:
Example
class A { int a; A(int x) { a = x; } } class B extends A { int b; B(int x, int y) { super(x); b = y; } void print() { System.out.println("Value of a: " + a); System.out.println("Value of b: " + b); } } public class Demo { public static void main(String args[]) { B obj = new B(7, 3); obj.print(); } }
Output
Value of a: 7 Value of b: 3
Now let us understand the above program.
The class A contains a data member a and constructor A(). The class B uses the extends keyword to derive from class A. It also contains a data member b and constructor B(). The method print() prints the values of a and b. A code snippet which demonstrates this is as follows:
class A { int a; A(int x) { a = x; } } class B extends A { int b; B(int x, int y) { super(x); b = y; } void print() { System.out.println("Value of a: " + a); System.out.println("Value of b: " + b); } }
In the main() method in class Demo, an object obj of class B is created. Then the method print() is called. A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String args[]) { B obj = new B(7, 3); obj.print(); } }
- Related Articles
- super keyword in Java
- Equivalent of Java Super Keyword in C#
- What are the uses of super keyword in java?
- Super keyword in JavaScript?
- What are the three usages of the super keyword in Java?
- Super keyword in Dart Programming
- Golang program to show use of super keyword in class
- How does Python's super() work with multiple inheritance?
- Why can't we use the "super" keyword is in a static method in java?
- Types of inheritance in Java
- Inheritance in Java
- Multilevel inheritance in Java
- Get super class of an object in Java
- Inheritance in C++ vs Java
- Single level inheritance in Java

Advertisements