0% found this document useful (0 votes)
5 views

Polymorphism in Java

The document explains polymorphism in Java, a key principle of Object-Oriented Programming, which includes compile-time and run-time polymorphism. Compile-time polymorphism is demonstrated through method overloading, while run-time polymorphism is shown via method overriding. Examples of both concepts are provided through Java code snippets illustrating addition methods and animal sounds.

Uploaded by

jeyaboopathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Polymorphism in Java

The document explains polymorphism in Java, a key principle of Object-Oriented Programming, which includes compile-time and run-time polymorphism. Compile-time polymorphism is demonstrated through method overloading, while run-time polymorphism is shown via method overriding. Examples of both concepts are provided through Java code snippets illustrating addition methods and animal sounds.

Uploaded by

jeyaboopathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Polymorphism in Java

Dr.J.Jeyaboopathiraja
Assistant Professor
Department of Computer Science
Sri Ramakrishna College of Arts & Science
Coimbatore
OOPs (Object-Oriented Programming System) in Java is a programming paradigm
based on the concept of objects and classes.

Class is the blueprint of an object. It is used to create objects.

An object is an instance of the class.

Polymorphism is one of the basic principles of Object-Oriented Programming. There


are two types of polymorphism:

• Compile time Polymorphism


• Run time Polymorphism
Compile-time polymorphism can be achieved by method overloading in
java.

When different functions in a class has the same name but different
signatures, it’s called method overloading.

Run-time polymorphism is achieved through method overriding, which


happens when a method in a subclass has the same name, return type,
and parameters as a method in its superclass.

When the superclass method is overridden in the subclass, it is called


method overriding.
class addition{
int add(int a, int b){
return a+b;
}
int add(int a,int b, int c){
return a+b+c;
}

double add(double a,double b){


return a+b;
}
}

public class Main {


public static void main(String[] args){
addition math = new addition();
System.out.println(math.add(5,5));
System.out.println(math.add(5,6,7));
System.out.println(math.add(5.5,6.7));
}
}
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override //(Overriding the method)
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
//Animal myAnimal = new Animal(); // Parent class object
// myAnimal.makeSound();

Dog myDog = new Dog(); // Child class object


myDog.makeSound();
Animal ref = new Dog(); // Parent reference, Child object (Runtime)
ref.makeSound(); (Method Overriding in action)
}
}
THANK YOU

You might also like