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();
}
}

No comments:

Post a Comment

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...