0% found this document useful (0 votes)
5 views

OOP_Concepts_with_Java_Examples

The document provides an overview of key object-oriented programming concepts in Java, including classes and objects, encapsulation, inheritance, polymorphism (both overriding and overloading), abstraction, and interfaces. Each concept is illustrated with code examples demonstrating their implementation. The document serves as a foundational guide for understanding these essential programming principles.

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP_Concepts_with_Java_Examples

The document provides an overview of key object-oriented programming concepts in Java, including classes and objects, encapsulation, inheritance, polymorphism (both overriding and overloading), abstraction, and interfaces. Each concept is illustrated with code examples demonstrating their implementation. The document serves as a foundational guide for understanding these essential programming principles.

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Class & Object

A class is a blueprint for creating objects. Each object is an instance of a class. For example, a Car class can

have properties like model and year, and methods like displayInfo().
Example: Creating a simple Car class.

class Car {
String model;
int year;

void displayInfo() {
System.out.println("Model: " + model + ", Year: " + year);
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car(); // Object
myCar.model = "Toyota";
myCar.year = 2020;
myCar.displayInfo(); // Output: Model: Toyota, Year: 2020
}
}

2. Encapsulation

Encapsulation is the concept of wrapping data (variables) and code (methods) together as a single unit. It

helps protect data from unauthorized access using access modifiers like private.
Example: Protecting balance in a BankAccount class.

class BankAccount {
private double balance; // private = encapsulated

public void deposit(double amount) {


if (amount > 0) balance += amount;
}

public double getBalance() {


return balance;
}
}

public class Main {


public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(1500);
System.out.println("Balance: " + acc.getBalance());
}
}
3. Inheritance

Inheritance allows one class to inherit the properties and methods of another class. This helps in code reuse

and establishing a relationship between parent and child classes.


Example: Dog inherits from Animal.

class Animal {
void sound() {
System.out.println("Animal makes a 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(); // Inherited method
d.bark(); // Dog's own method
}
}

4. Polymorphism - Overriding

Polymorphism means many forms. In method overriding, a subclass provides a specific implementation of a

method that is already defined in its superclass.


Method Overriding (Runtime Polymorphism)

class Animal {
void sound() {
System.out.println("Generic animal sound");
}
}

class Cat extends Animal {


void sound() {
System.out.println("Meow");
}
}

public class Main {


public static void main(String[] args) {
Animal a = new Cat(); // Polymorphic reference
a.sound(); // Output: Meow
}
}
4. Polymorphism - Overloading

In method overloading, multiple methods have the same name but differ in the type or number of parameters.
Method Overloading (Compile-time Polymorphism)

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(2, 3)); // 5
System.out.println(c.add(2.5, 4.5)); // 7.0
}
}

5. Abstraction

Abstraction means hiding complex implementation details and showing only the necessary features. It is

achieved using abstract classes or interfaces.


Example: Abstract Shape class.

abstract class Shape {


abstract void draw();
}

class Circle extends Shape {


void draw() {
System.out.println("Drawing a circle");
}
}

public class Main {


public static void main(String[] args) {
Shape s = new Circle();
s.draw(); // Output: Drawing a circle
}
}

6. Interface

An interface in Java is a reference type, similar to a class, that can contain only constants, method

signatures, default methods, static methods, and nested types. Interfaces help achieve full abstraction.
Example: Using interface for multiple behaviors.

interface Printable {
void print();
}

class Document implements Printable {


public void print() {
System.out.println("Printing document...");
}
}

public class Main {


public static void main(String[] args) {
Printable p = new Document();
p.print(); // Output: Printing document...
}
}

You might also like