How to Implement OOPs Concepts in Java?

 In Technologies
0
233 Views

First, we are going to show you how Object-oriented programming works actually. It brings together data and its behavior (methods) in a single location (object) makes it easier to understand how a program works. We are going to cover every functionality of Oops concept in detail so that you will be able to understand Oops Concepts easily.

Oops Concepts – Table of Contents

  1. What is an Object?
  2. What is a class?
  3. Constructor in Java
  4. Object-Oriented Programming Features
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  5. Abstract Class
  6. Interfaces in Java

What is an Object?

So the first term that strikes the mind is what actually is an Object. An entity that has state and behavior is known as an object e.g. Bike, marker, pen, table, car etc.

For example, Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.

From a programming point of view, an object can be a data structure, a variable or a function. It has a memory location allocated.

What is a Class?

As you have learned about Object now you are going to learn about Class. Below is a brief about the same:

  • A class is a template or blueprint that is used to create objects.
  • Class representation of objects and the sets of operations that can be applied to such objects.
  • A class consists of Data members and methods.

 

Constructor in Java

As you have learned about Object and Class now you are going to learn about Constructor. The constructor is a special type of method which is mainly used to initialize the object.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as a constructor.

Rules for creating java constructor

When you are going to create a constructor, you should keep below points in your mind:

  1. Constructor name must be same as its class name.
  2. Constructor must have no explicit return type.

So constructor looks like this in a java code.

public class MyClass{

//This is the constructor

MyClass(){

 } ..

}

Object-Oriented Programming Features

In this blog, you are going to learn the basics of oops concepts. Object-Oriented Programming is a standard or pattern that provides many concepts such as Abstraction, Encapsulation, Inheritance and Polymorphism.

Abstraction

Moving ahead with the term Abstraction, it is one of the most fundamental concepts of OOPs.

Abstraction is a process where only the functionality will be provided to the user and user will not be aware of the implementation details, or we can say the user will have the information on what the object does actually instead of how it does it.

 

For example, when you are withdrawing the money from ATM, you enter the required details and get money but you are not aware how actually transaction takes place behind or how it gets verified is all abstracted away from you.

Abstraction in Java is achieved by using interface and abstract class in Java.

Encapsulation

As you have gone through with Abstraction now you are going to learn about Encapsulation. Encapsulation simply means binding object state (fields) and behavior (methods) together. If you are creating a class, you are doing encapsulation.

So the main functionality or use of encapsulation is to hide the implementation details from users.

For Example: If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.

class EmployeeCount

{

private int numOfEmployees = 0;

public void setNoOfEmployees (int count)

{

numOfEmployees = count;

}

public double getNoOfEmployees ()

{

return numOfEmployees;

}

}

public class EncapsulationExample

{

public static void main(String args[])

{

EmployeeCount obj = new EmployeeCount ();

obj.setNoOfEmployees(5613);

System.out.println("No Of Employees: "+(int)obj.getNoOfEmployees());

}

}

So what is the benefit of encapsulation in java programming?
As shown in above example: if you want to change the implementation details of the class EmployeeCount, so you can make the change without affecting the classes that are using it.

Inheritance

When one class acquires the properties and functionalities of another class, the process to achieve it is called inheritance.

The class which inherits the properties of other is known as a subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Note: The biggest advantage of Inheritance is that the code in base class need not be rewritten in the child class.

We can use variables and methods of the base class in the child class as well.

class Teacher

{

String designation = "Teacher";

String college = "Beginnersbook";

void does()

{

System.out.println("Teaching");

}

}



public class MathTeacher extends Teacher

{

String mainSubject = "Maths";

public static void main(String args[])

{

MathTeacher obj = new MathTeacher();

System.out.println(obj.college);

System.out.println(obj.designation);

System.out.println(obj.mainSubject);

obj.does();

}

}

Polymorphism

Polymorphism is a feature of object-oriented programming that allows you to perform a single action in different ways.

Let’s take an example of a class Animal that has a method animalSound(), here you cannot give an implementation of this method because you are not aware at this point of time which Animal class would extend Animal class. So, you have to make this method abstract as below:

public abstract class Animal

{

public abstract void animalSound();

}

For Example: we have two Animal classes Dog and Lion and both extends Animal class. We can provide the implementation detail as below.

public class Lion extends Animal

{

public void animalSound()

{

System.out.println("Roar");

}

}

public class Dog extends Animal

{

@Override

public void animalSound()

{

System.out.println("Woof");

}

}

Abstract Class

A class is called Abstract Class if it is having the Abstract keyword in its declaration. For an Abstract class, it is not necessary to contain abstract methods, i.e., methods without body ( public void get(); )

But, if a class has at least one abstract method, then the class must be declared an abstract class and if a class is declared abstract, the class cannot be instantiated. If you want to use an abstract class, you have to inherit it from another class, and you will have to provide implementations to the abstract methods in it.

Also, if you are going to inherit an abstract class, you have to provide implementations to all the abstract methods in it.

abstract class Bike

{

abstract void run();

}

class Honda4 extends Bike

{

void run()

{

System.out.println("running safely..");

}

public static void main(String args[])

{

Bike obj = new Honda4();

obj.run();

}

}

Interface

Last but not the least you will learn here about Interface. An interface can also have methods and variables, but the interface cannot contain the implementation of the methods, only the signature (name, parameters and exceptions) of the method will be available in the interface.

Below are some important points about Interface, you should be aware of these:

  1. The interface cannot be instantiated directly.
  2. An interface does not contain any constructors.

All of the methods in an interface are abstract i.e there will be a declaration of the methods only no definition or body method will be there.

Interfaces are implemented by class and interfaces are not extended by a class. An interface can extend multiple interfaces.

interface MyInterface{

public void method1();

public void method2();

}

class Demo implements MyInterface{

public void method1(){

System.out.println("implementation of method1");

}

public void method2()   {

System.out.println("implementation of method2");

}

public static void main(String arg[])   {

MyInterface obj = new Demo();obj.method1();

}

}

I hope you guys understood the Oops concepts that we have discussed above in this blog i.e inheritance, encapsulation, abstraction and polymorphism. These concepts are very important for web application development.

In case, you have any question related to this topic request you to ask your question using the comments section of this “Object Oriented Programming” blog and we will get back to you as soon as possible.

RECOMMENDED POSTS

Start typing and press Enter to search