Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is Java Inheritance?

What is Java Inheritance?

What is Java Inheritance?

Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) in Java. It allows a new class (called a child or subclass) to inherit properties and behaviors (fields and methods) from an existing class (called a parent or superclass).

Syntax of Java Inheritance. 

The syntax for inheritance in Java involves creating a subclass that extends a superclass using the extends keyword. Here’s the basic structure:

Syntax: #

class Superclass {

    // Fields (variables)

    // Methods

}

class Subclass extends Superclass {

    // Additional fields (variables)

    // Additional methods or overriding methods

}

Types of Inheritance

1. Single Inheritance #

Definition: Single inheritance is where a subclass inherits from one superclass.

Example:

// Superclass

class Animal {

    void eat() {

        System.out.println("This animal eats food.");

    }

}

// Subclass

class Dog extends Animal {

    void bark() {

        System.out.println("The dog barks.");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog();

        myDog.eat(); // Inherited method

        myDog.bark(); // Subclass-specific method

    }

}

In this example, Dog inherits from Animal, and it can use the eat() method defined in Animal as well as its own bark() method.

2. Multilevel Inheritance

Definition: In multilevel inheritance, a class inherits from another class, which in turn inherits from another class, forming a chain.

Example:

// Base class

class Animal {

    void eat() {

        System.out.println("This animal eats food.");

    }

}

// Intermediate class

class Mammal extends Animal {

    void breathe() {

        System.out.println("This mammal breathes air.");
    }

}


// Derived class

class Dog extends Mammal {

    void bark() {

        System.out.println("The dog barks.");

    }

}

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog();

        myDog.eat(); // Inherited from Animal

        myDog.breathe(); // Inherited from Mammal

        myDog.bark(); // Dog-specific method

    }

}

3. Hierarchical Inheritance #

Definition: In hierarchical inheritance, multiple subclasses inherit from a single superclass.

Example:

// Superclass

class Animal {

    void eat() {

        System.out.println("This animal eats food.");

    }

}

// Subclass 1

class Dog extends Animal {

    void bark() {

        System.out.println("The dog barks.");

    }

}

// Subclass 2

class Cat extends Animal {

    void meow() {

        System.out.println("The cat meows.");

    }

}
public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog();

        Cat myCat = new Cat();

        myDog.eat(); // Inherited method

        myDog.bark(); // Dog-specific method

        myCat.eat(); // Inherited method

        myCat.meow(); // Cat-specific method

    }

}

In this example, both Dog and Cat inherit from Animal, but each has its own specific methods.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Advantages of Java OOPsWhat is Java Constructor?
Table of Contents
  • Syntax:
  • 1. Single Inheritance
  • 3. Hierarchical Inheritance
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105