OOPs Java (Encapsulation , Abstraction , Polymorphism )

 

Object-Oriented Programming Paradigm

Object-oriented programming (OOP) in Java is a programming methodology or paradigm (model)  to design a computer program using classes and objects.


Class

Objects

Abstraction 

Encapsulation

Inheritance

Polymorphism



1. Class:

  • Collection of objects is called class. It is a logical entity.
  • A class is a user-defined data type. It consists of data members and member functions, which can be accessed and used by creating an instance of that class. 
  • It represents the set of properties or methods that are common to all objects of one type. 
  • A class is like a blueprint for an object.  

Ex :-    Batter(Class)  but have some difference shots(Properties)



2. Object: 


Object : Any entity that has state and behavior is known as an object.

Here, state represents properties and behavior represents actions and functionality. 

 Ex : a chair, pen, table, keyboard, bike, etc. It can be physical or logical.

  • An Object can be defined as an instance of a class. 
  • An object contains an address and takes up some space in memory. 
  • Objects can communicate without knowing the details of each other's data or code. 

Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.




1.Encapsulation (Achieve write a class)

Binding (or wrapping) code(Behaviour) and data(characterstics) together into a single unit are known as encapsulation.


Ex :- Eggs , Coconut



2.Abstraction ( Achived by Access Specifiers)

Hiding internal details and showing functionality is known as abstraction.


In Java, we use abstract class and interface to achieve abstraction.


Ex : cloth , Password


3.Inheritance

The technique of constructing a new class by using an existing class functionality is called inheritance.

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.

 It provides code reusability. It is used to achieve runtime polymorphism.


Ex : Bricks Shaper , big brother Cloth



4.Polymorphism

It is Define as Single Name & Multiple behaviours.

If one task is performed in different ways, it is known as polymorphism.

Ex : to convince the customer differently.


In Java, we use method overloading and method overriding to achieve polymorphism.


Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.


Ex : Mother :_ Aai , Maa , Maata.




--------------------------------------------------------------------------------------------------------------------------------------------------------------


OOPS



Encapsulation Abstraction polymorphism Inheritance

                                        Access Specifier

achieved by 1. Abstract Class 1.Compile time(Overloading) 1. Single class

class 2. Interface (pure Abstraction) 2.Run time /dynamic 2.Hierarchical Inheritance - inherite more than 1 class

(Overriding) 3. Multilevel - 1st -> 2st -> 3rd

4. Hybrid Inheritance. -combination all above.



-------------------------------------------------------------------------------------------------------------------------------------------------------------------




Encapsulation 


-The process of binding data and corresponding methods (behavior) together into a single unit is called encapsulation in Java.

-Every Java class is an example of encapsulation because we write everything within the class only that binds

variables and methods together and hides their complexity from other classes.


class{ 

data member 

   +

Mmethod (behaviour)

}


Data Hiding : In the encapsulation technique, we declare fields as private in the class to prevent from other classes by accessing them directly & Anyone cannot access from outside the class. 

The required encapsulated data can be accessed by using public Java getter and setter method.


- Ex : Bank Accout .


implement Encapsulation in Java

1. Declaring the instance variable of the class as private so that it cannot be accessed directly by anyone from outside the class.

2. Provide the public setter and getter methods in the class to set/modify the values of the variable/fields.




------------------------------------




Abstraction

It is a process of hiding complex internal implementation details from the user and providing only necessary functionality to the users.


Ex : ATM Machine


How to achieve Abstraction in Java?

There are two ways to achieve or implement abstraction in java program. They are as follows:

1.Abstract class (0 to 100%)

2.Interface (100%)


Advantage of abstraction in Java

  • It reduces the complexity of viewing the things as it shows only essential information.
  • It allows the flexibility to modify/change implementation logic if needed, as long as the outcome of behavior is not affected by the modification.
  • It helps to increase security of an application or program as only important details are provided to the user.
  • It also helps to avoid code duplication and increases reusability.



----------------------------------

Polymorphism

The word polymorphism is derived from two Greek words: poly and morphs. The word “poly” implies many and “morphs” means forms.



Advantages of Polymorphism in Java


-One advantage of polymorphism is that it allows us to reuse existing code which can make things easier to read and maintain. 

-Another advantage is that, different form of objects can be referred with same type(parent class).



Ex . Water -> ice , liquid ,gas




Types of Polymorphism in Java


Basically, there are two types of polymorphism in java. They are:


1. Static polymorphism / Compile time poly /Early binding

2. Dynamic polymorphism /Runtime poly / Late Binding



1. Static polymorphism / Compile time poly /Early binding


- A polymorphism that exhibited during compilation is called static polymorphism in java. 

In static polymorphism, the behavior of a method is decided at compile-time.


-Hence, Java compiler binds method calls with method definition/body during compilation. 

Therefore, this type of polymorphism is also called compile-time polymorphism in Java.


-Since binding is performed at compile-time, it is also known as early binding. 

Compile-time polymorphism can be achieved/implemented by method overloading in java.


-Method overloading is a mechanism in which a class has multiple methods having the same name but different signatures.

It is one of the ways that Java implements polymorphism.



What is method signature ?

A method signature is part of the method declaration. It's the combination of the method name and the parameter list. Two methods will be called as of same signature if their names as well as number, type and order of their parameters are same.



2. Dynamic Polymorphism / Runtimepoly / Late Binding


-A polymorphism that is exhibited at runtime is called dynamic polymorphism in java. 

In dynamic polymorphism, the behavior of a method is decided at runtime,

therefore, the JVM (Java Virtual Machine) binds the method call with method definition/body

at runtime and invokes the relevant method during runtime when the method is called.


-This happens because objects are created at runtime and the method is called using an object of the class. 

The Java compiler has no awareness of the method to be called on an instance during compilation. 

Therefore, JVM invokes the relevant method during runtime.


-Dynamic or runtime polymorphism can be achieved/implemented in java using method overriding. 

Method overriding is a mechanism where a method of Base class is overridden in the derived class to provide a more specific implementation.


-The signature of method in both base and derived classes is the same but they only differ in their implementation.


Why do we call runtime polymophism as Late binding ?

Since the binding of method that needs to be called is happening at runtime not at compile time, that is why we also call it as late binding.






Definition : 

When a class defines two or more than two methods with same name but different in parameters, we call it method overloading while when a subclass defines a method with same name and same in parameters(number and type of parameters) as in parent class, we call it method overriding.

 

Method Overloading  : 

When a class has more than one method having the same name but with different parameter lists, this feature is called method overloading in Java.


Method overloading rules in Java


Here is the list of rules by which we can implement method overloading in Java. They are as follows:


1. The method name must be the same.


2. Parameters must be different, i.e. each overloaded method must take a unique list of parameter types. We can change the parameters in one of the following three ways:


- Data type of parameters

For example:


void add(int a, int b) // There are two parameters, a and b with data type int.

void add(int a, float b) // Here, two parameters a, and b with types int and float.


- Number of parameters

For example:


void sum(int a, int b) // Two parameters, a and b with data type int.

void sum(int a, int b, int c) // Three parameters a, b, and c with data type int.



- Sequence of data type of parameters

For example:


void sub(int a, double b)

void sub(double a, float a)



Use of Method overloading :


Method overloading is done to reuse the same method name.

It is done to make the program logically more readable and understandable.

It is used to achieve the compile-time polymorphism in Java.




Method overriding in Java

The subclass method which is overriding the superclass method is called overriding method in java.



There are mainly three reasons for which we need to create subclass of superclass in Java.

1. To add a new feature or properties. 

2. To override or change the existing functionality of superclass method.

3. To inherit the existing functionality of superclass method.


Method Overriding rules in Java


1. Subclass method name must be the same as superclass method name.


2. The parameters of subclass method must be the same as superclass method parameters. 


3. Subclass method’s return type must be the same as superclass method return type. But this rule is applicable until Java 1.4 version only. From Java 1.5 version onwards, covariant return types are also allowed.


4. Subclass method’s access modifier must be the same or less than the superclass method access modifier. Look at the below figure to understand better.



Comments

Popular Posts