> For the complete documentation index, see [llms.txt](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://muhammad-tri-wibowo.gitbook.io/java-tutorials/object-oriented-programming-oop/inheritance-and-polymorphism.md).

# Inheritance and Polymorphism

**Inheritance:**

Inheritance is one of the core concepts of object-oriented programming. It allows a class (subclass/child class) to inherit properties and behaviors from another class (superclass/parent class). Inheritance promotes code reuse and establishes relationships between classes.

**Syntax of Inheritance:**

```java
class ParentClass {
    // superclass members
}

class ChildClass extends ParentClass {
    // subclass members
}
```

**Example of Inheritance in Java:**

```java
// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class (subclass) inheriting from Animal
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}
```

In this example, the `Dog` class is a subclass of the `Animal` class. `Dog` inherits the `sound()` method from `Animal`. If you create a `Dog` object and call `sound()`, it will print "Dog barks".

**Polymorphism:**

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility in programming by allowing methods to work with objects of any subclass of the declared type. Java supports two types of polymorphism: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).

**Runtime Polymorphism (Method Overriding):**

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows a subclass to provide a specialized version of a method that is already defined in its superclass.

**Example of Method Overriding:**

```java
// Parent class (superclass)
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

// Child class (subclass) overriding the sound() method
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}
```

In this example, the `sound()` method in the `Dog` class overrides the `sound()` method in the `Animal` class. When you call `sound()` on a `Dog` object, it prints "Dog barks" instead of "Animal makes a sound".

**Example with Polymorphism:**

```java
public class PolymorphismExample {

    public static void main(String[] args) {
        Animal myDog = new Dog(); // Polymorphic statement
        myDog.sound(); // Output: Dog barks
    }
}
```

In this example, `myDog` is declared as an `Animal` type but is actually referring to a `Dog` object. This is an example of polymorphism. When `sound()` is called, it invokes the overridden method in the `Dog` class, demonstrating runtime polymorphism. This flexibility simplifies code and allows for more dynamic behavior in Java programs.
