Object Oriented Features Of Java

What is OOPs?

OOPs concept brings this data and behavior in a single place called “class” and we can create any number of objects to represent different state for each object.

Object-oriented programming System(OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Object oriented programming brings together data and its behaviour(methods) in a single location(object) makes it easier to understand how a program works. We will cover each and every feature of OOPs in detail so that you won’t face any difficultly understanding OOPs Concepts.

Object Oriented Features in Java

Features of OOPs:

  1. Classes
  2. Objects
  3. Data Abstraction
  4. Encapsulation
  5. Inheritance
  6. Polymorphism

What is Class?

Class represents a real world entity which acts as a blueprint for all the objects.

We can create as many objects as we need using Class.

Example:
We create a class for “ Student ” entity as below

Student.java

Class Student{
String id;
int age;
String course;
void enroll(){
System.out.println(“Student enrolled”);
}
}

Above definition of class contains 3 fields id,age and course and also it contains behavior or a method called “ enroll ”.

What is an Object?

Object Oriented Programming system(OOPS) is designed based on the concept of “Object”. It contains both variables (used for holding the data) and methods(used for defining the behaviors).

We can create any number of objects using this class and all those objects will get the same fields and behavior.

Student s1 = new Student();

Now we have created 3 objects s1,s2 and s3 for the same class “ Student ”.We can create as many objects as required in the same way.

We can set the value for each field of an object as below,

s1.id=123;
s2.age=18;
s3.course=”computers”;

What is Abstraction?

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.

For example, when you login to your bank account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to server, how it gets verified is all abstracted away from the you.

We can achieve “ abstraction ” in Java using 2 ways

1. Abstract class

2. Interface

1. Abstract Class

  • Abstract class in Java can be created using “ abstract ” keyword.
  • If we make any class as abstract then it can’t be instantiated which means we are not able to create the object of abstract class.
  • Inside Abstract class, we can declare abstract methods as well as concrete methods.
  • So using abstract class, we can achieve 0 to 100 % abstraction.

Example:
Abstract class Phone{
void receiveCall();
Abstract void sendMessage();
}
Anyone who needs to access this functionality has to call the method using the Phone object pointing to its subclass.

2. Interface

  • Interface is used to achieve pure or complete abstraction.
  • We will have all the methods declared inside Interface as abstract only.
  • So, we call interface as 100% abstraction.

Example:
We can define interface for Car functionality abstraction as below
Interface Car{
public void changeGear( int gearNumber);
public void applyBrakes();
}

Now these functionalities like changing a gear and applying brake are abstracted using this interface.

What is Encapsulation?

  • Encapsulation is the process of binding object state(fields) and behaviors(methods) together in a single entity called “Class”.
  • Since it wraps both fields and methods in a class, it will be secured from the outside access.
  • We can restrict the access to the members of a class using access modifiers such as private,protected and public keywords.
  • When we create a class in Java, it means we are doing encapsulation.
  • Encapsulation helps us to achieve the re-usability of code without compromising the security.

Example:
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());
}
}

 What is the benefit of encapsulation in java programming
Well, at some point of time, if you want to change the implementation details of the class EmployeeCount, you can freely do so without affecting the classes that are using it. For more information learn,

What is Inheritance?

  • One class inherits or acquires the properties of another class.
  • Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it, rest of the features can be inherited from the parent class.
  1. Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.
  2. It allows us to reuse of code, it improves reusability in your java application.
  3. The parent class is called the base class or super class. The child class that extends the base class is called the derived class or sub class or child class.

To inherit a class we use extends keyword. Here class A is child class and class B is parent class.

class A extends B
{
}

Types Of Inheritance:
Single Inheritance: refers to a child and parent class relationship where a class extends the another class.

Multilevel inheritance:  a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C.

Hierarchical inheritance:  where more than one classes extends the same class. For example, class B extends class A and class C extends class A.

What is Polymorphism?

  • It is the concept where an object behaves differently in different situations.
  • Since the object takes multiple forms, it is called Polymorphism.
  • In java, we can achieve it using method overloading and method overriding.
  • There are 2 types of Polymorphism available in Java,

Method overloading

In this case, which method to call will be decided at the compile time itself based on number or type of the parameters. Static/Compile Time polymorphism is an example for method overloading.

Method overriding

In this case, which method to call will be decided at the run time time based on what object is actually pointed by the reference variable.

Visit,

Don't miss out!
Subscribe To Our Newsletter

Learn new things. Get an article everyday.

Invalid email address
Give it a try. You can unsubscribe at any time.

Comments

comments