Professional Documents
Culture Documents
Programming
INHERITANCE
Focus
To explain inheritance concept,
To apply inheritance concept in
programming.
To list the types of accessibility
modifier
INHERITANCE
Contents
Introduction
Types of Inheritance.
Inheritance properties
Accessibility Modifier in Inheritance.
private
protected
public
default
Keyword Inheritance.
extends
super
INHERITANCE
What is Inheritance?
The new class is called subclass and the existing class is called
superclass.
INHERITANCE
Example
Square
The class Circle and Rectangle are derived from Shape and the
class Square is derived from Rectangle.
Every Circle and every Rectangle is Shape and every Square is 6
Rectangle
INHERITENCE
When it is used?
If we need a new class with a same
properties or method of the current
class, we do not need to define a new
class.
A new class might be inherited from the
current class.
7
INHERITANCE
Types of Inheritances?
Single inheritance
Is a subclass that derived from a single/one superclass
(existing class)
Multiple inheritance
Is a subclass that derived from more than one superclass
Not supported by Java
Geometry
Single Inheritance
Circle
Triangle
Square
Multiple Inheritance
Sphere
Cone
Cylinder
Cubes
9
INHERITANCE
Properties/Characteristics?
Involve 2 classes :
Super class.
Sub class
Rectangle
Cubes
Super class
Sub class
10
INHERITANCE
Super class and Sub class
Rectangle
double length
double width
Cubes
double height
11
INHERITANCE
Cubes
12
INHERITANCE
Hierarchy of Java Stream Classes
13
INHERITANCE
extends keyword
extends is a general syntax to implement the inheritance
concept.
Syntax
class SubClassName extends SuperClassName
{
Rectangle
// properties & methods
}
E.g.
Cubes
class Cubes extends Rectangle
{
// properties and coding
14
}
public Rectangle()
{
length = 0;
width = 0;
}
if (W >= 0)
width = w;
else
width = 0;
}
15
18
INHERITANCE
(Accessibility Modifier)
Sometimes , it is called visibility modifier
Not all properties can be accessed by sub class.
Super class can control a data accessing from
subclass by giving the type of accessing to the
members and methods.
A class can declare the data members or method
as a public, private or protected.
If it is not declared, the data or method will be set
to default type.
20
INHERITANCE
Data Accessibility
Accessibility modifier for members in Super class
Sub class
in same
package
public
default
protected
private
can access
can access
can access
cannot
access
cannot
access
can access
cannot
access
Sub class
in different can access
package
21
INHERITANCE
Data Accessibility
Super class
Package B
int a
public int b
protected int c
private int d
Sub class B
Sub class A
public int b
protected int c
int a
public int b
protected int c
Package A
22
public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
}
package p2;
public class C3
extends C1 {
can access x;
can access y;
can access z;
cannot access u;
}
public class C4
extends C1 {
can access x;
can access y;
cannot access z;
cannot access u;
public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;
24
Example:
Accessibility
modifier
class ClassX
{
private int m;
public String toString()
{
return new String("(" + m + ")");
}
}
public class ClassY extends ClassX
{
private int n;
public String toString()
{
return new String("(" + m + " , " + n + ")");
} // ERROR : mhasprivateaccessin
ClassX
}
class TestAccesibility
{
public static void main(String [] args)
{
ClassX x = new ClassX;
ClassY y = new ClassY;
System.out.println("x = " + x);
System.out.println("y = " + y);
}
25
INHERITANCE
E.g Accessibility Modifier : Private
public class Geometry {
private int height = 10;
public int getHeight ()
return height;
}
} // Geometry
Geometry
{
Triangle
27
INHERITANCE
Example: Inheritance
public class Rectangle {
private double length = 5.0;
protected double width = 4.0;
public double AreaOfRectangle(){
return length * width;
}
public double getLength() {
return length; }
} //Rectangle
sE.Display();
}}
28
INHERITANCE
Keyword super
Used in two ways
a) to call a method of the Superclass
b) to call a superclass constructor
E.g :
super( ) - referring to the Superclass constructor.
super.methodName() refer to a method in the
Superclass
super.VariableName refer to a variable (data) in the
Superclass.
29
INHERITANCE
Constructor of Superclass and Subclass
31
33
34
INHERITANCE
Example: super
public class Rectangle {
protected double length = 5.0;
protected double width = 4.0;
public double RectangleArea ()
return length * width;
}
} //Rectangle
Example: Inheritance
public class Rectangle {
private int width, height;
public Rectangle (int wdth, int hgth){
width = wdth;
height = hgth;
}
public void Display() {
for ( int row = 0; row < height; row ++) {
for (int column = 0; column < height; lajur++)
{
System.out.print (*);
System.out.println();
}
}
}
36
Continue
public class ExtRectangle extends Rectangle {
private String label;
public ExtRectangle (int width, int hgth, String lbl){
super (wdth, height); // call super class constructor
label = lbl;
}
// print rectangle shape using Draw(0 method that inherit from superclass
public void Draw() {
super.Draw();
System.out.println(label);
}
} // ExtRectangle
37
Continue
class Application {
public static void main(String [ ] args) {
Rectangle Rec = new Rectangle (4,10);
ExtRectangle ExtRec = new ExtRectangle(4,10,Box)
Rec.Draw();
System.out.println();
ExtRec.Draw();
}
} //Application
38
Output
**********
**********
**********
**********
**********
**********
**********
**********
Box
39
Advantages of Inheritance
A sub class can inherit a properties from
super class.
A sub class can add/edit inherited
properties
Programmer can reuse the code from super
class to create a new class/sub class.
Save your time during process
development system/applications.
40
Disadvantages of
Inheritance
Inheritance can impair encapsulation
The changes in super class implementation
will effect each inherited sub classes
41
References
Liang, D. (2005). Introduction to Java programming (5th ed.).
New Jersey: Prentice Hall.
Malik, D. S. & Nair. (2003). Java programming: From problem
analysis to program design. United States:Thomson Course
Technology.
42