Skip to content

Latest commit

 

History

History

04_Inheritance

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Inheritance & Polymorphism

Inheritance allows you to define a new class based on an existing class. In this section, you'll explore inheritance, polymorphism, and abstract classes.

Topics Covered:

  1. Inheritance - Reusing code from existing classes.
  2. Polymorphism - Using a single interface to represent different types.
  3. Super Keyword - Accessing parent class members.
  4. Abstract Classes - Working with abstract classes and methods.

Example Programs:

  • BoxWeight.java - Inherit class Box and add the weight variable.
  • SuperDemo.java - Use the super keyword to call the parent class constructor.
  • AbstractFigure.java - Demonstrate the use of abstract classes and methods.
  • DynamicDispatch.java - Demonstrate dynamic method dispatch (runtime polymorphism).

Exercises:

  1. Create a class BoxWeight that inherits from Box and adds a weight attribute.
  2. Write a program to implement polymorphism using dynamic method dispatch.
  3. Create an abstract class Figure and implement Rectangle and Triangle subclasses.

Continue to Packages & Interfaces after completing these.


Files

  • AbstractFigure.java: Demonstrates the use of abstract classes and methods.
  • BoxWeight.java: Inherit class Box and add the weight variable.
  • DynamicDispatch.java: Demonstrates dynamic method dispatch (runtime polymorphism).
  • SuperDemo.java: Demonstrates the use of the super keyword to call the parent class constructor.