Tuesday 13 March 2012

Polymorphism:Core of Object Oriented Programming

Java is an Object Oriented Language and Polymorphism is its core concept.Nobody can program efficiently without knowing the core programming techniques like Polymorphism,Code Reuse etc.The genuine meaning of polymorphism is "many forms".In Java this meaning is rightly justified.Polymorphism in Java means a method can have various versions.It may be in A super class and it can also be in sub class.There are ways to invoke the methods from either super class or the sub class.As you are familiar with Java you must know that a sub class in Java inherits all the methods of its super class(except methods are declared private in super class).Every sub class can provide its own functionality to every super class method.That is in simpler terms the method is overridden in sub class.We can invoke the super class method using both reference variable types either as of super class or of sub class.

Following Example make things clear for you.

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

public void shape_square(){
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
Shape shp=new Shape();
sqr.imageShape();
}
}

Output:-
The output of above program is
Image Shape Printed
Square Shape Printed

Explanation:-
There are certain you must have noticed in the code.First of all starting from our super class Shape it has one method imageShape().The sub class Square inherits the method imageShape() from its parent Shape.Square class provides square specific functionality to this method.
Note:-super keyword can be used to refer to the super class version of imageShape() method.
never use super outside method body.If you do so compiler don't hesitate to slap you.You can also add your additional methods to your more specific classes as we did here.
TestShape class makes object of the Square class and invokes method imageShape().This reference cause the sub class version of imageShape() which results in the presented output.

The story is not as you like there are some problems for you I know you can solve them,if possible let me know the answers with proper explanations:-


1.What would be the output of following:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
System.out.println("Square Shape Printed");
}

public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
sqr.shape_square();
}
}

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

2.What would be the output of following code:-

class Shape{
public void imageShape(){
System.out.println("Image Shape Printed");
}
}

class Square extends Shape{

public void imageShape(){
super.imageShape();
System.out.println("Square Shape Printed");
}

imageShape();
public void shape_square(){
imageShape();
System.out.println("The Shape is square");
}
}

class TestShape{
public static void main(String a[]){
Square sqr=new Square();
sqr.imageShape();
}
}

Why Java does not support multiple Inheritance?

One can always think of extending multiple classes to inherit the multiple features in the form of methods.Like following


class Car extends Accelerate,Color{ //interesting code}

but the above declaration is not legal in terms if Java.A class cannot extend more than one class.This exactly implies that only one parent class per class.But a class can have multiple ancestors such that if class A extends class B and class B extends class C then class A has two ancestors class B and C .A class can have multiple ancestors up the inheritance tree but it is not possible by direct declaration of extending classes.

In C++ ,One can extend multiple classes simultaneously that is the declaration we have done for class Car is correct in case of C++.Capability of extending multiple classes is called as "Multiple Inheritance".Java creators thought a lot about allowing the multiple inheritance,but they were messed up after using the multiple inheritance.Like in this case ,if a class extended two classes and if both classes has method(s) in common then how the methods will be inherited and how we will come to know that which method is called of which class.That is why they excluded the multiple inheritance in Java.There is a famous problem which is faced during the multiple inheritance application called as "Deadly Diamond of Death".This is named so because the shape of class diagram which is formed after multiple inheritance implementation.The diamond is formed when classes B and C extend A ,and both B and C inherit methods from A.If class D extends both B and C,and both B and C have overridden the methods in class A,class D in theory has inherited the two different implementations of same method.
:-


There is an indirect way by which you can implement the multiple inheritance in Java and this is through extending a class and implementing an interface just like following:-

class Car extends Accelerate implements Movable{
// implement all Movable methods
}

In this way you can get methods from both class and interface and you can override them according to your use.Remember all methods of interface needs to be overridden in implementing class

Run Postman API remote or code or command line

POSTMAN is used to run the APIs generally. What if we want to create a collection in postman and run it from some other code or command l...