# 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://muhammad-tri-wibowo.gitbook.io/java-tutorials/object-oriented-programming-oop/inheritance-and-polymorphism.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
