Object Oriented Programming in Java - Part 5

Q: What is Polymorphism ?
A: Poly means "many" and morphism means "form". Polymorphism means "Many forms". Formal definition, Data to be processed in more than 1 form.
Note: Java DOESN'T support operator overloading.
Q: What are 2 types of Polymorphism ?
A: Runtime Polymorphism and Compile time Polymorphism.
Q: What is Compile time or Static polymorphism ?
A: This is achieved via Method overloading. Multiple functions with same name exist so function is called based upon the no of arguments, type of arguments, and order of arguments each has. Its decided during compile time which function to call so its called compile time polymorphism.
public class Main {
public static void main(String[] args) {
Fruit a = new Fruit(); //constructor 1 called
Fruit b = new Fruit("apple", "red", 10); //constructor 2 invoked
Fruit c = new Fruit("Melon", "grey"); //constructor 3 invoked
a.display();
b.display();
c.display();
}
static class Fruit{
String name;
String color;
int weight;
Fruit(){ // constructor-1
name = "Grape";
color = "Green";
weight = 0;
}
Fruit(String name, String color, int weight){
// constructor-2
this.name = name;
this.color = color;
this.weight = weight;
}
Fruit(String name, String color){
// constructor-3
this.name = name;
this.color = color;
weight = 1;
}
void display(){
System.out.println("Name: " + this.name);
System.out.println("Color: " + this.color);
System.out.println("Weight: " + this.weight);
}
}
}
Q: What is Run time or Dynamic polymorphism ?
A: This is achieved via Method Overriding. Function with the same name exists in both the Parent class and Child class, therefore the function in parent class is overridden by child class. This happens during runtime so Run time polymorphism.
public class Main {
public static void main(String[] args) {
Child a = new Child(); // from child
a.sayHello(); // 1- output is ?
Parent b = new Child();
b.sayHello(); // 2- output is ?
}
static class Parent{
void sayHello(){ // from parent
System.out.println("Hello from parent");
}
}
static class Child extends Parent{
void sayHello(){ // from child
System.out.println("Hello from child");
}
}
}
// output 1 - "Hello from child"
// output 2 - "Hello from child"
Q: Explain the output for Parent b = new Child(); b.sayHello(); ?
A: Output is "Hello from child" why ? because the reference variable decides which attributes and methods can be accessed but The object decides which function to be executed. Even though reference is of type parent its the Object class which decides which version of the function to call.
Q: Can we prevent method overriding if yes how ?
A: Yes by using final keyword before the function name OR using static before the function name.
Q: Why overriding is NOT possible using static keyword ?
A: overriding depends on objects, but static doesn't depend on objects. So static methods cannot be overridden.
Q: What happens when we use final keyword before class-name?
A: This means you cannot create sub-class from that class. This is used to prevent inheritance.
NOTE: Overloading is not possible for data members of class.
Q: What is encapsulation ?
A: Binding of data members and member functions into a single unit. Encapsulation ensures 1 object cannot influence/interfere with another object until stated otherwise.
Example- we have boat class and 2 instances Boat A and Boat B. Boat A cannot control Boat B and similarly Boat B can control itself but not Boat A.
Q: What is abstraction ?
A: Hiding unnecessary details and presenting relevant information. For example a car, you start a car using key and start button but you don't know exactly what is happening inside the engine. This is abstraction. It provides security and simplicity.
Q: Difference between encapsulation and abstraction?
A: Abstraction deals with the design part and encapsulation deals with the implementation of the design.



