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() {
[Link]("This animal eats food.");
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
Main:
Dog dog = new Dog();
[Link](); // Accessing inherited method
[Link](); // Accessing method from Dog class
Features:
- 'super' keyword: Used to call a parent class's methods or constructors.
Example:
class Parent {
void show() {
[Link]("Parent class method");
class Child extends Parent {
void show() {
[Link](); // Calls Parent's show() method
[Link]("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() {
[Link]("Some sound");
class Dog extends Animal {
@Override
void sound() {
[Link]("Bark");
Main:
Animal animal = new Dog();
[Link](); // Outputs: Bark
3. Abstraction
Abstraction hides implementation details while exposing the essential
functionalities.
Abstract Class:
abstract class Shape {
abstract void draw();
void info() {
[Link]("This is a shape.");
}
}
class Circle extends Shape {
void draw() {
[Link]("Drawing a circle.");
Interface:
interface Flyable {
void fly();
class Bird implements Flyable {
public void fly() {
[Link]("Bird is flying.");
Default and Static Methods in Interfaces (Java 8):
interface Vehicle {
default void start() {
[Link]("Vehicle is starting.");
static void stop() {
[Link]("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) {
[Link] = name;
5. Inner Classes
Static Nested Class:
class Outer {
static class Nested {
void display() {
[Link]("Static nested class.");
Non-Static Inner Class:
class Outer {
class Inner {
void display() {
[Link]("Inner class.");
Anonymous Inner Class:
interface Greeting {
void sayHello();
Greeting greet = new Greeting() {
public void sayHello() {
[Link]("Hello!");
};
[Link]();
6. Generics
Generics allow you to write type-safe code.
class Box<T> {
private T item;
public void set(T item) {
[Link] = item;
public T get() {
return item;
}
Main:
Box<String> box = new Box<>();
[Link]("Hello");
[Link]([Link]()); // 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) {
[Link]([Link]());
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 ([Link]("Circle")) return new Circle();
else if ([Link]("Rectangle")) return new Rectangle();
return null;