1.
Inheritance in Java (Concept, Types, Syntax & Example)
Definition
Inheritance is an important concept in Object-Oriented Programming. It allows one class
(child/subclass) to acquire the properties and methods of another class
(parent/superclass). It promotes code reusability and hierarchical classification.
In simple words, Inheritance means 'one class getting features from another class'.
Syntax of Inheritance
class ParentClass {
// parent class code
class ChildClass extends ParentClass {
// child class code
Example of Inheritance
class Animal {
void sound() {
System.out.println("Animal makes sound");
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // From parent class
d.bark(); // From child class
Types of Inheritance in Java
1. Single Inheritance (✔ Supported in Java)
2. Multilevel Inheritance (✔ Supported)
3. Hierarchical Inheritance (✔ Supported)
4. Multiple Inheritance using Interfaces (✔ Supported)
5. Hybrid Inheritance (❌ Not supported directly)
Note: Java does not support multiple inheritance with classes to avoid ambiguity.
It uses Interfaces to implement multiple inheritance.
2. Java Program: Simple Banking System using Single
Inheritance
Concept
We will create a simple banking system using the concept of Single Inheritance.
A base class BankAccount will hold general account details. A derived class
SavingsAccount will extend it and include interest calculation.
Java Code
class BankAccount {
String accountNumber;
double balance;
BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
void displayBalance() {
System.out.println("Account Number: " + this.accountNumber);
System.out.println("Current Balance: " + this.balance);
class SavingsAccount extends BankAccount {
double interestRate;
SavingsAccount(String accountNumber, double balance, double interestRate) {
super(accountNumber, balance);
this.interestRate = interestRate;
double calculateInterest() {
return (this.balance * this.interestRate) / 100;
void displaySavingsDetails() {
displayBalance();
System.out.println("Interest Rate: " + this.interestRate + "%");
System.out.println("Calculated Interest: " + calculateInterest());
public class Main {
public static void main(String[] args) {
SavingsAccount myAccount = new SavingsAccount("ACC12345", 10000.0, 5.0);
myAccount.displaySavingsDetails();
Output
Account Number: ACC12345
Current Balance: 10000.0
Interest Rate: 5.0%
Calculated Interest: 500.0