Object-Oriented Programming in java

Object-oriented programming is a programming paradigm or methodology. it is an approach to solve a problem using the programming language. The main features of oop’s to provides code Reusability and Securities to our program.

The six pillars of oop’s are

Class

Object

class car
{
String color="red";
void show()
{
System.out.println("Audi car colour is red");
}
}
class Main
{
public static void main(String[] args)
{
car Audi =new car();
Audi.show();
}
}

Inheritance

class A //single inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class Main
{
public static void main(String[] args)
{
B obj =new B();
obj.showA();
obj.showB();
}
}
class A //multilevel inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class c extends B
{
void showC()
{
System.out.println("class C method is call");
}
}
class Main
{
public static void main(String[] args)
{
c obj =new c();
obj.showA();
obj.showB();
obj.showC();
}
}
class A//hierarchical inheritance
{

void showA()
{
System.out.println("class A method is call");
}
}
class B extends A
{
void showB()
{
System.out.println("class B method is call");
}
}
class c extends A
{
void showC()
{
System.out.println("class C method is call");
}
}
class Main
{
public static void main(String[] args)
{
B obj1 =new B();
obj1.showA();
obj1.showB();

c obj2 =new c();
obj2.showA();
obj2.showC();
}
}

Encapsulation

class Employee
{
private int empid;
public void setEmpid(int eid)
{
empid=eid;
}
public int getEmpid()
{
return empid;
}
}
class Company
{
public static void main(String[] args)
{
Employee e=new Employee();
e.setEmpid(101);
System.out.println(e.getEmpid());
}
}

Abstraction

abstract class Vehicle //abstraction using abstract class and method
{
abstract void start();
}
class Car extends Vehicle
{
void start()
{
System.out.println("car starts with key");
}
}
class Scooter extends Vechicle
{
void start()
{
System.out.prinbtln("scooter start with kick");
}
public static void main(String[] args)
{
car c=new car();
c.start();

Scooter s=new Scooter();
s.start();
}
}
interface I1 // abstraction using interface
{
void show();
}
class Test implements I1
{
public void show()
{
System.out.prinbtln("1");
}
public static void main(String[] args)
{
Test t =new Test();
t.show();
}
}

polymorphism

class Test \\method overloading
{
void show()
{
System.out.println("1");
}
void show(int a)
{
System.out.println("2");
}
public static void main(String[] args)
{
Test t =new Test();
t.show();
t.show(10);
}
}
class Test//method overriding
{
void show(String a, int b)
{
System.out.println("1");
}
}
class Xyz extends Test
{
void show(String a, int b)
{
System.out.println("2");
}
public static void main(String[] args)
{
Test t=new Test();
t.show("test",10);

Xyz x =new Xyz();
x.show("test1",20);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store