AP Computer Science A - Unit 9: Java Inheritance, Polymorphism, and Constructors
Overview
In this unit, we will cover three main topics: inheritance, polymorphism, and constructors in Java. Here's what we will learn:
- How methods are inherited and used in subclasses
- Understanding polymorphism and its applications
- Writing constructors in standalone classes and in a class hierarchy
Inheritance
Key Concepts
- Subclass: A class that inherits from another class.
- Example:
Tiger is a subclass of Animal.
- Superclass: A class that is inherited by another class.
- Example:
Animal is a superclass of Tiger.
- Inheritance relationship: Subclasses extend superclasses using the
extends keyword in Java.
- A Java class can have many subclasses but only one superclass.
- Access Modifiers:
public: Methods and fields are inherited and can be accessed from other classes.
private: Methods and fields are not inherited, but they can be accessed indirectly from within the class.
Example: Worker Class Hierarchy
- Worker Class: Does not explicitly extend another class, so it implicitly extends
Object.
- Tradesperson Class: Subclass of
Worker; inherits doWork and defines doSkilledWork.
- Carpenter Class: Subclass of
Tradesperson; inherits doWork and doSkilledWork, and defines doWoodwork.
- Method Access:
doWork can be called on any instance of Worker or its subclasses.
earnMinimumWage (private) cannot be directly accessed outside Worker.
Polymorphism
Understanding Polymorphism
- Definition: Allows reference variables to point to objects of their own class or any subclass.
- Example:
Animal variable can point to Animal, Mammal, Reptile, Lion, etc.
- Method Access:
- A reference variable can only access methods defined in its own class, even if pointing to a subclass.
Example Code
- Animals and their subclasses (Mammal, Reptile, etc.) show method accessibility and overriding.
- Reference variables determine method availability based on declared type, not object type.
Constructors
Constructors in Java
- Purpose: Run when creating a new object, setting up initial field values.
- Rules:
- Must have the same name as the class.
- Cannot have a return type.
Overloaded Constructors
- Allow different ways to create objects using different parameters.
- Example:
Solaria class shows use of this keyword and default constructor.
Constructor Inheritance
- Superclass Constructor Calls: Every constructor must call a superclass constructor, explicitly or implicitly.
- Example:
Vehicle, Airplane, and F16 demonstrate constructor chaining and superclass constructor calls.
Special Considerations
- Java provides a no-argument constructor if none are defined.
- Constructors can pass important information to superclass constructors.
Summary
This unit provides an understanding of how inheritance, polymorphism, and constructors work in Java. These concepts are crucial for organizing and structuring code in larger applications. Understanding these topics will aid in writing more efficient and manageable Java programs.