OOP Concepts

Object-oriented concepts in java

Aditya Kumar
5 min readAug 22, 2021

Following is a brief description of different OOP concepts.

  • Objects: Objects represent a real-world entity that has properties and methods. An object can also contain some other object. For example, an library management system can have objects like books, students, etc.
  • Class: Class is a blueprint or we can say prototype of objects. This decides the nature and behavior of the object.
  • Encapsulation: Encapsulation is a way of binding data and methods that operate on that data to a single unit so that data is not accessed from outside our class. We provide methods to access those data through methods.
  • Abstraction: Abstraction is just an extension of encapsulation. While encapsulation deals with data security , abstraction is used to expose only relevant methods to end-user by hiding the internal implementation. In java, we have a concept of abstract class and interfaces where we declare methods. The class who implement those abstract class and interface override the methods.
  • Inheritance: Inheritance is a mechanism by which a child object acquires all properties and behavior from the parent object. Parent and child class has Is-a relationship between them. For ex. a dog is an animal, dog class inherit all properties and behavior of animal class.
  • Polymorphism: Polymorphism means the ability to take different forms. An object can take a different form based on the way the object is called. In case of method overloading an object method can exist in different form in a class or its subclass which is also called compile-time polymorphism. In inheritance a method overriding way to achieve polymorphism in which child class implements the method in a different way than parent class which is also called runtime polymorphism.

Classes and Objects

Class is the blueprint of the object. Class represent all common properties and methods which will be available in all instance of this class. A class has the following components.

1.Access modifiers: Access modifiers specify the availability or accessibility of class, methods, constructor, or fields. There are 4 types of access modifiers in java. which are as following.

  1. Private: Access level is only within the class.
  2. Default: Access level is only within the package.
  3. Protected: Access level is only within the package. By child class, it can be accessed from another package.
  4. Public: This can be accessed from any package.

2.Class Keyword: In Java class is created using the class keyword.

3.ClassName: Classes are named using the PascalNaming convention in which the first letter of each word is in upper case. Methods are named as per camelNaming convention in which the first letter of the first word is lower rest as per pascal convention

4.Class body: All content of the class is inside the curly bracket {}.

Object: Object is an instance of the class. It has state (fields) and behavior(methods).

Instantiation(Declaration) of Object: It is declared like a primitive variable. Which will be useful to refer to the object of type, here Triangle.

Triangle  trn;

Initialization of object: Object is initialized by a new keyword. By default constructor without argument is used by the java compiler to create an object. If we initialize through parametrized constructors we have to create all types of constructors.

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

Encapsulation is a mechanism of hiding data that can not be manipulated by the end-user. Let us design a game. The game starts with a default point of 100. We can set a point while the game start. If a player collides with an obstacle he losses point. If energy is zero he/she is knocked out.

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;
}
}

In the above example points value is protected. No one can put more than 100 points. We have not provided any method to alter the point it can be initialized while the start of the game with the constructor.

Abstraction

Abstraction is a way to hide the internal implementation of any function/method. For example, when we call objects we call them by method name we don’t know how this method is implemented. In java, abstraction is achieved in two ways

  1. Abstract class
  2. Interface

Abstraction by abstract class

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);
}
}

In the above example, the Animal class has the abstract method eat() which is overridden by Cow and Lion differently.

Abstraction by Interface

We use an interface when more than one class has the same functionality. So instead of overriding everywhere we just override at one place.

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

Inheritance is the mechanism by virtue of which the child class inherits from the parent class. Types of inheritance.

  1. Single inheritance: When children inherit from a single parent.
  2. Multiple inheritances: When a child inherits from more than two-parent
  3. Multi-Level inheritance: When a child has grandparents in a linear fashion
  4. Hierarchical inheritance: When two children inherit from the same parent.
  5. Hybrid inheritance: when inheritance is a mix of more than one above type.
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");
}
}

In the above example void method inherit from the parent and implement in different ways.

Polymorphism

Polymorphism is the property of an object to exist in two or more forms. In OOP polymorphism can be achieved via overriding or overloading. An overriding method with the same name can exist in different forms in parent and child. We call it run time polymorphism because at runtime it is decided by the compiler after looking at the type of object. In case of overloading methods in the same class or subclass have the same name but different arguments.

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);
}
}
}

In the above example, method bark exists in two forms.

Mention

Created By

@AdityaKumar94

Gitlab

--

--

Aditya Kumar
Aditya Kumar

Written by Aditya Kumar

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