OOP CONCEPTS IN JAVA - EXAMPLES & EXPLANATIONS
1. Class & Object
-----------------
Code:
class Person {
// fields (attributes)
String name;
int age;
// constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
// method
void show() {
[Link]("Name: " + name + ", Age: " + age);
}
}
public class MainClass {
public static void main(String[] args) {
Person p = new Person("Alice", 25); // object creation
[Link]();
}
}
Explanation:
A class is a blueprint. An object is an instance of that blueprint.
2. Encapsulation
----------------
Code:
class Encapsulated {
// private fields - hidden from outside
private int salary;
// public getter
public int getSalary() {
return salary;
}
// public setter with validation
public void setSalary(int salary) {
if (salary > 0) [Link] = salary;
}
}
Explanation:
Encapsulation hides internal data and exposes it through methods (get/set).
3. Inheritance
-------------
Code:
class Animal {
void eat() { [Link]("Eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("Barking..."); }
}
public class InheritDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // inherited
[Link]();
}
}
Explanation:
Inheritance allows a class (subclass) to reuse code from another (superclass).
4. Polymorphism
--------------
a) Compile-time (Method Overloading)
Code:
class MathOp {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
b) Runtime (Method Overriding)
Code:
class Animal2 {
void sound() { [Link]("Some sound"); }
}
class Cat extends Animal2 {
@Override
void sound() { [Link]("Meow"); }
}
public class PolyDemo {
public static void main(String[] args) {
Animal2 a = new Cat(); // reference of parent, object of child
[Link](); // calls Cat's sound() -> runtime polymorphism
}
}
Explanation:
Overloading = same name, different parameters (compile-time).
Overriding = same signature in subclass, called based on actual object
(runtime).
5. Abstraction
--------------
a) Abstract class
Code:
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
double area() { return [Link] * r * r; }
}
b) Interface
Code:
interface Printable {
void print();
}
class Document implements Printable {
public void print() { [Link]("Printing document"); }
}
Explanation:
Abstraction hides complex details. Abstract classes and interfaces define
contracts.
6. Constructors & this / super
------------------------------
Code:
class Parent {
Parent() { [Link]("Parent Constructor"); }
}
class Child extends Parent {
Child() {
super(); // calls parent constructor
[Link]("Child Constructor");
}
}
Explanation:
Constructors initialize objects. 'this' refers to current object; 'super'
calls superclass members.
7. Static & Final
-----------------
Code:
class Utils {
static int count = 0; // shared by all objects
final int id; // constant after initialization
Utils(int id) {
[Link] = id;
count++;
}
}
Explanation:
static -> belongs to class. final -> cannot be changed once set.
8. Association, Aggregation, Composition
---------------------------------------
Code:
class Engine { }
class Car {
Engine engine; // Association (Car has-a Engine)
Car(Engine e) { [Link] = e; }
}
class Department {
List<Student> students; // Aggregation (students can exist outside)
}
class Library {
private List<Book> books = new ArrayList<>(); // Composition (books lifecycle
tied to library)
}
Explanation:
Association: general relationship.
Aggregation: has-a but independent lifecycles.
Composition: strong ownership; parts depend on whole.
---
Tips:
- Keep methods small and focused.
- Prefer interfaces for behavior contracts.
- Use encapsulation to protect fields.
Generated by ChatGPT - concise Java OOP guide with examples.