Object-Oriented Programming
Duration: 7.0 min
Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm based on objects and classes. It's the foundation of Java and enables you to write modular, reusable, and maintainable code. OOP principles include encapsulation, inheritance, polymorphism, and abstraction. Understanding OOP is crucial for professional Java development and building large-scale applications.
public class Car {
private String brand;
public Car(String brand) { this.brand = brand; }
public void display() { System.out.println("Brand: " + brand); }
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota");
car.display();
}
}Brand: Toyotaclass Animal {
void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking"); }
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}Eating
Barkingclass Shape {
void draw() { System.out.println("Shape"); }
}
class Circle extends Shape {
@Override void draw() { System.out.println("Circle"); }
}
class Rectangle extends Shape {
@Override void draw() { System.out.println("Rectangle"); }
}Polymorphism examplepublic class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}Encapsulation examplepublic class Student {
private String name;
public Student() { this.name = "Unknown"; }
public Student(String name) { this.name = name; }
}Constructor examplepublic class Counter {
static int count = 0;
Counter() { count++; }
static void display() { System.out.println("Count: " + count); }
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
Counter.display();
}
}Count: 2interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Woof"); }
}
class Cat implements Animal {
public void sound() { System.out.println("Meow"}; }
}Interface exampleabstract class Vehicle {
abstract void start();
void stop() { System.out.println("Stopped"); }
}
class Car extends Vehicle {
void start() { System.out.println("Car started"); }
}Abstract class examplepublic class Example {
int x = 10;
void display() {
int x = 20;
System.out.println("Local: " + x);
System.out.println("Instance: " + this.x);
}
}Local: 20
Instance: 10class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
void display() {
super.display();
System.out.println("Child");
}
}Parent
Childpublic class FinalExample {
final int MAX = 100;
final void display() { System.out.println("Final method"); }
}
final class ImmutableClass { }Final keyword example💡 Tip: Best Practice: Use encapsulation to protect your data. Make fields private and provide public getter/setter methods.
💡 Tip: Common Mistake: Forgetting to use @Override annotation when overriding methods. This helps catch errors at compile time.
Learn more: https://docs.oracle.com/javase/tutorial/java/concepts/
❓ What is a class?
❓ What is inheritance?
❓ What does encapsulation do?
❓ What is polymorphism?
❓ What is the purpose of a constructor?
❓ What does the static keyword do?
❓ What is an interface?
❓ What is an abstract class?
❓ What does the this keyword refer to?
❓ What does the super keyword do?