Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Inheritance in Java

The document explains runtime polymorphism through method overriding, where a subclass provides a specific implementation of a method from its superclass, with the method executed determined at runtime. It highlights the benefits of polymorphism, including flexibility, reusability, maintainability, and extensibility of code. An example in Java demonstrates how different animal classes can override a generic sound method.

Uploaded by

11210208
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance in Java

The document explains runtime polymorphism through method overriding, where a subclass provides a specific implementation of a method from its superclass, with the method executed determined at runtime. It highlights the benefits of polymorphism, including flexibility, reusability, maintainability, and extensibility of code. An example in Java demonstrates how different animal classes can override a generic sound method.

Uploaded by

11210208
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

• Runtime Polymorphism (Method Overriding): A subclass provides a

specific implementation of a method already defined in its superclass. The


method to be executed is determined at runtime based on the object's actual
type.
o Example:
Java
class Animal {
public void makeSound() { System.out.println("Generic animal
sound"); }
}

class Dog extends Animal {


@Override
public void makeSound() { System.out.println("Woof!"); }
}

class Cat extends Animal {


@Override
public void makeSound() { System.out.println("Meow!"); }
}

Benefits of Polymorphism:
• Flexibility: Code can work with objects of multiple related types without
knowing their specific subclass.
• Reusability: Methods can be written once and used with different object
types.
• Maintainability: Code becomes easier to understand and modify.
• Extensibility: New classes can be added to the hierarchy without modifying
existing code.

You might also like