Advanced OOP Java Fixed
Advanced OOP Java Fixed
1. Inheritance
Inheritance allows one class to acquire the properties and methods of another
class.
Key Terms:
Example:
class Animal {
void eat() {
void bark() {
Main:
Example:
class Parent {
void show() {
void show() {
2. Polymorphism
class Calculator {
return a + b;
return a + b;
}
Runtime Polymorphism (Method Overriding):
class Animal {
void sound() {
System.out.println("Some sound");
@Override
void sound() {
System.out.println("Bark");
Main:
3. Abstraction
functionalities.
Abstract Class:
void info() {
System.out.println("This is a shape.");
}
}
void draw() {
System.out.println("Drawing a circle.");
Interface:
interface Flyable {
void fly();
System.out.println("Bird is flying.");
interface Vehicle {
System.out.println("Vehicle is starting.");
System.out.println("Vehicle stopped.");
}
4. Encapsulation
unintended changes.
Example:
class Person {
return name;
this.name = name;
5. Inner Classes
class Outer {
void display() {
class Outer {
class Inner {
void display() {
System.out.println("Inner class.");
interface Greeting {
void sayHello();
System.out.println("Hello!");
};
greet.sayHello();
6. Generics
class Box<T> {
private T item;
this.item = item;
public T get() {
return item;
}
Main:
box.set("Hello");
Custom Exceptions:
CustomException(String message) {
super(message);
Main:
try {
} catch (CustomException e) {
System.out.println(e.getMessage());
8. Design Patterns
Singleton Pattern:
class Singleton {
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
return instance;
Factory Pattern:
class ShapeFactory {
return null;