You are on page 1of 41

Advance

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

Advantages and disadvantages of Inheritance


4

INHERITANCE
What is Inheritance?

A mechanism that allowing us to extend the definition of a class


without making any physical changes to the class.

Inheritance let us to create a new class from the existing classes.

The new class is called subclass and the existing class is called
superclass.

The subclass inherits the properties of the superclass.

The properties refer to the method or the attribute (data)


Inheritance is is-a relation

Example : if a circle inherits the shape class, hence the circle is a


shape. (see the next figure)

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

Super class Rectangle


Sub class Cubes
Attributes for rectangle : length, width
Attribute for Cubes : height

Cubes class inherits the length


and width of the rectangle class
(superclass)

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
}

E.g. : Rectangle class


public class Rectangle
{
private double lenght;
private double width;

public double getLength()


{
return length;
}

public Rectangle()
{
length = 0;
width = 0;
}

public double getWidth()


{
return width;
}

public Rectangle(double L. double W)


{
setDimension(L,W);
}
public void setDimension(double L. double W)
{
if (L >= 0)
length = 1;
else
length = 0;

public double area()


{
return length * width;
}
public void print()
{
system.out.print(length = + length);
system.out.print(width = + width);
}
} // end for Rectangle class

if (W >= 0)
width = w;
else
width = 0;
}

15

The class Rectangle has 10 members


Rectangle
- length : double
- width : double
+ Rectangle()
+ Rectangle(double,double)
+ setDimension(double,double) : void
+ getLength() : double
+ getWidth() : double
+ area() : double
+ perimeter() : double
+ print() : void

UML class diagram of the Rectangle class


16

E.g. : Box class


public class Box extends Rectangle
{
private double height;
public Box()
{
super();
height = 0;
}
public Box(double L. double W, Double H)
{
super(L,W);
height = H;
}
public void setDimension(double L. double W, Double H)
{
if (H >= 0)
height = h;
else
height = 0;
}

public double getHeight()


{
return height;
}
public double area()
{
return 2 * (getLength() * getWidth()
+ getLength() * height
+ getWidth(0 * height);
}
public double volume()
{
return super.area * height()
}
public void print()
{
super.print();
system.out.print(height = + height);
}
} // end for class Box extends
17

The class Box has 7 members


Box
- height : double
+ Box()
+ Box(double,double)
+ setDimension(double,double) : void
+ getHeight() : double
+ area() : double
+ volume() : double
+ print() : void

UML class diagram of the class Box

18

The class Box is derived from the class Rectangle


Therefore, all public members of Rectangle are public members of Box
The class Box overrides the method print and area
The class Box has 3 data members : length, width and height
The instance variable length and width are private members of the class
Rectangle
So, it cannot be directly accessed in class Box
Use the super keyword to call a method of the superclass.
To print the length and width, a reserve word super should be put into the print
method of class Box.
The same case happen in the volume method where super.area is being used to
determine the base area of box.
The method area of the class Box determines the surface area of the box.
To retrieve the length and width, the methods getLength and getWidth in class
Rectangle is being used.
Here, the reserve word super is not used because the class Box does not override
the methods getLength and getWidth
19

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

Refer to the previous slide


Super class contains 2 classes : Subclass A and
Subclass B.
Subclass A is defined in same package with superclass
since subclass B is defined outside the package.
There are 4 accessibility data types: public, protected,
private and by default.
Subclass A can inherit all properties of superclass
except private.
But, subclass B only inherit the properties outside the
package which are public and protected.
23

Example: Visibility Modifiers


package p1;
public class C1 {
public int x;
protected int y;
int z;
private int u;

protected void m() {


}

public class C2 {
C1 o = new C1();
can access o.x;
can access o.y;
can access o.z;
cannot access o.u;
}

can invoke o.m();

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;

can invoke m();


}

public class C5 {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
cannot access o.u;

can invoke m();


}

cannot invoke o.m();

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);
}

private int m must be


replaced to
protected int m to
avoid error message

25

INHERITANCE
E.g Accessibility Modifier : Private
public class Geometry {
private int height = 10;
public int getHeight ()
return height;
}
} // Geometry

Geometry
{

public class Triangle extends Geometry


public int base, area;
public void calArea() {
area = base * getHeight();
}
}// Triangle

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

public class Test(){


public static void main(String a[])
{
Cubes kb = new Cubes();
Rectangle sE = new Rectangle();
S.O.P(kb.length + kb.width);
S.O.P(sE.AreaOfRectangle());
S.O.P(kb. AreaOfCubes());
kb.Display();

public class Cubes extends Rectangle


{ double height = 10.0;

sE.Display();
}}

public double AreaOfCubes() {


return AreaOfRectangle() * height;
}
length
public void Display(){
System.out.println(Length= + getLength() +
width = + width); }
} //Cubes

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

Constructors are not inherited.


Each class must define its own constructors.
A reserved word super is used.
Example 1 : the definition of the constructor
for the class Box (without parameter)
public Box
{
super();
height = 0;
}
30

Example 2 : the definition of the constructor for


the class Box (with three parameters)
A reserve word super is used with the
appropriate parameters
A call to the constructor of the superclass
must be in the first statement.
public Box(double l, double w, double h)
{
super(l,w);
height = h;
}

31

Consider the following statements.


Rectangle myRectangle = new Rectangle(5,3);
Box myBox = new Box(6,5,4);

Statement in first line creates the Rectangle object


myRectangle.
Thus, the object Rectangle has two instance
variables: length and width.
The statement in second line creates the Box object
myBox with three instance variable; length, width and
height.
See the next figure;
32

Rectangle myRectangle = new Rectangle(5, 3);


Box myBox = new Box(6, 5, 4);

33

Consider the following statements.


myRectangle.print();
System.out.println();
myBox.print();
System.out.print()

The output for the first statement is:


length = 5.0; width = 3.0
The output for the third statement is:
length = 6.0; width = 5.0; height = 4.0

34

INHERITANCE
Example: super
public class Rectangle {
protected double length = 5.0;
protected double width = 4.0;
public double RectangleArea ()
return length * width;
}
} //Rectangle

public class Cubes extends Rectangle {


private double height = 10.0;
public void PrintArea() {
System.out.println(Cubes
System.out.println(Cubes
System.out.println(Cubes
System.out.println(Cubes
}
} //Cubes

length = + super. length);


width = + super. width);
height = + height);
Area = + super.RectangleArea()* height;
35

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

You might also like