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

OOP Java Code Examples

The document provides examples of key Object-Oriented Programming (OOP) concepts in Java, including classes and objects, encapsulation, inheritance, polymorphism (both overriding and overloading), abstraction, and interfaces. Each concept is illustrated with code snippets demonstrating their implementation and functionality. The examples cover various scenarios such as creating a Car class, managing a BankAccount, and using interfaces for multiple behaviors.

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 Java Code Examples

The document provides examples of key Object-Oriented Programming (OOP) concepts in Java, including classes and objects, encapsulation, inheritance, polymorphism (both overriding and overloading), abstraction, and interfaces. Each concept is illustrated with code snippets demonstrating their implementation and functionality. The examples cover various scenarios such as creating a Car class, managing a BankAccount, and using interfaces for multiple behaviors.

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/ 3

1.

Class & Object


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
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
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
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
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
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
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