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; public class EncapsulationTest { public static void main(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()); } } class EncapsulateData { 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:
package com.javahonk.polymorphism; public class Pizza { String dough; String sauce; String cheese; public Pizza(String dough, String sauce, String cheese) { super(); this.dough = dough; this.sauce = sauce; this.cheese = cheese; } public void getMyPizza() { System.out.println("Your Pizza. Dough: " + dough + " Sauce: " + sauce + " Cheese: " + cheese); } }
package com.javahonk.polymorphism; public class ChickenPizza extends Pizza{ String toppings; public ChickenPizza(String dough, String sauce, String cheese,String toppings) { super(dough, sauce, cheese); this.setToppings(toppings); } public String getToppings() { return toppings; } public void setToppings(String toppings) { this.toppings = toppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Chicken Pizza toppings:" +toppings); } }
Note the overridden getMyPizza method. In addition to the information provided before, additional data about the toppings is included to the output.
package com.javahonk.polymorphism; public class VegetablePizza extends Pizza{ String veggieToppings; public VegetablePizza(String dough, String sauce, String cheese, String veggieToppings) { super(dough, sauce, cheese); this.setVeggieToppings(veggieToppings); } public String getVeggieToppings() { return veggieToppings; } public void setVeggieToppings(String veggieToppings) { this.veggieToppings = veggieToppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Vegitable Pizza toppings:" +veggieToppings); } }
Note that once again, the getMyPizza method has been overridden. This time again we have added veggieToppings.
To summarize, there are three classes: Pizza, ChickenPizza, and VegitablePizza. The two subclasses override the getMyPizza method and print pizza information.
Here is a BuildPizza test program that creates two Pizza variables. Each variable is assigned to one of the two Pizza classes. Each variable will be then printed.
package com.javahonk.polymorphism; public class BuildPizza { public static void main(String[] args) { Pizza chickenPizza, vegitablePizza; chickenPizza=new ChickenPizza("Classic", "Pizza Sause", "Normal", "Chicken"); vegitablePizza=new VegitablePizza("Modern", "Pizza Sause", "Medium", "Vegitable"); System.out.println("Your chicken pizza details:\n"); chickenPizza.getMyPizza(); System.out.println("\nYour vegitable pizza details:\n"); vegitablePizza.getMyPizza(); } }
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 polymorphism
Method 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:
package com.javahonk.polymorphism; public class Pizza { String dough; String sauce; String cheese; public Pizza(String dough, String sauce, String cheese) { super(); this.dough = dough; this.sauce = sauce; this.cheese = cheese; } public void getMyPizza() { System.out.println("Your Pizza. Dough: " + dough + " Sauce: " + sauce + " Cheese: " + cheese); } }
package com.javahonk.polymorphism; public class ChickenPizza extends Pizza{ String toppings; public ChickenPizza(String dough, String sauce, String cheese,String toppings) { super(dough, sauce, cheese); this.setToppings(toppings); } public String getToppings() { return toppings; } public void setToppings(String toppings) { this.toppings = toppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Chicken Pizza toppings:" +toppings); } }
Note the overridden getMyPizza method. In addition to the information provided before, additional data about the toppings is included to the output.
package com.javahonk.polymorphism; public class VegitablePizza extends Pizza{ String veggieToppings; public VegitablePizza(String dough, String sauce, String cheese, String veggieToppings) { super(dough, sauce, cheese); this.setVeggieToppings(veggieToppings); } public String getVeggieToppings() { return veggieToppings; } public void setVeggieToppings(String veggieToppings) { this.veggieToppings = veggieToppings; } public void getMyPizza(){ super.getMyPizza(); System.out.println("Vegitable Pizza toppings:" +veggieToppings); } }
Note that once again, the getMyPizza method has been overridden. This time again we have added vegibleToppings.
To summarize, there are three classes: Pizza, ChickenPizza, and VegitablePizza. The two subclasses override the getMyPizza method and print pizza information.
Here is a BuildPizza test program that creates three Pizza variables. Each variable is assigned to one of the three Pizza classes. Each variable will be then printed.
package com.javahonk.polymorphism; public class BuildPizza { public static void main(String[] args) { Pizza chickenPizza, vegitablePizza; chickenPizza=new ChickenPizza("Classic", "Pizza Sause", "Normal", "Chicken"); vegitablePizza=new VegitablePizza("Modern", "Pizza Sause", "Medium", "Vegitable"); System.out.println("Your chicken pizza details:\n"); chickenPizza.getMyPizza(); System.out.println("\nYour vegitable pizza details:\n"); vegitablePizza.getMyPizza(); } }
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: