OOP Concepts Java Explained
OOP Concepts Java Explained
Examples
Explanation and Code Demonstration
Class & Object – Explanation
• A class is a blueprint for creating objects. An
object is an instance of a class. It contains
properties (variables) and behaviors
(methods).
Class & Object – Code Example
• class Car {
• String color = "Red";
• void display() {
• System.out.println("Car color is: " + color);
• }
• }
• public class Main {
• public static void main(String[] args) {
• Car myCar = new Car();
• myCar.display();
• }
• }
Encapsulation – Explanation
• Encapsulation binds data and code together
and restricts access to internal state using
access modifiers. This protects the object’s
state.
Encapsulation – Code Example
• class Student {
• private String name;
• public void setName(String n) { name = n; }
• public String getName() { return name; }
• }
• public class Main {
• public static void main(String[] args) {
• Student s = new Student();
• s.setName("Amit");
• System.out.println(s.getName());
• }
• }
Inheritance – Explanation
• Inheritance allows a class (child) to inherit
properties and behavior from another class
(parent), promoting reusability.
Inheritance – Code Example
• class Animal {
• void sound() { System.out.println("Animal sound"); }
• }
• class Dog extends Animal {
• void bark() { System.out.println("Dog barks"); }
• }
• public class Main {
• public static void main(String[] args) {
• Dog d = new Dog();
• d.sound();
• d.bark();
• }
• }
Polymorphism – Overloading –
Explanation
• Method overloading allows multiple methods
with the same name but different parameters.
It's resolved at compile-time.
Polymorphism – Overloading – Code
Example
• class Calculator {
• int add(int a, int b) { return a + b; }
• double add(double a, double b) { return a + b; }
• }
• public class Main {
• public static void main(String[] args) {
• Calculator c = new Calculator();
• System.out.println(c.add(5, 10));
• System.out.println(c.add(3.2, 4.8));
• }
• }
Polymorphism – Overriding –
Explanation
• Method overriding allows a subclass to
provide a specific implementation of a method
already defined in its superclass.
Polymorphism – Overriding – Code
Example
• class Vehicle {
• void start() { System.out.println("Vehicle starts"); }
• }
• class Bike extends Vehicle {
• void start() { System.out.println("Bike starts"); }
• }
• public class Main {
• public static void main(String[] args) {
• Vehicle v = new Bike();
• v.start();
• }
• }
Abstraction – Abstract Class –
Explanation
• Abstraction hides implementation details and
only shows essential features. Abstract classes
can't be instantiated and may contain abstract
methods.
Abstraction – Abstract Class – Code
Example
• abstract class Shape {
• abstract void draw();
• }
• class Circle extends Shape {
• void draw() { System.out.println("Drawing Circle"); }
• }
• public class Main {
• public static void main(String[] args) {
• Shape s = new Circle();
• s.draw();
• }
• }
Abstraction – Interface – Explanation
• Interfaces define contracts. A class that
implements an interface must define all its
methods. Useful for achieving full abstraction.
Abstraction – Interface – Code Example
• interface Animal {
• void makeSound();
• }
• class Cat implements Animal {
• public void makeSound() {
• System.out.println("Cat meows");
• }
• }
• public class Main {
• public static void main(String[] args) {
• Animal a = new Cat();
• a.makeSound();
• }
• }
Constructor Overloading – Explanation
• Constructor overloading allows multiple
constructors in the same class with different
parameter lists to initialize objects differently.
Constructor Overloading – Code
Example
• class Employee {
• String name;
• int age;
• Employee(String n) { name = n; }
• Employee(String n, int a) {
• name = n;
• age = a;
• }
• void show() {
• System.out.println(name + " - " + age);
• }
• }
• public class Main {
• public static void main(String[] args) {
• Employee e1 = new Employee("Ravi");
• Employee e2 = new Employee("Sneha", 25);
• e1.show();
• e2.show();
• }
• }
Static Keyword – Explanation
• Static members belong to the class rather than
any instance. Useful for constants, counters, or
utility methods shared among all objects.
Static Keyword – Code Example
• class Counter {
• static int count = 0;
• Counter() {
• count++;
• System.out.println("Object count: " + count);
• }
• static void showTotal() {
• System.out.println("Total objects created: " + count);
• }
• }
• public class Main {
• public static void main(String[] args) {
• new Counter();
• new Counter();
• Counter.showTotal();
• }
• }