OOP Chapter 4
OOP Chapter 4
OOP Chapter 4
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
HOW TO IMPLEMENT INHERITANCE ?
In Java we use the keyword extends to indicate that a class is inheriting its
characteristics from another class
In order to use the inherited attributes; they should be declared in the parent class
as either public (public accessors) or protected.
If we only want child classes to use the attributes; protected access modifier is
used.
A child class can have its own attributes and operations.
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
An Example
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
CONSTRUCTORS
If the parent class has a constructor with parameters, a child class should
explicitly call the constructor of the parent class in its constructor and pass it the
needed arguments using the keywork super.
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
An Example
public class Employee
{
protected String name;
protected double salary;
}
CREATING OBJECTS
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
An Example
public class TestEmployee
{
publis static void main(String[] args)
{
SalesEmployee sm = new SalesEmployee(“ahmad”, 500, 100, 0.10);
Employee em = new Employee(“enas”, 600);
//.. some code here
}
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
OVERRIDING
A child class can use public operations that are defined in the parent class.
A child class can add other operations that are specific to the child class.
A child class can also redefine the parent class operations. This is called
overriding.
Overriding allows the child class to add its own implementation to the inherited
operation, but it should keep the same signature (name and parameters)
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
An Example
public class Employee
{
protected String name;
protected double salary;
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
INTERFACE
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
An Example
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
POLYMORPHISM
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
POLYMORPHISM
Suppose that Animal class has a method called breathe( ) and Cat class overrides
breathe( ) method. This method can be called using a1 or a2 objects.
The type of the referenced object will determine at runtime which breathe( ) to
call.
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP
See lectures polymorphism examples
THESE SLIDES ARE DESIGNED TO BE USED IN OOP COURSE AT PHILADELPHIA UNIVERSITY BY ENAS NAFFAR. SOME SLIDES
ARE TAKEN FROM PEARSON EDUCATION, AND MARTY STEP