Inheritance allows you to define a new class based on an existing class. In this section, you'll explore inheritance, polymorphism, and abstract classes.
- Inheritance - Reusing code from existing classes.
- Polymorphism - Using a single interface to represent different types.
- Super Keyword - Accessing parent class members.
- Abstract Classes - Working with abstract classes and methods.
BoxWeight.java
- Inherit classBox
and add theweight
variable.SuperDemo.java
- Use thesuper
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).
- Create a class
BoxWeight
that inherits fromBox
and adds aweight
attribute. - Write a program to implement polymorphism using dynamic method dispatch.
- Create an abstract class
Figure
and implementRectangle
andTriangle
subclasses.
Continue to Packages & Interfaces after completing these.
- AbstractFigure.java: Demonstrates the use of abstract classes and methods.
- BoxWeight.java: Inherit class
Box
and add theweight
variable. - DynamicDispatch.java: Demonstrates dynamic method dispatch (runtime polymorphism).
- SuperDemo.java: Demonstrates the use of the
super
keyword to call the parent class constructor.