Extending Classes (extends keyword)
Class inheritance in Java programming is an important and useful fundamental concept of object-oriented programming (OOP). It allows Java users to declare a root class to use and apply the properties, data type fields, and methods defined in another subclass’s Behavior. The class inheritance concept is implemented using the extends keyword, which allows a root class to extend another subclass and inherit its features and functions from multiple subclass properties.

What is the inheritance concept in Java?
Inheritance features in Java class programming allow Java users to create a new inherited class based on an existing class. Here, a new class, called a subclass or child class, is created from the base or root class, allowing other subclasses to inherit class-defined fields, data types, and function methods from an existing class, called the superclass or root parent class. Subclasses can also define their own additional class fields and function methods within the existing class, or override or modify inherited class methods if needed.
Inheritance syntax using the extends keyword in Java.
To inherit or extend a class created in a Java program, Java users apply the extends keyword, followed by the name of the user-defined desired root or superclass. Here, the subclass defined in the root superclass inherits the public and protected class data type members and elements declared in the superclass. This provides access permissions to subclasses, but it does not grant Java users access to private class members.
extends keyword syntax.
class Subclass extends Superclass {
/ Here we can add additional class fields and methods for one or more subclasses.
}
Example of the inheritance concept in a Java class.
So, let’s declare two classes in a class, with Course defined as the superclass and Java as the subclass. Here, the Java subclass inherits properties and behavioural method features from the Course class.
Superclass(Course):
public class Course {
// Here we define a course instance variable of the superclass
String course_name;
int price;
// Here we create a course class constructor
public Course(String course_name, int price) {
this.course_name = course_name;
this.price = price;
}
// here we define Method of the course superclass
public void select_course() {
System.out.println(“Select a course”);
}
// here we define Getter methods for course class
public String getCourse_name() {
return course_name;
}
public int getPrice() {
return price;
}
}
Subclass (Java):
public class Java extends Course {
// here we define an Additional field specific to Java extended class
String duration;
//here we define a Constructor for Java class
public Java(String course_name, int price, String duration) {
// here it Calling the constructor of the superclass element
super(course_name, price);
this.duration = duration;
}
// Here we are overriding the select_course() method of the Course class.
@Override
public void select_course() {
System.out.println(course_name + ” you select a Java course”);
}
// Here we are using the Getter method for the duration of the Java subclass data type.
public String getDuration() {
return duration;
}
}
Explanation of the inheritance concept.
Superclass or root class (Course).
Here, the Course superclass defines class fields like course_name and price. A constructor is created to initialize the Course class fields, and a method select_course() is defined, which prints a user-generated course message.
Subclass based on superclass (Java).
Here, the Course superclass defines a Java class that extends the Course class. This means that the Java subclass inherits the course_name and price field data types and the select_course() class method from the Course class.
An additional field, duration, is defined in the Java subclass, which is a special subclass element for the Java subclass.
Here, the Java subclass overrides the select_course() method to provide its own implementation, which is a special feature for the Java subclass.
Using the super keyword in Java classes.
Here, the constructor of the Java subclass Course calls the super(course_name, price) method to invoke the constructor of the superclass Course and initialize the inherited fields course_name and price as class field data types.
The super keyword in a Java class can be used to access or process superclass methods or variables in a class method that have been hidden or overridden by the subclass in the current root superclass.
Use the Main class to Analyze inheritance features in Java.
public class Main {
public static void main(String[] args) {
/ Here we are creating an object of the Course superclass
Course course = new Course(“Java”, 3999, “2 Month”);
/ Here we are accessing inherited methods from the Course class
System.out.println(course.getCourse_name()); // here it Inherited from Course super class
System.out.println(course.getPrice()); // here it Inherited from Course super class
System.out.println(course.getDuration()); // here it is specific to course super class
// here it Calling the overridden select_course()() method from Course class
course.select_course()(); // Result – you select java course
}
}
