Polymorphism: Application Programming: Java Programming
Polymorphism: Application Programming: Java Programming
2
Objectives
• Explain the concept of polymorphism
• Describe and distinguish method overloading
• Describe and distinguish method overriding
• Explain the concepts of early binding and late binding
• Demonstrate when it is appropriate to implement
polymorphism
3
Defining Polymorphism
• Polymorphism is the ability of different objects to
respond to the same message in different ways. This
means that different objects can have very different
method implementations for the same message.
4
Method Overloading
• Method Overloading is the process of declaring
methods with the same name but different parameter
types.
5
Rules of Method Overloading
1. Overloaded methods must change the argument list.
6
Implementing Method Overloading
public static void main(String[] args) {
Sales s = new Sales();
System.out.println(s.computeSales(100));
System.out.println(s.computeSales(100,2));
System.out.println(s.computeSales(100,2,30));
}
class Sales {
double computeSales(double price) {
double sales;
sales = price;
return sales;
}
double computeSales(double price, int qty) {
double sales;
sales = price * qty;
return sales;
}
double computeSales(double price, int qty, double discount) {
double sales;
sales = (price * qty) - discount;
return sales; 100.0
} 200.0
} 170.0
7
Method Overriding
• Method Overriding allows a subclass to redefine
methods of the same name from the superclass.
8
Rules of Method Overriding
1. An overridden method must have
• the same name
• the same number of parameters and types
• the same return type
as the overridden method.
2. Overriding a method cannot narrow the method access
level defined in the overridden method.
3. Overriding a method cannot widen checked exceptions
defined in the overridden method.
4. Methods declared as private, static, or final
cannot be overridden.
5. A static method cannot override an instance method.
9
Implementing Method Overriding
public static void main(String[] args) {
Sales s = new Sales(); Define Sales reference
Sales st = new SalesTax(); variable containing SalesTax
object
System.out.println(s.computeSales(100));
System.out.println(s.computeSales(100,2));
System.out.println(s.computeSales(100,2,30));
System.out.println(st.computeSales(100));
}
class Sales {
double computeSales(double price) {
double sales = price;
return sales; It determined which method
} to run based on object type
double computeSales(double price, int qty) { (SalesTax) instead of
double sales = price * qty; reference type (Sales)
return sales;
}
double computeSales(double price, int qty, double discount) {
double sales = (price * qty) - discount;
return sales;
}
}
11
Implementing Polymorphism
class Animal {
public void eat() { System.out.println("Animal eating...");}
}
class Snake extends Animal {
public void eat() { System.out.println("Snake eating...");}
}
class Horse extends Animal {
public void eat() { System.out.println("Horse eating...");}
public void eat(String s) { System.out.println("Horse eating " + s); }
}
Animal eating...
public static void main(String[] args) { Snake eating...
Animal pig = new Animal(); Horse eating...
Snake viper = new Snake(); Horse eating grass
Horse stallion = new Horse(); Snake eating...
Animal animalViper = new Snake();
Horse eating...
Animal animalStallion = new Horse();
13
Questions and Comments
14