[go: up one dir, main page]

0% found this document useful (0 votes)
118 views19 pages

Polymorph Is M

The document discusses polymorphism in Java, specifically method overloading and overriding. It defines polymorphism as many shapes, and in Java it refers to having multiple methods with the same name in a class. There are two types of polymorphism: overloading, which uses different signatures for methods, and overriding, which replaces an inherited method with the same signature. Signatures distinguish methods based on parameter types and number in Java. Method overloading allows same named methods with different signatures, while overriding replaces a parent method with a child version.

Uploaded by

Pranay Kinra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views19 pages

Polymorph Is M

The document discusses polymorphism in Java, specifically method overloading and overriding. It defines polymorphism as many shapes, and in Java it refers to having multiple methods with the same name in a class. There are two types of polymorphism: overloading, which uses different signatures for methods, and overriding, which replaces an inherited method with the same signature. Signatures distinguish methods based on parameter types and number in Java. Method overloading allows same named methods with different signatures, while overriding replaces a parent method with a child version.

Uploaded by

Pranay Kinra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Polymorphism

Polymorphism
Polymorphism means many (poly) shapes
(morph)
In Java, polymorphism refers to the fact that
you can have multiple methods with the
same name in the same class
There are two kinds of polymorphism:
Overloading
Two or more methods with different signatures

Overriding
Replacing an inherited method with another having the
same signature

Signatures
In any programming language, a signature is what
distinguishes one function or method from another
In C, every function has to have a different name
In Java, two methods have to differ in their names
or in the number or types of their parameters
foo(int i) and foo(int i, int j) are different
foo(int i) and foo(int k) are the same
foo(int i, double d) and foo(double d, int i) are different

In C++, the signature also includes the return type


But not in Java!

Method Overloading
Method Overloading is the process of using
methods having same names with different
signature.
A method can be overloaded in the same class
or in a subclass.
Which overloaded method to call is based on
reference type and determined at compile time.
It is also called as compile time polymorphism.
Automatic type conversions apply to
overloading.

How to NOT overload methods:


Its also very important to understand that method
overloading is NOT something that can be
accomplished with either, or both, of these two
things:
1. Just changing the return type of the method. If the
return type of the method is the only thing
changed, then this will result in a compiler error.
2. Changing just the name of the method
parameters, but not changing the parameter
types. If the name of the method parameter is the
only thing changed then this will also result in a
compiler error.

Demonstrate method overloading


class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}

class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}

No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test (123.2): 15178.24

OVERRIDING
The process of defining a method in child class with the same
name & signature as that of a method already present in parent
class.
If a class inherits a method from its super class, then
there is a chance to override the method provided that it
is not marked final.

Benefit: A subclass can implement a parent class method


based on its requirement.

In object-oriented terms, overriding means to override


the functionality of an existing method.

OVERRIDING EXAMPLE 1
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
class TestDog{
public static void main(String args[]){
Animal a = new Animal();
// Animal reference and object
Animals can move

Dogs can walk and


run

Animal b = new Dog();


// Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
}

class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and
run");
}
}
class Cat extends Dog{
public void move(){
System.out.println("Cats can jump,walk
and run");
}
}
class Rat extends Cat{
public void move(){
System.out.println("Rats can
dance,jump,walk and run");
}
}

class TestDog{
public static void main(String
args[]){
Animal a = new Animal();
Animal reference and object

Animal b = new Dog();


// Animal reference but Dog object

Dog b = new Dog();


b.move();
b=new Cat();
b.move();
b=new Rat();
a.move();
// runs the method in Animal class

b.move();
//Runs the method in Dog class

}
}

Output

OVERRIDING BASE REFERENCE AND CHILD


OBJECT

Suppose there is a scenario that CHILD inherits BASE class, as


shown in the figure.
Both BASE and CHILD classes would have their
own methods, as well as some overridden
BASE
CHIL
methods, if any.
Category I: Methods present in BASE class only.
Category II: Methods present in CHLD class only.
Category III: Methods available in BASE but
overridden in CHILD.
Now, if we create an object with BASE class
reference and CHILD class object as:
BASE ref = new CHILD();
Then, ref could access the methods belonging to
Category I and Category III only.

OVERRIDING EXAMPLE 1 - EXPLANANTION


In the above example, even though b is a type of Animal; it runs
the move method in the Dog class.
REASON:
In compile time, the check is made on the reference type.
However, in the runtime, JVM figures out the object type and
would run the method that belongs to that particular object.

Therefore, in the above example, the program will compile


properly since Animal class has the method move. Then, at the
runtime, it runs the method specific for that object.

OVERRIDING EXAMPLE 2
1. class Animal{
2. }
3. class Dog extends Animal{
4.
public void bark(){
5.
System.out.println("Dogs can bark");
6.
}
7. }
8. public class TestDog{
9.
public static void main(String args[]){
10.
Animal a = new Animal(); // Animal reference
and object
11.
Animal b = new Dog(); // Animal reference but
Dog object
12.
b.bark();
Result (ERROR):
13.
}
14.}
TestDog.java:12: cannot find
symbol
symbol : method bark()
location: class Animal b.bark();

RULES FOR METHOD OVERRIDING PART 1


1. The argument list should be exactly the same as that of
the overridden method.
2. The return type should be the same or a subtype of the
return type declared in the original overridden method in
the superclass.
3. The access level cannot be more restrictive than the
overridden method's access level. For example: if the
superclass method is declared public then the overridding
method in the sub class cannot be either private or
protected.
4. A method declared final cannot be overridden.
5. A method declared static cannot be overridden.

RULES FOR METHOD OVERRIDING PART 2


6. If a method cannot be inherited, then it cannot be
overridden.
7. A subclass within the same package can override any
superclass method that is not declared private or final.
8. A subclass in a different package can only override the
non-final methods declared public or protected.
9. . Constructors cannot be overridden.

OVERRIDING: USING THE SUPER KEYWORD


class Animal{
public void move(){
System.out.println("Animals can move");
}
When invoking a superclass version
}
of an overridden method the super
class Dog extends Animal{ keyword is used.
public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
class TestDog{
public static void main(String args[]){
Animal b = new Dog();
// Animal reference but This
Dogwould
object
produce the following
b.move();
result:
//Runs the method in Dog class
Animals can move
}
Dogs can walk and run
}

You might also like