Oop Review Part 2
Oop Review Part 2
Enterprise Application
Development
Tariq Mahmood Rana
Object-Oriented Programming
Revisited
Practical Assignment # 1 for previous lecture
Polymorphism
Practical Assignment
Assignment # 1 is based on Previous lecture OOP
Review Part 1
You have to write example programs which
inheritance.
After completion of programs you will have to
assignment.
Polymorphism
Poly means –Many and Morphism means -Forms. Hence Polymorphism
means many forms.
The last major feature of object-oriented programming is polymorphism.
By using polymorphism, you can create new objects that perform the same
functions as the base object but which perform one or more of these
functions in a different way.
For example, you may have a shape object that draws a circle on the
screen. By using polymorphism, you can create a shape object that draws a
rectangle instead.
You do this by creating a new version of the method that draws the shape
on the screen.
Both the old circle-drawing and the new rectangle-drawing method have the
same name (such as DrawShape()) but accomplish the drawing in a
different way.
Encapsulation, Inheritance,
and Polymorphism
Car as an object having several characteristics (direction,
position, and speed)
And several means (steering wheel, gas pedal, and brakes)
to act on those characteristics.
In terms of constructing a class for a car object, you can think
of direction, position, and speed as the class's data fields
Steering wheel, gas pedal, and brakes as representing the
class's methods.
The first step in creating an object is to define its class.
For now, you'll use pseudo-code to create a Car class.The
base Carclass might look like.
The pseudocode for a Base
Car Class.
class Car
{
data direction;
data position;
data speed;
method Steer();
method PressGasPedal();
method PressBrake();
}
Deriving a New Class Using
Inheritance
Class PassingCar inherits from Car
{
method Pass();
}
Using Polymorphism to Create
a Faster Car
class FastCar inherits from PassingCar
{
method Pass();
}
1. Number of parameters.
2. Data type of parameters.
3. Sequence of Data type of parameters.
Method overloading is also known as Static
Polymorphism.
Points to be remembered
1. Static Polymorphism is also known as
compile time binding or early binding.
2. Static binding happens at compile time.
Method overloading is an example of static
binding where binding of method call to its
definition happens at Compile time.
Example 1: Overloading – Different Number of
parameters in argument list
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Method Overriding in Java
Declaringa method in subclass which is
already present in parent class is known as
method overriding.
Example:
class Human{
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
obj.eat();
}
}
Output:
Boy is eating
Advantages
The main advantage of method overriding is
that the class can give its own specific
implementation to a inherited method without
even modifying the parent class(base class).