You are on page 1of 26

INHERITANCE

WHAT IS INHERITANCE?
 ability of objects in java to inherit properties
and methods from other objects

A
parent, superclass,
base class

child, subclass,
B
derive class
ADVANTAGES OF INHERITANCE
1. Reuse of classes that have already been written
2. Grouping of related classes
3. Enhanced understandability and readability by
allowing classes to be arranged in a logical
manner
WHEN TO USE INHERITANCE
 is-a rule
Example:
a. class Student & class UndergraduateStudent
b. class Employee & class Faculty
c. class Shape & class Circle
INSIDE INHERENCE
 Keyword extends
class Shape{ class that is being inherited

}
class Circle extends Shape{

class doing inheriting


}
ACCESSING MEMBERS OF AN INHERITED
CLASS
class Student {
private int studentID;
Student () {
studentID = 12345;
}
protected void display(){
System.out.println(“Student ID:” +studentID);
}
}

class GraduateStudent extends Student{

}
class Demo {
public static void main (String args[]) {
GraduateStudent gs = new GraduateStudent();
gs.display();
}
}
THE SUPERCLASS CAN BE INSTANTIATED
class Student {
private int studentID;
Student () {
studentID = 12345;
}
protected void display(){
System.out.println(“Student ID:” +studentID);
}
}
class GraduateStudent extends Student{

}
class Demo {
public static void main (String args[]) {
Student s = new Student( );
s.display();
}
}
CALLING CONSTRUCTORS
 At least two constructors are involved
whenever a subclass inherits a superclass
CALLING CONSTRUCTORS
class Superclass{
Superclass(){
System.out.println(“superclass”);
}
}
class Subclass extends Superclass{
Subclass(){
System.out.println(“subclass”);
}
}
class Demo{
public static void main(String args[]){
Subclass s=new Subclass();
}
}
TYPES OF INHERITANCE
 Simple or one way inheritance
 Example

Student

GraduateStudent
TYPES OF INHERITANCE…CONT.
 Multiple inheritance
 Example

Person Student

GraduateStudent
TYPES OF INHERITANCE…CONT.
 Multilevel inheritance
 Example

Person

Student

GraduateStudent
 Example

Person

Doctor Typist

Psychiatrist

A “family tree” of all the classes in a program is called a class


hierarchy
USING THE SUPER KEYWORD

2 uses of a super keyword


1. used to refer to the constructor of the superclass
2. used to refer to the member of the superclass
class Student{ class Demo{
private int studentID; public static void main
Student (){ (String args []){
studentID = 12345; GraduateStudent gs =
} new GraduateStudent ();
Student (int sID){ gs.display2();
studentID = sID; }
} }
protected void display(){
System.out.println(“Student ID:” +studentID);
}
}
class GraduateStudent extends Student{
GraduateStudent(){
super ();
}
public void display2() {
super.display();
}
}
class Student{ class Demo{
private int studentID; public static void main
Student (){ (String args []){
studentID = 12345; GraduateStudent gs =
} new GraduateStudent ();
Student (int sID){ gs.display2();
studentID = sID; }
} }
protected void display(){
System.out.println(“Student ID:” +studentID);
}
}
class GraduateStudent extends Student{
GraduateStudent(){
super (55555);
}
public void display2() {
super.display();
}
}
class Person{
private String name;
Person(){
name = “Bob Smith”;
}
protected void displayName(){
System.out.println(“Student Name:” +name);
}
}
class Student extends Person{
private int studentID;
Student (){
studentID = 12345;
}
protected void displayStudentID(){
System.out.println(“Student ID:” +studentID);
}
class Demo{
}
public static void main
class GraduateStudent extends Student {
(String args[]){
protected void display(){
GraduateStudent gs =
displayName();
new GraduateStudent();
displayStudentID();
gs.display();
}
}
}
}
OVERRIDING METHOD MEMBER USING
INHERITANCE
 Rules that must be followed when a
subclass overrides a method in a superclass:
1. The return type, method name, and parameter
list must be identical.
2. The access specifier must be at least that of the
parent.
class Person{
protected String name;
Person(){
name = “Bob Smith”;
}
void display(){
System.out.println(“Person Class:” + name);
}
}
class Student extends Person{
protected int studentID;
Student(){
studentID = 12345;
}
void display(){
System.out.println(“Student Class:” +studentID);
}
}
class Demo{
public static void main (String args[]){
Student s = new Student ();
s.display();
}
}
class Person{
protected String name;
Person(){
name = “Bob Smith”;
}
void display(){
System.out.println(“Person Class:” + name);
}
class Demo{
}
public static void main (String
class Student extends Person{
args[]){
protected int studentID;
GraduateStudent gs = new
Student(){
GraduateStudent ();
studentID = 12345;
gs.display();
}
}}
void display(){
System.out.println(“Student Class:” +studentID);
}
}
class GraduateStudent extends Student {
void display(){
System.out.println(“Graduate Student Class:”);
System.out.println(“Name:” +name);
System.out.println(“Student ID:” +studentID);
}
}
FINAL KEYWORD
 3 uses of the final keyword

1. it can be used to create the equivalent of a


named constant
example:
final int STUDENT_ID=12345;
FINAL KEYWORD…CONT.
2. it can be used to prevent overriding of methods

example:

class Person{
final void warningMsg(){
System.out.println(“Invalid Entry.”);
}
}
FINAL KEYWORD…CONT.
3. it can be used to prevent a class from being
inherited

example:
final class Person{
void warningMsg(){
System.out.println(“Invalid Entry.”);
}
}
THE OBJECT CLASS AND SUBCLASSES
 Object class – a master class that is
automatically inherited by all other classes,
including classes you define in your program
ABSTRACT CLASS
 is a class that contains at least one abstract method
member
 is a superclass that defines a general method member

 the keyword abstract is placed before the class


keyword
 Note: It is not possible to instantiate an object from the
abstract class.
 abstract method member – similar in declaring a
method member, except that the definition begins with
the keyword abstract.
class AbstractDemo{
public static void main (String args[]){
Student s = new Student();
s.display();
}
}
abstract class Person{
abstract void display();
}
class Student extends Person{
protected int studentID;
Student (){
studentID = 12345;
}
void display(){
System.out.println(“Student Class:” +
studentID);
}
}

You might also like