- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Inheritance in Java with an example
In Java, inheritance is a fundamental concept of object-oriented programming (OOP), allowing a new class to be based on an existing class, inheriting all of its properties and methods while adding new features or functionality. In Java, inheritance is accomplished through the use of the extends keyword.
The following is the general form for inheritance implementation in Java:
class ParentClass { // member variables and methods } class ChildClass extends ParentClass { // additional member variables and methods }
The "class" keyword is used to define the parent class, and then the class's name is specified. Variables and methods can be defined as members of this class. Then, the child class is defined using the "class" keyword followed by the class name and the "extends" keyword followed by the parent class's name.
In addition to inheriting the member variables and methods from the parent class, the child class can define additional member variables and methods.
Once the child class is defined, objects of the child class can be created and its member variables and methods can be used, in addition to those inherited from the parent class.
Java inheritance example
Consider the following program as an example demonstrating inheritance in Java.
/* * MyClass is the parent class, * which has a name and age attribute, * and a speak() method to output the name and age. */ public class MyClass { private String name; private int age; public MyClass(String name, int age) { this.name = name; this.age = age; } public void speak() { System.out.println("My name is " + name + " and I am " + age + " years old."); } } /* * MyStudent is a child class of MyClass, * which inherits the name and age attributes and the speak() method, * and also has a major attribute and a study() method. */ public class MyStudent extends MyClass { private String major; public MyStudent(String name, int age, String major) { super(name, age); this.major = major; } public void study() { System.out.println(name + " is studying " + major + ".\n"); } } /* * MyTeacher is a child class of MyClass, * which also inherits the name and age attributes and the speak() method, * and has a subject attribute and a teach() method. */ public class MyTeacher extends MyClass { private String subject; public MyTeacher(String name, int age, String subject) { super(name, age); this.subject = subject; } public void teach() { System.out.println(name + " is teaching " + subject + "."); } } /* * InheritanceExample is the main class, * which creates objects of the child classes MyStudent and MyTeacher and calls their methods. */ public class InheritanceExample { public static void main(String[] args) { MyStudent myStudent = new MyStudent("Lucas", 22, "Java"); myStudent.speak(); myStudent.study(); MyTeacher myTeacher = new MyTeacher("William", 29, "Python"); myTeacher.speak(); myTeacher.teach(); } }
My name is Lucas and I am 22 years old. Lucas is studying Java. My name is William and I am 29 years old. William is teaching Python.
A parent class (MyClass) is extended by two child classes (MyStudent and MyTeacher) in this example. The name and age attributes, as well as the speak() method, are passed down from the parent class to both child classes.
MyStudent also has an additional attribute called major, and its study() method returns the student's name and major. The name of the teacher and the subject they are teaching are output by the methods teach() and an additional attribute called subject in the MyTeacher class.
Objects of both child classes are created and their respective methods are called in the main method of the InheritanceExample class. The inherited speak() and study() methods are called by the MyStudent object, while the inherited speak() and teach() methods are called by the MyTeacher object.
Advantages of inheritance in Java
- Programmers can save a ton of time and effort by reusing code from preexisting classes thanks to inheritance.
- It takes less time and effort to maintain code because all of a parent class's offspring automatically inherit any changes made to the parent class.
- Polymorphism, made possible by inheritance, enables the treatment of objects from various classes as if they were members of a single class, increasing the flexibility and readability of code.
- Abstract classes and interfaces are made possible by inheritance, and they offer a means of defining and enforcing a set of guidelines for subclasses.
Disadvantages of inheritance in Java
- Due to the tight coupling that inheritance causes between parent and child classes, the code is less adaptable and more challenging to change.
- A program can become more complex as a result of inheritance, especially when dealing with complex inheritance hierarchies.
- Inheritance is a delicate mechanism because changes to one parent class may have unintended effects on its child classes.
- Overusing inheritance can result in a bloated and overly complicated class hierarchy.
« Previous Tutorial Next Tutorial »