Chapter 8.1

14
DEFINING CLASSES FOR INHERITANCE Chapter 8.1:

Transcript of Chapter 8.1

Page 1: Chapter 8.1

DEFINING CLASSES FOR

INHERITANCE

Chapter 8.1:

Page 2: Chapter 8.1

INHERITANCE: What is

Inheritance?

Inheritance let us to create a new class from theexisting classes.

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

The subclass inherits the properties of thesuperclass.

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)

Page 3: Chapter 8.1

INHERITANCE: Example

The class Circle and Rectangle are derived from Shape and the class Box is derived from Rectangle.

Every Circle and every Rectangle is Shape and every Box is a Rectangle

Page 4: Chapter 8.1

INHERITANCE: 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.

Page 5: Chapter 8.1

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

Page 6: Chapter 8.1

Geometry

Circle Triangle Square

Sphere Cone Cylinder Cubes

Single Inheritance

Multiple Inheritance

Page 7: Chapter 8.1

INHERITANCE:

Properties/Characteristics?

Involve 2 classes : Super class.

Sub class

Rectangle

Box

Super class

Sub class

Page 8: Chapter 8.1

INHERITANCE: Super class and

Sub class

Rectangle

double length

double width

Box

double height

Super class – Rectangle

Sub class – Box

Attributes for Rectangle : length, width

Attribute for Box : height

Box class inherits the length and width of the

Rectangle class (superclass)

Page 9: Chapter 8.1

INHERITANCE: keyword extends

extends is the keyword to implement inheritancein Java.

Syntax

class SubClassName extends SuperClassName

{

// properties & methods}

E.g.

class Box extends Rectangle

{

// properties and coding}

Rectangle

Box

Page 10: Chapter 8.1

E.g. : Rectangle classpublic class Rectangle

{

private double length;

private double width;

public Rectangle()

{

length = 0;

width = 0;

}

public Rectangle(double L, double W)

{

setDimension(L,W);

}

public void setDimension(double L, double W)

{

if (L >= 0)

length = L;

else

length = 0;

if (W >= 0)

width = W;

else

width = 0;

}

public double getLength()

{

return length;

}

public double getWidth()

{

return width;

}

public double area()

{

return length * width;

}

public void print()

{

System.out.print(“length = “ + length);

System.out.print(“width = “ + width);

}

} // end for Rectangle class

Page 11: Chapter 8.1

Rectangle

- length : double

- width : double

+ Rectangle()

+ Rectangle(double,double)

+ setDimension(double,double) : void

+ getLength() : double

+ getWidth() : double

+ area() : double

+ print() : void

The class Rectangle has 9 members

UML class diagram of the Rectangle class

Page 12: Chapter 8.1

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)

{

setDimension(L,W);

if (H >= 0)

height = H;

else

height = 0;

}

public double getHeight()

{

return height;

}

public double area()

{

return 2 * (getLength() * getWidth()

+ getLength() * height

+ getWidth() * height);

}

public double volume()

{

return super.area() * height;

}

public void print()

{

super.print();

system.out.print(“height = “ + height);

}

} // end for class Box extends

Page 13: Chapter 8.1

UML class diagram of the class Box

Box

- height : double

- length : double (can’t access directly)

- width : double (cant’ access directly)

+ Box()

+ Box(double,double)

+ setDimension(double,double) : void

+ setDimension(double,double,double) : void

(overloads parent’s setDimension())

+ getLength() : double

+ getWidth() : double

+ getHeight() : double

+ area() : double (overrides parent’s area())

+ volume() : double

+ print() : void (overrides parent’s print())

The class Box has 13 members

Page 14: Chapter 8.1

Declaring Arrays and Accessing Array Components

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 to call the parent’s print() method.

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