[go: up one dir, main page]

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

OOP_Concepts_Java_Programs

The document provides Java programming examples that illustrate key Object-Oriented Programming (OOP) concepts including classes and objects, encapsulation, inheritance, polymorphism (overloading and overriding), abstraction (abstract classes and interfaces), constructor overloading, and the static keyword. Each concept is demonstrated with code snippets and a main method to showcase functionality. This serves as a comprehensive guide from basics to intermediate OOP principles in Java.

Uploaded by

saniyachandere12
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 views10 pages

OOP_Concepts_Java_Programs

The document provides Java programming examples that illustrate key Object-Oriented Programming (OOP) concepts including classes and objects, encapsulation, inheritance, polymorphism (overloading and overriding), abstraction (abstract classes and interfaces), constructor overloading, and the static keyword. Each concept is demonstrated with code snippets and a main method to showcase functionality. This serves as a comprehensive guide from basics to intermediate OOP principles in Java.

Uploaded by

saniyachandere12
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/ 10

Java Programs to Demonstrate

OOP Concepts
Basics to Intermediate Examples
Class & Object
• 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
• 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
• 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
• 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
• 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
• 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
• 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
• 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
• 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();
• }
• }

You might also like