Post navigation

What is concept of OOPS

What is concept of OOPS

Answer : Java languages provide mechanisms that help us to implement object-oriented model. They are encapsulation, inheritance and polymorphism. Let’s take these one by one:

Encapsulation : It is mechanism which binds code and the data together which it manipulates, and
keeps both safe from outside interference and misusing. To restrict access of variable and method inside the class you could use three modifiers public, protected and private. Below table shows the access to members permitted by each modifier.

Access Levels

Modifier

Class

Package

Subclass

World

public

Y

Y

Y

Y

protected

Y

Y

Y

N

no modifier

Y

Y

N

N

private

Y

N

N

N

Example:

package com.javahonk.encapsulationTest;publicclassEncapsulationTest{publicstaticvoidmain(String[] args){
EncapsulateData encapsulateData =new EncapsulateData();// ERROR below line : The field encapsulateData.name is not visible//encapsulateData.name = "Changing your name";// Only you can get name whatever set in the class
System.out.println("Your name: "+ encapsulateData.getName());//We can change name because modifer is public
encapsulateData.changeMyName="Java Monk";
System.out.println("Your change name: "+ encapsulateData.getChangeMyName());}}classEncapsulateData{private String name ="Java Honk";public String changeMyName ="Java Honk";public String getName(){return name;}public String getChangeMyName(){return changeMyName;}}

Output:

Your name: Java Honk
Your change name: Java Monk

Inheritance : Different kinds of objects often have a certain amount in common with each other. Chicken Pizza and Vegetable Pizza for example, all share the characteristics of Pizza (Dough, Sauce, Cheese). Yet each also defines additional features that make them different: Chicken Pizza would have chicken toppings and Vegetable Pizza will have vegetable topping.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Below example, Pizza now becomes the super class of ChickenPizza and Vegetable Pizza. In the Java programming language, each class is allowed to have one direct super class, and each super class has the potential for an unlimited number of sub classes:

The JVM calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable’s type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.

Output:

Polymorphism : It refers to a principle in which an object can have many different forms or stages. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class and used in different forms called Polymorphism.

Different forms of Polymorphism : In java we have two types of Polymorphism:

Dynamic or Run time polymorphism : This kind of polymorphism existed at run-time. Java compiler does not understand which method to called at compile. Only JVM decides which method is to be called at run-time.Method overriding is example of Dynamic or Run time polymorphism because method binding between method call and method definition happens at run time and it’s depend on the object of the class (when object create at run time and goes to heap) .

Static or Compile time polymorphism : This kind of polymorphism exhibited at compile time. Java compiler knows in advance which method to be called. Method overloading and method overriding using static methods; method overriding using private or final methods are examples for static polymorphismMethod overloading is an example of Static or Compile time polymorphism because method binding between method call and method definition happens at compile time and it’s depend on the reference of the class(when objects create at compile time and goes to stack).

To demonstrate polymorphic features example given below where we are Pizza class with ChickenPizza and VegitablePizza and adding extra field toppings in both child class:

The JVM calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable’s type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.