0% found this document useful (0 votes)
2 views2 pages

OOP Concepts Java Programs

The document provides Java programs that illustrate key Object-Oriented Programming (OOP) concepts: Abstraction, Encapsulation, Inheritance, and Polymorphism. Each concept is demonstrated through a simple class structure and methods, showcasing how they are implemented in Java. The examples include drawing shapes, managing student age, animal behaviors, and vehicle functionalities.
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)
2 views2 pages

OOP Concepts Java Programs

The document provides Java programs that illustrate key Object-Oriented Programming (OOP) concepts: Abstraction, Encapsulation, Inheritance, and Polymorphism. Each concept is demonstrated through a simple class structure and methods, showcasing how they are implemented in Java. The examples include drawing shapes, managing student age, animal behaviors, and vehicle functionalities.
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/ 2

Java Programs Demonstrating OOP Concepts

Abstraction
abstract class Shape {
abstract void draw(); // abstract method
}

class Circle extends Shape {


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

public static void main(String[] args) {


Shape s = new Circle();
s.draw();
}
}

Encapsulation
class Student {
private int age; // private variable

public void setAge(int a) {


age = a;
}

public int getAge() {


return age;
}

public static void main(String[] args) {


Student s = new Student();
s.setAge(20);
System.out.println(s.getAge());
}
}

Inheritance
class Animal {
void eat() {
System.out.println("Animal eats");
}
}

class Cat extends Animal {


public static void main(String[] args) {
Cat c = new Cat();
c.eat(); // Inherited method
}
}

Polymorphism
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}

class Bike extends Vehicle {


void run() {
System.out.println("Bike is running");
}

public static void main(String[] args) {


Vehicle v = new Bike(); // Upcasting
v.run(); // Runtime Polymorphism
}
}

You might also like