You are on page 1of 14

Inheritance

Session 8
Course : M0864 - Programming I
Year : September 2011
Bina Nusantara
Learning Outcomes
At the end of this session, the students will be
able to :
Apply the concept of inheritance on GUI in
Java
Bina Nusantara
Lecture Outline
Inheritance (extends)
Overriding VS Overloading.
Inheritance Components


Inheritance
Inheritence from superclass to subclass.

Subclass inherits and given the right to acces data
member ( fields/variables, methods, and nested classes
) that are existed in the Superclass except constructor.

Able to add new data member on Subclass.

Use extends to access the parent class.


Bina Nusantara
Inheritance
Bina Nusantara
A Subclass inherits all access to public and
protected from its Superclass, the package of
subclass is not a concern.

If a subclass has the same package as
superclass then subclass will inherits member
package-private from its superclass as well.

Is not allowed to receive multiple inheritance
from more than one parents.
Inheritance
In Java GUI programming, all class is definitely
inheritance, usually are inheritance from JFrame




GUI classes in Java can be modified.
Bina Nusantara
Overriding VS Overloading
Overriding :
Subclass modifies method that has been defined by superclass.
Function that is declared by subclass should have the same
signature(name, numbers and data types of parameters) and
return value method with the existing one in the superclass
Overloading :
More than one functions that have the same name with the
existing one in the superclass.
Function that is declared in subclass does not have to have the
same signature (name, numbers and data types of parameter)
and return value method with the existing one in the superclass

Bina Nusantara
Inherite Components
Self-create class that is an inheritance from one
component and contains of other GUI components

Generally are inheritance from class Container (JFrame,
JPanel and JApplet) that is most often to be inherited.

Usually is used to create a user control component.

Bina Nusantara
Inherite Components
Bina Nusantara
Reusability class.

In designing User Interface (UI) Form, organize UI
components that have the functionality in 1 group in a
class that is handled by a Container class such as
JPanel.

Easy to modify screen design.
Inherite Components
Sample :

Bina Nusantara
MyInformation class is a subclass of
JPanel and consists of components to
create a user control
Inherite Components
Sample in the caller class :
Bina Nusantara
Inherite Components
Output
Bina Nusantara
Is an object of
Class MyInformation
Bina Nusantara
Reference
Introduction to java programming comprehensive version, Chapter
10, Page 357
Software Java Solutions, Chapter 8, Page 467
http://home.cogeco.ca/~ve3ll/jatutor5.htm
http://download.oracle.com/javase/tutorial/java/IandI/subclasses.htm
l
http://download.oracle.com/javase/tutorial/java/concepts/interface.ht
ml

You might also like