Learn Java lessons online! - So Simple Even Your Kids Can Do It

Page 1

Learn Java lessons online! Java is a high-level programming language developed by Sun Microsystems but later was taken over by Oracle. This tutorial gives a basic understanding on Polymorphism. What is Polymorphism? The ability of an object to take many forms is Polymorphism. The most common use of polymorphism in OOP occurs when a parent class reference is used to mention to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. Polymorphism is the capability of a method to do variety of things based on the object that it is acting upon. In simple words, polymorphism allows you to define one interface and have multiple executions. Key points: • • • •

This feature allows one interface to be used for many class of actions. An operation may show different behavior in different instances. Types of data used in the operation decide the behavior Polymorphism is extensively used in executing inheritance.

Two types of polymorphism available in JAVA are: 1) Method Overloading 2) Method Overriding A method is a set of code which is mentioned by name and can be invoked at any point in a program simply by utilizing the method’s name. 1)Method Overloading: With different argument list or parameters,in Java it is possible to define two or more methods of same name in a class. This concept is called Method Overloading. An overloaded method can throw different expectations. And it can have different access modifiers. Rules for Method Overloading Change method signature. Return type method is never part of method signature, so only changing the return type of method does not amount to method overloading. Overloaded method throws the same supposition, a different exception or it simply does not toss any exception; no effect at all on method loading.


Example: class Overload { void demo (int a) { System.out.println ("a: " + a); } void demo (int a, int b) { System.out.println ("a and b: " + a + "," + b); } double demo(double a) { System.out.println("double a: " + a); return a*a; } } class MethodOverloading { public static void main (String args []) { Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20); result = Obj .demo(5.5); System.out.println("O/P : " + result); } }

Here the method demo() is encumbered 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The methods are implored with the same type and number of variable used. Output: a: 10 a and b: 10,20 double a: 5.5 O/P : 30.25

2) Method Overriding In method overriding, child class overrides the parent class method without even touching the source code of the base class. Child class has the same method as of base class. Rules for Method Overriding: 1. 2. 3. 4. 5. 6.

Only inherited methods can be overridden object type determines which overridden method will be used at execution. Overriding method can have different result type Abstract methods must be overridden What can't be overridden are constructors and static and final methods. It is also known as Runtime polymorphism.


class Vehicle {

public void move () { System.out.println ("Vehicles are used for moving from one place to another "); } } class Car extends Vehicle { public void move () { super. move (); // invokes the super class method System.out.println ("Car is a good medium of transport "); } } public class TestCar { public static void main (String args []){ Vehicle b = new Car (); // Vehicle reference but Car object b.move (); //Calls the method in Car class } }

Output: In order to commute from one place to another vehicles are used. Car is a good medium of transportation


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.