Sobes.tech
Junior — Middle

What is the application of inheritance mechanism in object-oriented programming?

sobes.tech AI

Answer from AI

Inheritance in object-oriented programming allows creating a new class based on an existing one, inheriting its properties and methods. This promotes code reuse and simplifies the extension of functionality.

Application of inheritance:

  • Creating class hierarchies that reflect the real model of the subject area.
  • Allows overriding methods of the base class to change behavior in derived classes.
  • Provides polymorphism, where objects of different classes can be used through a common interface.

Example in Java:

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Bark");
    }
}

Here, the Dog class inherits from Animal and overrides the sound method.

What is the application of inheritance mechanism in… - sobes.tech