Do everything in a seperate class!! Goodluck π
A blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the objects created from the class will have.
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();
}
}
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;
}
}
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
}
}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 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");
}
}Define the structure and behavior of your program's entities.
Control access to data and behavior to protect integrity.
Reuse code by allowing one class to inherit from another.
Use the same method or object in different ways (method overriding).
Hide unnecessary details and expose only the essential parts of an object.
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)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.0Create 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)
};π 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!π 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! π 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! 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! π¦