0% found this document useful (0 votes)
12 views17 pages

Oop Review Part 2

Uploaded by

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

Oop Review Part 2

Uploaded by

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

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

implement access modifiers described in lecture.


 You have to provide example program for

inheritance.
 After completion of programs you will have to

demonstrate what you have done.


 There will be strict punishment for copied

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();
}

The FastCarclass looks exactly like the original


PassingCarclass. However, rather than just
inheriting the Pass()method, it defines its own
version.
Point to be remember
 Because the FastCarclass inherits from
PassingCar, which itself inherits from Car, a
FastCar also inherits all the data fields and
methods of the Carclass.
 There are ways that you can control how

inheritance works (using the public,


protected, and privatekeywords)
Different types of polymorphism
in java
 Method Overloading
 Method Overriding
Method Overloading
 Method Overloading is a feature that allows a
class to have two or more methods having
same name, if their argument lists are different.
 Argument lists could differ in –

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).

 Methodoverriding is also called Dynamic


Polymorphism
Assignment
 Write
a program that implements
polymorphsim and implements overloading
and overriding.

You might also like