Prepared by: Prof. Irysh Paulo R. Tipay, MSCS
Quote for the Day
“That's the thing about people who think they hate computers. What they really hate is lousy programmers.” ― Larry Niven
Recap!
What you already know…
In OOP, EVERYTHING is an OBJECT
A Class is a blueprint/model used for creating
Objects
An Object is an instance of a Class
Both Objects and Classes have an attribute and behaviour A class should have at least one Constructor.
A Constructor is used to set the default values of the attributes
A Class Diagram is a UML which is basically a graphical representation of a class
Inheritance
This is a way of relating 2 or more classes.
This is the idea of passing down the attributes and behaviours of the super class to the sub class. Super class – (base class,
SUPER CLASS parent class)
Sub class – (child class,
CLASS
derivedSUB class) This is the symbol used to show inheritance
Inheritance
Inheritance is an “is-a” relationship.
Student is a Person
Teacher is a Person
Cat is an Animal
Dog is a Fruit
Grape is a Fruit
Inheritance
Java supports single inheritance, meaning a super class can have multiple sub classes, but a sub class can only have one super class. SUPER CLASS
SUB CLASS
SUB CLASS
SUB CLASS
Inheritance
A sub class can be a super class of another class. CLASS A
CLASS B
CLASS C
Inheritance
Observe…
VEHICLE
CAR
Inheritance
Vehicle
- String color
- String bodyNumber
-String
numberOfWheels
+Vehicle()
+void accelerate();
+ void brake();
Car
- String plateNumber
- String mileage
+Car()
+void playMusic();
+ void enableGPS();
Car myCar = new Car( ); myCar.accelerate( );
In this Example, the Car class is able to inherit all the members of the
Vehicle class EXCEPT for its private members. The “extends” keyword public class Vechicle{ private String color; private String bodyNumber; private String numberOfWheels; public Vehicle(){ color = “blue”;
public