Principles
Code with Detailed Comments:
// Object-Oriented Programming (OOP) Concepts in Java
import java.util.*;
// 1. Class and Object
class Animal {
// A base class to represent an animal
protected String name; // Public Attribute (protected for inheritance)
protected String species; // Public Attribute
public Animal(String name, String species) {
this.name = name;
this.species = species;
}
public String makeSound() {
// A general method to make a sound
return "Some generic sound";
}
@Override
public String toString() {
// toString Method to return a string representation
return this.name + " is a " + this.species;
}
}
// 2. Encapsulation: Protecting data using private attributes
class BankAccount {
// A class to demonstrate Encapsulation
private String accountHolder; // Public attribute
private double balance; // Private attribute (Encapsulation)
public BankAccount(String accountHolder, double balance) {
this.accountHolder = accountHolder;
this.balance = balance;
}
public String deposit(double amount) {
// Public method to deposit money
if (amount > 0) {
this.balance += amount;
return "Deposited " + amount + ", New Balance: " + this.balance;
}
return "Invalid deposit amount";
}
public String withdraw(double amount) {
// Public method to withdraw money
if (0 < amount && amount <= this.balance) {
this.balance -= amount;
return "Withdrew " + amount + ", Remaining Balance: " + this.balance;
}
return "Insufficient funds";
}
public double getBalance() {
// Getter method to access private attribute
return this.balance;
}
}
// 3. Abstraction: Hiding complex details and exposing only necessary parts
abstract class Vehicle {
// Abstract class that forces subclasses to implement specific methods
public abstract String startEngine();
public abstract String stopEngine();
}
class Car extends Vehicle {
// Concrete class implementing the abstract methods
@Override
public String startEngine() {
return "Car engine started";
}
@Override
public String stopEngine() {
return "Car engine stopped";
}
}
// 4. Inheritance (IS-A Relationship): Creating a hierarchy
class Dog extends Animal {
// Dog IS-A Animal (Inheritance)
private String breed; // Additional attribute
public Dog(String name, String breed) {
super(name, "Dog"); // Calling parent class constructor
this.breed = breed;
}
@Override
public String makeSound() {
// Method Overriding (Polymorphism)
return "Bark!";
}
}
class Cat extends Animal {
// Cat IS-A Animal
private String color;
public Cat(String name, String color) {
super(name, "Cat");
this.color = color;
}
@Override
public String makeSound() {
return "Meow!";
}
}
// 5. Multiple Inheritance (using interfaces in Java)
interface Flying {
// A interface to add flying capability
String fly();
}
class Bird extends Animal implements Flying {
// Bird IS-A Animal and it also CAN Fly
public Bird(String name, String species) {
super(name, species);
}
@Override
public String makeSound() {
return "Chirp!";
}
@Override
public String fly() {
return "I can fly!";
}
}
// 6. Multilevel Inheritance
class Puppy extends Dog {
// Puppy IS-A Dog, which IS-A Animal
private int age;
public Puppy(String name, String breed, int age) {
super(name, breed);
this.age = age;
}
public String isCute() {
return this.name + " is a cute " + this.age + "-month-old puppy!";
}
}
// 7. Hierarchical Inheritance: Multiple child classes from the same base
class Lion extends Animal {
// Lion IS-A Animal
public Lion(String name) {
super(name, "Lion");
}
@Override
public String makeSound() {
return "Roar!";
}
}
// 8. Polymorphism: Method Overriding
class PolymorphismDemo {
public static String animalSound(Animal animal) {
// Function to demonstrate polymorphism
return animal.makeSound();
}
}
// 9. Method Overloading
class MathOperations {
// Demonstrating method overloading
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
}
// 10. Static Methods
class Utility {
// A class with static methods
public static String greet() {
return "Hello, welcome to OOP in Java!";
}
public static String describeClass() {
return "This is Utility class.";
}
}
// 11. Composition (HAS-A Relationship)
class Engine {
// Engine class
public String start() {
return "Engine started";
}
public String stop() {
return "Engine stopped";
}
}
class CarWithEngine {
// Car HAS-A Engine (Composition)
private String model;
private Engine engine; // Composition
public CarWithEngine(String model) {
this.model = model;
this.engine = new Engine();
}
public String startCar() {
return this.model + ": " + this.engine.start();
}
public String stopCar() {
return this.model + ": " + this.engine.stop();
}
}
// Demonstration of OOP concepts
public class Main {
public static void main(String[] args) {
// Creating Objects
Dog dog = new Dog("Buddy", "Golden Retriever");
Cat cat = new Cat("Whiskers", "Black");
Lion lion = new Lion("Simba");
// Demonstrate Polymorphism
System.out.println(PolymorphismDemo.animalSound(dog)); // Output: Bark!
System.out.println(PolymorphismDemo.animalSound(cat)); // Output: Meow!
System.out.println(PolymorphismDemo.animalSound(lion)); // Output: Roar!
// Demonstrate Encapsulation
BankAccount account = new BankAccount("Alice", 1000);
System.out.println(account.deposit(500));
System.out.println(account.withdraw(300));
System.out.println("Balance: " + account.getBalance());
// Demonstrate Abstraction
Car car = new Car();
System.out.println(car.startEngine());
System.out.println(car.stopEngine());
// Demonstrate Composition
CarWithEngine myCar = new CarWithEngine("Tesla Model X");
System.out.println(myCar.startCar());
System.out.println(myCar.stopCar());
// Demonstrate Multiple Inheritance (via interfaces)
Bird bird = new Bird("Eagle", "Bird of Prey");
System.out.println(bird.fly()); // Output: I can fly!
// Demonstrate Static Methods
System.out.println(Utility.greet()); // Output: Hello, welcome to OOP in Java!
System.out.println(Utility.describeClass()); // Output: This is Utility class.
// Demonstrate Multilevel Inheritance
Puppy puppy = new Puppy("Max", "Labrador", 3);
System.out.println(puppy.isCute());
// Demonstrate Method Overloading
MathOperations mathOps = new MathOperations();
System.out.println(mathOps.add(5, 10)); // Output: 15
System.out.println(mathOps.add(5, 10, 20)); // Output: 35
}
}Summary of OOP Concepts Demonstrated:
Key OOP Concepts Covered:
🔹 Python Code with Detailed Comments:
Last updated