Fundamentals of Software
Development
CT010-3-1
Introduction to Inheritance
Prepared by: STH First Prepared on:27 th July 2005 Last Modified on:19th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation
Topic & Structure of the lesson
Inheritance
superclass and subclass
single level inheritance
using extends
Constructors and creating them
Sample programs
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Learning Outcomes
At the end of this topic, you should be able to:
Explain object-oriented programming concepts
using appropriate examples and inheritance using
appropriate examples.
Create, edit, compile, run and debug simple objectoriented Java programs using inheritance.
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
Inheritance
Superclass
Subclass
Constructors
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
What is Inheritance?
Definition : Inheritance is a mechanism for defining a new class in terms of an existing class.
Inheritance allows a group of related classes to come together under a single umbrella, so
that they can be considered and managed collectively.
Parent class
(superclass)
ball
basketball
football
softball
==> ball is the superclass of basketball, football, softball
and tennis ball.
==> basketball, football, softball and tennis ball are the
subclass of ball.
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
tennis ball
Child class
(subclass)
What is Inheritance?
ball
Kind-of
football
attributes
attributes
size
size
color
color
methods
methods
throw
throw
catch
catch
ball
attributes
attributes
size
size
color
color
methods
methods
throw
throw
catch
catch
kick
kick
football
Subclass inherits state and methods from the superclass.
(for eg. states : size, color; methods : throw, catch
Subclasses are not limited by the states and methods provided by the superclass. Subclasses
can add own variables and methods. (for eg. Football can add in a method : kick)
Subclasses can also override inherited methods and provide specialized implementations for
those methods. (for eg. Football can override the method throw by its own implementation)
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
What is Inheritance?
To define a subclass of ball, the keyword extends is used
Java programming language.
Example:
class football extends ball {
}
football inherits the members of its superclass,
ball
Creating a subclass from one and only one
super class is termed as single level inheritance.
Java supports only single level inheritance
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Sample program using Inheritance
Example 1 :
public class Greetings {
public void greet(String name) {
System.out.println("Welcome To " + name + "'s Web Site");
}
Greetings
Class
class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings();
welcome.greet(God");
}
}
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
TestGreetings
class
Sample program using Inheritance
Subclassing &
Inheritance
public class GreetingsSub extends Greetings {
public void sayGoodBye(String name) {
System.out.println(Sayonara, " + name + ". Visit us
again next time !!!");
}
}
GreetingsSub
class
(subclass of
Greetings)
class TestGreetings2 {
public static void main(String[] args) {
GreetingsSub welcome = new GreetingsSub();
welcome.greet(God");
welcome.sayGoodBye(Satan);
}
}
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
TestGreetings2
class
Sample program using Inheritance
Example 2 :
public class Greetings {
String salutation;
Greetings(String s) {
salutation=s;
}
public void greet(String name) {
System.out.println(salutation + "Welcome To " + name + "'s Web Site");
}
Greetings
class
class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings("Hello,");
welcome.greet(God");
}
}
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
TestGreetings
class
Constructors
Constructor is a special method which is having
the same name of the class.
Constructors are using for creating Objects as
well as to initialize the variables specified in the
class.
Constructors does not return any value at all.
Multiple constructors are possible for a single
class.
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Constructors
class Book
Book class has a
{
constructor
// state
that initializes pages
protected int pages;
// constructor
public Book(int num_pages)
{
pages = num_pages;
}
public void page_message()
{
System.out.println("Number of pages: " + pages);
} // method page_message
} // class Book
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Constructors
Dictionary inherits
variable pages from
Book
class Dictionary extends Book {
// state
private int definitions;
// constructor
public Dictionary(int num_pages, int num_definitions)
{
super(num_pages);
definitions = num_definitions;
}
public void definition_message()
{
System.out.println ("Number of definitions: " +
definitions);
} // method definition_message
} // class Dictionary
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Constructors
public class Words
{
public static void main(String[] args)
{
Dictionary webster = new Dictionary(1500,52500);
webster.page_message();
webster.definition_message();
} // method main
} // class Words
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Sample program using Inheritance
Subclassing & Inheritance
public class GreetingsSub extends Greetings {
GreetingsSub(String s) {
salutation=s;
}
public void sayGoodBye(String name) {
System.out.println(Sayonara, " + name + ". Visit us again next time !!!");
}
GreetingsSub
class
class TestGreetings2 {
public static void main(String[] args) {
GreetingsSub welcome = new GreetingsSub(Hello );
welcome.greet(God");
welcome.sayGoodBye(Satan);
}
}
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
TestGreetings2
class
Constructors
constructors are not inherited
Book constructor cannot be invoked directly in
Dictionary
super keyword is used to invoke a constructor of
the super class
super reference is used in the Dictionary
constructor to invoke the constructor of the Book
class, passing in the initial value of the pages
variable to be initialized. Dictionary constructor
then proceeds to initialize its own variable,
definitions
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Quick Review Question
What is inheritance?
What is super class?
What is subclass?
List the keyword in Java for inheritance
What is a constructor?
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Follow Up Assignment
Create a class named Room with two integer variables such as
length and breadth followed by a method which calculates and
returns the area (product of length and breadth ). Variables must be
initialized with a constructor.
Create a Bedroom class as a subclass of Room class and add
height as a new variable with data type as integer and volume as a
method which returns the area of Bedroom by multiplying
length,breadth and height. Subclass constructor must be there to
initialize the values for variables.
Create the main class and then create an object of the subclass and
display the area and volume by accessing both super and subclass
methods.
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Summary of Main Teaching Points
Inheritance is the process of creating a subclass
from a super class.
Inheritance is using for code reusability.
Inheritance got many flavours while java supports
single level inheritance.
Constructors are special methods.
Superclass constructors can invoke from a
subclass constructor by using the keyword super
in Java.
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance
Next Lesson
Consolidation of the Module
Question and Answers
Presentations
CT010-3-1 Fundamentals of Software Development
Introduction to Inheritance