3. Intended Learning Outcomes (ILOs)
▪ Inherit properties of classes in other classes.
▪ Apply user-defined constructors during class inheritance.
▪ Apply appropriate access modifies of class methods in hiding
information.
▪ Identify and set non-overriden methods of a class.
▪ Create, implement and reuse an interface.
▪ Create a Java project that exhibit the use of class
inheritance, method overriding, and interfaces
New Wave Analytica
4. Teaching and Learning Activities (TLAs)
▪ Lecture
▪ Assignment
▪ Program Simulation and Demonstration
▪ Group Discussion
▪ Exercise
New Wave Analytica
6. Inheritance
▪ Java / Other OOP Languages
▪Inheritance is a mechanism that enables one class to inherit, or assume,
both the behavior and the attributes of another class.
▪Inheritance is the principle that allows you to apply your knowledge of a
general category to more specific objects.
New Wave Analytica
7. Genetic Inheritance
▪ Genetic
▪Facts / Attributes
▪ Blood type
▪ Eye Color
▪Behavior
▪ Attitude for saving money
▪ Pulling ears when tired
New Wave Analytica
8. Termonologies
▪ Base Class / SuperClass / Parent Class
▪Class that is used as basis for inheritance
▪ Derived Class / SubClass / Child Class
▪Class that inherits from a base class
New Wave Analytica
9. SuperClass vs SubClass
▪ Size
▪Sublclass is larger that the SuperClass
▪Contains all the fields and methods of the SuperClass
▪Additional fields and methods
New Wave Analytica
10. Extending Classes
▪ extends
▪use to achieve inheritance in Java
▪Example
▪EmployeeWithBenefits extends Employee
New Wave Analytica
11. Overriding SuperClass Methods
▪ SubClass contains data and methods defined in the
SuperClass
▪Any Child Class has all the attributes of its parent.
▪ SuperClass data fields are not entirely appropriate for the
SubClass objects.
▪ Override the parent class members
New Wave Analytica
12. Calling Constructors During Inheritance
▪ When you instantiate an object that is a member of a
subclass, you are actually calling at least two
constructors
▪Constructor for the base class
▪Constructor for the derived class
▪When you create any subclass object, the superclass
constructor must execute first, and then the subclass
constructor executes
New Wave Analytica
13. SuperClass Constructors that Require Arguments
▪ When you create a class that do not have a
Constructor, Java automatically supplies a default
constructor – one that never requires argument.
▪ When you write your own constructor, you replace the
automatically supplied version.
New Wave Analytica
14. SuperClass Constructors that Require Arguments
▪ Your subclass constructors can contain any number of
statements, but if all superclass constructors require
arguments, then the first statement within each
subclass constructor must call the superclass
constructor.
New Wave Analytica
15. Format of statement that calls a SuperClass Constructor
▪ super(list of arguments);
New Wave Analytica
16. Types of Non-Overriden Methods
▪ static methods
▪ final methods
▪ methods with final class
New Wave Analytica
18. Abstract Class
▪ Abstraction refers to the ability to make a class abstract in OOP.
▪An abstract class is one that cannot be instantiated.
▪ All other functionality of the class still exists, and its fields,
methods, and constructors are all accessed in the same manner.
▪You just cannot create an instance of the abstract class.
New Wave Analytica
19. Extending Two or More Classes
▪ Extending two or more classes is not allowed
▪ Example
public class EmployeeWithBenefits extends EmployeeA, EmployeeB
New Wave Analytica
20. Information Hiding
▪ Private field data cannot be directly altered by an instance of a
class (in another Class)
▪ Example
Employee newEmployee = new Employee();
newEmployee.empNumber = 123;
▪ Only methods that are part of the class are allowed to alter a
private data.
New Wave Analytica
21. Information Hiding
▪ When a class serves as a superclass of other classes, the
subclass inherits (use) all the data and methods of the
superclass with one exception
▪Private members of a parent class are not accessible within a
child’s class methods.
New Wave Analytica
22. Information Hiding : Protected
▪ Provides an immediate level of security between public and
private access.
▪ Protected fields and methods can only be accessed by its own
class and its descendant (subclass)
New Wave Analytica
23. Interface
▪ A Java interface is an abstract type used to designate a set of
abstract methods for classes to implement.
▪ When a class implements an interface, it must inherit all of the
abstract methods declared within, as though signing into a
contract and carrying out the agreement.
▪ An interface is a completely abstract class that is used to group
related methods with empty bodies.
New Wave Analytica
24. Example
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void run(); // interface method (does not have a body)
}
New Wave Analytica