OOPs Concept’s in Java

Inheritance

  1. Single inheritance
  2. Multi-level inheritance
  3. Hierarchical inheritance
  4. Multiple inheritances
  5. Hybrid inheritance

Single inheritance

public class Animal{       public void voice(){
System.out.println("Hello I am animal");
}
}
public class Cow extends Animal{
public void sound(){
System.out.println("Hello I am Cow");
}
}
public class Main{
public static void main(String[] args){
Cow obj = new Cow();
obj.sound();
obj.voice();
}}

Multi-level inheritance

class Animal{
public void voice(){
System.out.println("Hello I am animal");
}}
class Calf extends Cow{
public void soundCalf(){
System.out.println("Hello I am Calf");
}
}
public class Main{
public static void main(String[] args){
Calf obj = new Calf();
obj.soundCalf();
obj.sound();
obj.voice();
}}

Hierarchical inheritance

public class Animal{
public void voice(){
System.out.println("Hello I am animal");
}
}
public class Cow extends Animal{
public void soundCow(){
System.out.println("Hello I am Cow");
}
}
public class Dog extends Animal{
public void soundDog() {
System.out.println("Hello I am Dog");
}
}
public class Main{
public static void main(String[] args){
Dog obj1 = new Dog();
Cow obj2 = new Cow();
obj1.voice();
obj2.voice();
}}

Multiple inheritance

Polymorphism

  1. Compile-time polymorphism(method overloading)
  2. Run-time polymorphism(method overriding)

Compile-time polymorphism

public class Calc{
public void add(int a, int b){
System.out.println(a + b);
}
public void add(int a, int b, int c){
System.out.println(a + b + c);
}}
public class Main{
public static void main(String[] args){
Calc obj1 = new Calc();
Calc obj2 = new Calc();
obj1.add(2, 3);
obj2.add(2, 3, 4);
}}

Run-time polymorphism

public class Parent{
public void voice(){
System.out.println("Hello I am Parent");
}
}
public class Child extends Parent{
@Override
public void voice(){
System.out.println("Hello I am Child");
}
}
public class Main{
public static void main(String[] args){
Child obj = new Child();
obj.voice();
}}

Encapsulation

public class Person{
private String name;
private int age ;
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}}
public class Main{
public static void main(String[] args){
Person obj = new Person();
obj.setName("Harish");
obj.setAge(27);
System.out.println(obj.getName());
System.out.println(obj.getAge());
}}

Abstraction

abstract class Animal{
abstract void voice();
}
class Cow extends Animal{
void voice(){
System.out.println("Hello I am Cow");
}}
public class Main{
public static void main(String[] args){
Animal obj = new Cow();
obj.voice();
}}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The World’s First Middle-Out Compression for Time-series Data — Part 2

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Technical solutions against transaction surges

SELinux Policy for OpenShift Containers

El Correo Libre Issue 40

How to build your home infrastructure for data collection and visualization and be the real owner…

PIXIU token emission rate

Old Town Road With WordPress

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
Achalgupta

Achalgupta

More from Medium

Java I/O Byte Stream Implementation

Collections in Java

Overriding .equals method for inheriting classes in Java

Hackerrank — Abstract Classes — Polymorphism Solution — C++