Complete Guide: Object Oriented Features of Java!

Object-Oriented programming course

What is OOPs?

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

Object-oriented programming training (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 behavior (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?

The class represents a real-world entity that 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”);
}
}

The above definition of the 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 log in 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 the server, how it gets verified is all abstracted away from 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 the “ abstract ” keyword.
  • If we make any class abstract then it can’t be instantiated which means we are not able to create the object of an 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 receive all();
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

  • The 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 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 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 outside access.
  • We can restrict 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 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 in 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,

Start Learning Java Programming

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 ghostwriter diplomarbeit, the rest of the features can be inherited from the parent class.
  1. Inheritance is the 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 code ghostwriter bachelorarbeit, it improves reusability in your java application.
  3. The parent class is called the base class or superclass. The child class that extends the base class is called the derived class or subclass 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 another class.

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

Hierarchical inheritance:  where more than one class 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 ghostwriter agentur, 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 the number or type of the parameters ghostwriter deutschland. Static/Compile Time polymorphism is an example of method overloading.

Method overriding

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

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Our Programs

Do You Want To Boost Your Career?

drop us a message and keep in touch