This project contains Java examples demonstrating key object-oriented programming concepts including inheritance, polymorphism, method overriding, method overloading, and the use of super and this keywords.
- Animal.java - Base class with
atypefield andaDes()method - Dog.java - Extends Animal, demonstrates variable shadowing
- Cat.java - Extends Animal, demonstrates method overriding and overloading
- SuperThis.java - Demonstrates the use of
superkeyword - ShadowOverride.java - Demonstrates variable shadowing and method overriding
- Person.java - Base class with personal information (SSN, name fields)
- Student.java - Extends Person, adds college class information
- StudentApp.java - Application demonstrating Student class usage
- PolyBankAccount.java - Base bank account class
- PolySavingAccount.java - Extends PolyBankAccount, adds interest calculation
- PolyBankingApp.java - Demonstrates polymorphism with bank accounts
- A.java - Simple class with
hello()method - B.java - Uses composition (has an A object)
- BB.java - Uses inheritance (extends A)
- AApp.java - Demonstrates composition approach
- BApp.java - Demonstrates inheritance approach
- Classes extending other classes to inherit properties and methods
- Examples:
Student extends Person,Dog extends Animal,PolySavingAccount extends PolyBankAccount
- PolyBankingApp.java demonstrates runtime polymorphism where a
PolyBankAccountreference can point to aPolySavingAccountobject - Method calls are resolved at runtime based on the actual object type
- Subclasses override parent class methods (e.g.,
toString()in PolySavingAccount,aDes()in Dog and Cat)
- Cat.java demonstrates method overloading with multiple
aDes()methods having different parameters
- Dog.java and Cat.java demonstrate how subclass variables can shadow parent class variables
- ShadowOverride.java shows the difference between variable shadowing and method overriding
- SuperThis.java demonstrates using
superto access parent class members - Various classes show
super()in constructors to call parent constructors
- B.java shows composition (has-a relationship)
- BB.java shows inheritance (is-a relationship)
- Both approaches achieve similar functionality through different design patterns
javac *.javajava SuperThis
java ShadowOverridejava StudentAppjava PolyBankingAppjava AApp
java BApp- Some files have
.bakbackup versions - Compiled
.classfiles are included in the project - StudentApp.java uses
JOptionPanefor user input (requires GUI environment) - PolyBankingApp.java calls a
pay()method that may need to be implemented in PolyBankAccount or its subclasses
After reviewing this code, you should understand:
- How to create class hierarchies using inheritance
- The difference between method overriding and method overloading
- How polymorphism works in Java
- When to use composition vs inheritance
- How to use
superandthiskeywords effectively - The concept of variable shadowing in inheritance