Skip to content

passthesnowwe/Java-Object-Oriented-Programming-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Object-Oriented Programming Examples

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.

Project Structure

Inheritance Examples

Animal Hierarchy

  • Animal.java - Base class with atype field and aDes() method
  • Dog.java - Extends Animal, demonstrates variable shadowing
  • Cat.java - Extends Animal, demonstrates method overriding and overloading
  • SuperThis.java - Demonstrates the use of super keyword
  • ShadowOverride.java - Demonstrates variable shadowing and method overriding

Person/Student Hierarchy

  • 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

Banking System (Polymorphism Example)

  • PolyBankAccount.java - Base bank account class
  • PolySavingAccount.java - Extends PolyBankAccount, adds interest calculation
  • PolyBankingApp.java - Demonstrates polymorphism with bank accounts

Class Composition vs Inheritance

  • 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

Key Concepts Demonstrated

1. Inheritance

  • Classes extending other classes to inherit properties and methods
  • Examples: Student extends Person, Dog extends Animal, PolySavingAccount extends PolyBankAccount

2. Polymorphism

  • PolyBankingApp.java demonstrates runtime polymorphism where a PolyBankAccount reference can point to a PolySavingAccount object
  • Method calls are resolved at runtime based on the actual object type

3. Method Overriding

  • Subclasses override parent class methods (e.g., toString() in PolySavingAccount, aDes() in Dog and Cat)

4. Method Overloading

  • Cat.java demonstrates method overloading with multiple aDes() methods having different parameters

5. Variable Shadowing

  • 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

6. Super and This Keywords

  • SuperThis.java demonstrates using super to access parent class members
  • Various classes show super() in constructors to call parent constructors

7. Composition vs Inheritance

  • B.java shows composition (has-a relationship)
  • BB.java shows inheritance (is-a relationship)
  • Both approaches achieve similar functionality through different design patterns

Compilation and Execution

Compile All Java Files

javac *.java

Run Individual Applications

Animal Examples

java SuperThis
java ShadowOverride

Student Application

java StudentApp

Banking Application

java PolyBankingApp

Composition/Inheritance Examples

java AApp
java BApp

Notes

  • Some files have .bak backup versions
  • Compiled .class files are included in the project
  • StudentApp.java uses JOptionPane for user input (requires GUI environment)
  • PolyBankingApp.java calls a pay() method that may need to be implemented in PolyBankAccount or its subclasses

Learning Objectives

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 super and this keywords effectively
  • The concept of variable shadowing in inheritance

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages