OOP Concepts

Object-oriented concepts in java

Triangle  trn;
class Triangle{
private int a;
private int b;
private int c;
//Constructor without argument
public Triangle(){
this(0,0,0); //This calls 2nd constructor
}
//2nd constructor
public Triangle(int a, int b, int c){
this.a=a;
this.b=b;
this.c=c;
}
}
//Initialistion
Triangle firstTriangle = new Triangle();
Triangle secondtriangle = new Triangle(2,7,9);

Encapsulation

public class Player{
private String name;
private int points=100; //Initial points=500

public Player(String name, int points){
this.name=name;
if(point>0 && point<=100){
this.points=points;
}
}
public void losePoint(int lostPoint){
this.points=this.points-lostPoint;
if(this.points<=0){
System.out.println("Knocked out");
}
}
public int getPoints(){
return this.points;
}
}

Abstraction

public abstract class Animal{
private String name;
public Animal(String name){
this.name=name;
}
public abstract void eat();
public String getName(){
return this.name;
}
}
public class Cow extends Animal{
public Cow(String name){
super(name);
}
@override
public void eat(){
System.out.println(getName()+" is eating grass");
}
}
public class Lion extends Animal{
public Lion(String name){
super(name);
}
@override
public void eat(){
System.out.println(getName()+" is eating meat);
}
}

Abstraction by Interface

public abstract class Animal{
private String name;
public Animal(String name){
this.name=name;
}
public abstract void eat();
public String getName(){
return this.name;
}
}
public interface EatGrass{
void eatGrass();
}
public abstract class Herbivore extends Animal implements EatGrass {
public Herbivore(String name){
super(name);
}

@override
public void eatGrass(){
System.out.println(getName()+" is eating grass");
}
}
public class Cow extends Herbivore{
public Cow(String name){
super(name);
}
//Herbivore class implements interface EatGrass we don't need override method here
}
public class Deer extends Herbivore{
public Deer(String name){
super(name);
}
}

Inheritance

public class Animal{
private String name;
private int weight;
private int legs;
publiuc Animal(String name, int weight, int legs){
this.name=name;
this.weight=weight;
this.legs=legs;
}
public void voice(){
System.out.println(name+"is speaking");
}
}
public class Lion extends Animal{
public Lion(String name , int weight, int legs){
super(name, weight, legs);
}
@override
public void voice(){
System.out.println(name+" is roaring");
}
}

Polymorphism

public class Dog{
public void bark(){
system.out.out("woof");
}
public void bark(int number){
for(int i=0;i<number;i++){
System.out.println("woof);
}
}
}

--

--

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.

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

Hi, I am Aditya. I studied at IIT BHU Varanasi , India. I am a Java developer. I partricipate in competitive programming.