Ch2-Inheritance Polymorphism Interfaces
Ch2-Inheritance Polymorphism Interfaces
Interfaces
Objectives
At the end of the lesson, the student should be able to:
• Superclass
• Any class above a specific class in the class hierarchy.
• Subclass
• Any class below a specific class in the class hierarchy.
Inheritance
• Superclass
• Any class above a specific class in the class hierarchy.
• Subclass
• Any class below a specific class in the class hierarchy.
Inheritance
• Benefits of Inheritance in OOP : Reusability
• Once a behavior (method) is defined in a superclass, that behavior
is automatically inherited by all subclasses.
• Thus, you can encode a method only once and they can be used by
all subclasses.
• A subclass only needs to implement the differences between itself
and the parent.
Inheritance
• To derive a class, we use the extends keyword.
• In order to illustrate this, let's create a sample parent class.
• Suppose we have a parent class called Person.
public class Person {
protected String name;
protected String address;
/**
* Default constructor
*/
public Person(){
System.out.println(“Inside Person:Constructor”);
name = ""; address = "";
}
. . . .
}
Inheritance
• Now, we want to create another class named Student.
• Since a student is also a person, we decide to just extend the
class Person, so that we can inherit all the properties and
methods of the existing class Person.
• To do this, we write,
public class Student extends Person {
public Student(){
System.out.println(“Inside Student:Constructor”);
}
. . . .
}
Inheritance
• When a Student object is instantiated, the default constructor of
its superclass is invoked implicitly to do the necessary
initializations.
• After that, the statements inside the subclass's constructor are
executed.
Inheritance:
• To illustrate this, consider the following code,
public static void main( String[] args ){
Student anna = new Student();
}
public Student(){
super( "SomeName", "SomeAddress" );
System.out.println("Inside Student:Constructor");
}
The “super” keyword
• Few things to remember when using the super constructor call:
• The super() call MUST OCCUR AS THE FIRST STATEMENT IN A
CONSTRUCTOR.
• The super() call can only be used in a constructor definition.
• This implies that the this() construct and the super() calls CANNOT
BOTH OCCUR IN THE SAME CONSTRUCTOR.
The “super” keyword
• Another use of super is to refer to members of the superclass
(just like the this reference ).
• For example,
public Student() {
super.name = “somename”;
super.address = “some address”;
}
Overriding methods
• If for some reason a derived class needs to have a different
implementation of a certain method from that of the superclass,
overriding methods could prove to be very useful.
Person ref;
Student studentObject = new Student();
Employee employeeObject = new Employee();
printInformation( studentObject );
printInformation( employeeObject );
}
Abstract Classes
• Abstract class
• a class that cannot be instantiated.
• often appears at the top of an object-oriented programming class
hierarchy, defining the broad types of actions possible with objects
of all subclasses of the class.
Abstract Classes
• abstract methods
• methods in the abstract classes that do not have implementation
• To create an abstract method, just write the method declaration
without the body and use the abstract keyword
• For example,
public abstract void someMethod();
Sample Abstract Class
public abstract class LivingThing {
public void breath(){
System.out.println("Living Thing breathing...");
}
/**
* abstract method walk
* We want this method to be overridden by subclasses of
* LivingThing
*/
public abstract void walk();
}
Abstract Classes
• When a class extends the LivingThing abstract class, it is
required to override the abstract method walk(), or else, that
subclass will also become an abstract class, and therefore
cannot be instantiated.
• For example,
public class Human extends LivingThing {
}
Sample Abstract Class
public abstract class Animal {
/*
abstract methods
*/
public abstract void move();
public abstract void makeNoise();
}
Abstract Classes
• When a class extends the Animal abstract class, we need to
override both move and makeNoise methods.
• For example,
public class Cow extends Animal {
Difference:
Interface does not have any implementation of the methods
Creating Interfaces
• To create an interface, we write: