Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Inheritance in Java

Java Inheritance in Java

Inheritance in Java

Inheritance is a fundamental concept in object-oriented programming (OOP). In Java, inheritance allows one class to inherit the properties and behaviors (fields and methods) of another class. It helps in code reuse and establishing a relationship between different classes.

Key Concepts:

  1. Super Class (Parent Class): The class whose properties and methods are inherited.

  2. Sub Class (Child Class): The class that inherits the properties and methods of another class.

Syntax:

class SuperClass {    // SuperClass fields and methods}class SubClass extends SuperClass {    // SubClass fields and methods}

Types of Inheritance in Java:

  1. Single Inheritance: A class inherits from one superclass.

  2. Multilevel Inheritance: A class inherits from another class which itself is inherited from another class.

  3. Hierarchical Inheritance: Multiple classes inherit from a single superclass.

  4. Multiple Inheritance (not supported directly): A class cannot directly inherit from more than one class. However, multiple inheritance can be achieved through interfaces.


Example of Inheritance (Single Inheritance)

// SuperClass (Parent Class)class Animal {    void sound() {        System.out.println("Animals make different sounds");    }}// SubClass (Child Class) inherits from Animal classclass Dog extends Animal {    // Overriding the method of the superclass    @Override    void sound() {        System.out.println("Dog barks");    }}public class InheritanceExample {    public static void main(String[] args) {        Dog dog = new Dog();        dog.sound();  // Calls the overridden method in Dog class    }}

Output:

Dog barks

Explanation:

  1. The Animal class is the superclass.

  2. The Dog class is the subclass that extends the Animal class.

  3. The sound() method is overridden in the Dog class to provide a more specific implementation.

  4. When dog.sound() is called, the method in the Dog class is executed.


Example of Multilevel Inheritance

// Grandparent classclass Animal {    void eat() {        System.out.println("Animals can eat");    }}// Parent class inherits from Animalclass Mammal extends Animal {    void giveBirth() {        System.out.println("Mammals give birth to live young");    }}// Child class inherits from Mammalclass Dog extends Mammal {    void bark() {        System.out.println("Dog barks");    }}public class MultilevelInheritance {    public static void main(String[] args) {        Dog dog = new Dog();        dog.eat();         // Inherited from Animal class        dog.giveBirth();   // Inherited from Mammal class        dog.bark();        // Defined in Dog class    }}

Output:

Animals can eatMammals give birth to live youngDog barks

Explanation:

  1. Animal is the grandparent class.

  2. Mammal is the parent class that inherits from Animal.

  3. Dog is the child class that inherits from Mammal.

  4. The Dog class has access to the methods from both Mammal and Animal through multilevel inheritance.


Example of Hierarchical Inheritance

// Superclassclass Animal {    void eat() {        System.out.println("Animals can eat");    }}// Subclass 1class Dog extends Animal {    void bark() {        System.out.println("Dog barks");    }}// Subclass 2class Cat extends Animal {    void meow() {        System.out.println("Cat meows");    }}public class HierarchicalInheritance {    public static void main(String[] args) {        Dog dog = new Dog();        dog.eat();  // Inherited from Animal class        dog.bark(); // Defined in Dog class        Cat cat = new Cat();        cat.eat();  // Inherited from Animal class        cat.meow(); // Defined in Cat class    }}

Output:

Animals can eatDog barksAnimals can eatCat meows

Explanation:

  • Both Dog and Cat classes inherit from the same superclass Animal. This is hierarchical inheritance.

  • Both subclasses have access to the eat() method from the Animal class, but they also define their own specific behaviors.


Accessing Superclass Members (super Keyword)

In Java, you can use the super keyword to refer to the superclass members (methods and variables) from the subclass.

Example: Accessing a Superclass Method

class Animal {    void sound() {        System.out.println("Animal makes sound");    }}class Dog extends Animal {    void sound() {        super.sound();  // Call the superclass method        System.out.println("Dog barks");    }}public class SuperKeywordExample {    public static void main(String[] args) {        Dog dog = new Dog();        dog.sound();  // Calls the Dog class method and also the Animal class method    }}

Output:

Animal makes soundDog barks

Explanation:

  • The super.sound() in the Dog class calls the sound() method from the Animal class.

  • The subclass method (Dog's sound()) can call the superclass method using the super keyword.


Constructor in Inheritance

When a subclass is created, the constructor of the superclass is called automatically before the subclass constructor, using the super() keyword. You can also explicitly call the superclass constructor.

Example: Constructor in Inheritance

class Animal {    Animal() {        System.out.println("Animal constructor");    }}class Dog extends Animal {    Dog() {        super();  // Calling the superclass constructor        System.out.println("Dog constructor");    }}public class ConstructorInheritanceExample {    public static void main(String[] args) {        Dog dog = new Dog();    }}

Output:

Animal constructorDog constructor

Explanation:

  • The super() call in the Dog constructor explicitly calls the constructor of the Animal class before executing the Dog class's constructor.


Method Overriding in Inheritance

When a subclass defines a method that is already defined in its superclass, it is called method overriding. The subclass's method overrides the method in the superclass.

Example: Method Overriding

class Animal {    void sound() {        System.out.println("Animal makes sound");    }}class Dog extends Animal {    @Override    void sound() {        System.out.println("Dog barks");    }}public class MethodOverridingExample {    public static void main(String[] args) {        Animal animal = new Animal();        animal.sound();  // Calls Animal's sound        Dog dog = new Dog();        dog.sound();     // Calls Dog's sound    }}

Output:

Animal makes soundDog barks

Explanation:

  • The sound() method is overridden in the Dog class. When dog.sound() is called, the overridden method in the Dog class is executed instead of the one in the Animal class.


Conclusion

  • Inheritance allows a class to inherit the properties and behaviors of another class, enabling code reuse and establishing a relationship between classes.

  • You can override methods, use the super keyword to refer to the superclass, and extend functionality through subclassing.

  • Java supports single, multilevel, and hierarchical inheritance. However, multiple inheritance is not directly supported through classes but can be achieved using interfaces.

Let me know if you'd like more detailed examples or have any questions! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql