OOPs_Concepts_in_Java
OOPs_Concepts_in_Java
class Car {
String brand = "Toyota";
void honk() {
System.out.println("Beep!");
}
public static void main(String[] args) {
Car myCar = new Car();
System.out.println(myCar.brand);
myCar.honk();
}
}
OOPs Concepts in Java with Examples
class Car {
Car() {
System.out.println("Default Constructor");
}
Car(String brand) {
System.out.println("Car Brand: " + brand);
}
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car("Ford");
}
}
OOPs Concepts in Java with Examples
Pillars of OOPs
Types of Inheritance
// Java supports:
// - Single
// - Multilevel
// - Hierarchical
class A {}
class B extends A {} // Single Inheritance
class C extends B {} // Multilevel Inheritance
OOPs Concepts in Java with Examples
Polymorphism
Friend functions
Static method
class Utility {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(Utility.square(4));
}
}
OOPs Concepts in Java with Examples
Access specifiers
Abstraction vs Encapsulation
class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
OOPs Concepts in Java with Examples
Implementation