Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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.
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
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
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
Introduction to Inheritance
What is Inheritance?
To define a subclass of ball, the keyword extends is used
Java programming language.
Example:
Introduction to Inheritance
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
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
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.
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
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
Introduction to Inheritance
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
Introduction to Inheritance
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.
Introduction to Inheritance
Introduction to Inheritance
Next Lesson
Consolidation of the Module
Question and Answers
Presentations
Introduction to Inheritance