Skip to content

TheHonored-Ones/OOP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Key OOP Concepts

Do everything in a seperate class!! Goodluck πŸ’˜

1. Classes and Objects

Class:

A blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the objects created from the class will have.

Object:

An instance of a class. When you create an object, you are instantiating the class, making it a real, usable entity.

public class Car {
    String model;
    int year;

    public void startEngine() {
        System.out.println("The engine is starting.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of Car
        Car myCar = new Car();
        myCar.model = "Toyota";
        myCar.year = 2022;
        myCar.startEngine();
    }
}

2. Encapsulation

Encapsulation is about bundling the data (variables) and methods that operate on the data within one unit (a class). It also involves restricting direct access to some of the object's components, which is done through access modifiers (private, public, etc.). Encapsulation helps protect data from unintended modifications and enforces controlled access through getter and setter methods.Encapsulation is implemented using access modifiers and getter/setter methods. Private fields and methods can only be accessed or modified within the class, while public methods allow controlled access.

public class Person {
    private String name; // Private field
    private int age;     // Private field
}

Here, the variables cannot be accessed directly from outside the Person class. Since the variables are private, we need methods to get and update their values safely.

public class Person {
    private String name;
    private int age;

    // Getter for name
    public String getName() {
        return name;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for age
    public int getAge() {
        return age;
    }

    // Setter for age
    public void setAge(int age) {
        if (age > 0) {  // Ensuring only valid ages are set
            this.age = age;
        } else {
            System.out.println("Invalid age!");
        }
    }
}

Now, values can be retrieved or modified only through these controlled methods.

public class Person {
    private String name;  // Private variable
    private int age;

    // Getter method
    public String getName() {
        return name;
    }

    // Setter method
    public void setName(String name) {
        this.name = name;
    }
}

3. Inheritance

Inheritance allows one class (child class) to inherit the properties and behaviors of another class (parent class). This allows you to create a new class based on an existing class, which promotes code reuse and avoids duplication.

public class Animal {
    public void speak() {
        System.out.println("Animal speaks");
    }
}

public class Dog extends Animal {
    public void speak() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.speak();  // Output: Bark
    }
}

4. Polymorphism

Polymorphism means "many shapes" and it allows objects of different classes to be treated as objects of a common superclass. It also allows methods to have different behaviors depending on the object that calls them (method overriding).

In method overriding, a subclass provides a specific implementation of a method already defined in its superclass.

public class Animal {
    public void sound() {
        System.out.println("Some animal sound");
    }
}

public class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myCat = new Cat();  // Animal reference but Cat object
        
        myAnimal.sound();  // Output: Some animal sound
        myCat.sound();     // Output: Meow
    }
}

Abstraction

Abstraction is the concept of hiding the complex implementation details and exposing only the necessary features of an object. This allows users to interact with objects without needing to understand their internal workings.

In Java, abstraction can be achieved using abstract classes and interfaces.

abstract class Animal {
    abstract void sound();  // Abstract method
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Woof");
    }
}

Summary

Classes and Objects:

Define the structure and behavior of your program's entities.

Encapsulation:

Control access to data and behavior to protect integrity.

Inheritance :

Reuse code by allowing one class to inherit from another.

Polymorphism:

Use the same method or object in different ways (method overriding).

Abstraction:

Hide unnecessary details and expose only the essential parts of an object.

Exercies: Mastering OOP in Java

Car Factory (Classes and Objects)

Create a class Car with these attributes:

String brand String model int year πŸ‘‰ Inside Car, create a method displayInfo() that prints the car details. πŸ‘‰ In main(), create 3 Car objects and call displayInfo() on each. Example

Car car1 = new Car("Toyota", "Corolla", 2020);
car1.displayInfo();  
// Output: Toyota Corolla (2020)

Bank Account (Encapsulation - Getters & Setters

Create a class BankAccount with:

private double balance πŸ‘‰ Add methods:

deposit(double amount) βœ… Adds money withdraw(double amount) βœ… Deducts money if sufficient balance getBalance() βœ… Returns balance πŸ“Œ Example:

BankAccount myAccount = new BankAccount();
myAccount.deposit(1000);
myAccount.withdraw(200);
System.out.println(myAccount.getBalance());
// Output: 800.0

Student Management(Array of Objects)

Create a class Student with:

String name double grade πŸ‘‰ In main(), create an array of 3 Student objects, assign values, and print their details.

πŸ“Œ Example:

Student[] students = {
    new Student("John", 85.5),
    new Student("Emily", 92.0),
    new Student("Mark", 78.5)
};

Sports Team (Inheritance - Extending a Class)

πŸ‘‰ Create a base class Player with:

String name int age πŸ‘‰ Create a subclass BasketballPlayer that adds:

int pointsScored πŸ‘‰ Override the introduce() method to include points scored.

πŸ“Œ Example:

BasketballPlayer player = new BasketballPlayer("LeBron", 36, 35000);
player.introduce();
// Output: I am LeBron, 36 years old, and I have scored 35000 points!

Superheroes (Polymorphism - Method Overriding)

πŸ‘‰ Create a base class Hero with a method attack(). πŸ‘‰ Create 2 subclasses Superman and Batman, each with their own attack() method.

πŸ“Œ Example:

Hero hero1 = new Superman();
Hero hero2 = new Batman();
hero1.attack();  
// Output: Superman uses Laser Eyes!  
hero2.attack();  
// Output: Batman throws Batarangs!  

Uber System (Interfaces)

πŸ‘‰ Create an interface Vehicle with a method startEngine(). πŸ‘‰ Implement Car and Bike classes that override startEngine().

πŸ“Œ Example:

Vehicle car = new Car();
Vehicle bike = new Bike();
car.startEngine();  
// Output: Car engine started!  
bike.startEngine();  
// Output: Bike engine started!  

Bonus Challenge πŸ†: Zoo Simulator

Create a base class Animal with String name and makeSound(). πŸ‘‰ Create subclasses Lion, Dog, and Bird that override makeSound(). πŸ‘‰ Store multiple Animal objects in an array and call makeSound() for each!

πŸ“Œ Example:

Animal[] zoo = {new Lion(), new Dog(), new Bird()};
for (Animal a : zoo) {
    a.makeSound();
}
// Output:
// Roar! 🦁
// Woof! 🐢
// Chirp! 🐦

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages