Advanced OOP Concepts in Java (OOP II)
1. Inheritance
Inheritance allows one class to acquire the properties and methods of another
class.
Key Terms:
- Superclass (Parent): The class being inherited from.
- Subclass (Child): The class that inherits.
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
Main:
Dog dog = new Dog();
dog.eat(); // Accessing inherited method
dog.bark(); // Accessing method from Dog class
Features:
- 'super' keyword: Used to call a parent class's methods or constructors.
Example:
class Parent {
void show() {
System.out.println("Parent class method");
class Child extends Parent {
void show() {
super.show(); // Calls Parent's show() method
System.out.println("Child class method");
2. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class.
Compile-Time Polymorphism (Method Overloading):
class Calculator {
int add(int a, int b) {
return a + b;
double add(double a, double b) {
return a + b;
}
Runtime Polymorphism (Method Overriding):
class Animal {
void sound() {
System.out.println("Some sound");
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
Main:
Animal animal = new Dog();
animal.sound(); // Outputs: Bark
3. Abstraction
Abstraction hides implementation details while exposing the essential
functionalities.
Abstract Class:
abstract class Shape {
abstract void draw();
void info() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle.");
Interface:
interface Flyable {
void fly();
class Bird implements Flyable {
public void fly() {
System.out.println("Bird is flying.");
Default and Static Methods in Interfaces (Java 8):
interface Vehicle {
default void start() {
System.out.println("Vehicle is starting.");
static void stop() {
System.out.println("Vehicle stopped.");
}
4. Encapsulation
Encapsulation ensures that the internal state of an object is protected from
unintended changes.
Example:
class Person {
private String name;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
5. Inner Classes
Static Nested Class:
class Outer {
static class Nested {
void display() {
System.out.println("Static nested class.");
Non-Static Inner Class:
class Outer {
class Inner {
void display() {
System.out.println("Inner class.");
Anonymous Inner Class:
interface Greeting {
void sayHello();
Greeting greet = new Greeting() {
public void sayHello() {
System.out.println("Hello!");
};
greet.sayHello();
6. Generics
Generics allow you to write type-safe code.
class Box<T> {
private T item;
public void set(T item) {
this.item = item;
public T get() {
return item;
}
Main:
Box<String> box = new Box<>();
box.set("Hello");
System.out.println(box.get()); // Outputs: Hello
7. Exception Handling in OOP
Custom Exceptions:
class CustomException extends Exception {
CustomException(String message) {
super(message);
Main:
try {
throw new CustomException("This is a custom exception.");
} catch (CustomException e) {
System.out.println(e.getMessage());
8. Design Patterns
Singleton Pattern:
Ensures a class has only one instance.
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
return instance;
Factory Pattern:
class ShapeFactory {
static Shape getShape(String type) {
if (type.equals("Circle")) return new Circle();
else if (type.equals("Rectangle")) return new Rectangle();
return null;