Upcoming SlideShare
×

# Chapter 8.1

206 views

Published on

Published in: Sports, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
206
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Chapter 8.1

1. 1. DEFINING CLASSES FOR INHERITANCE Chapter 8.1:
2. 2. INHERITANCE: What is Inheritance?  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)
3. 3. 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
4. 4. 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.
5. 5. 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
6. 6. Geometry Circle Triangle Square Sphere Cone Cylinder Cubes Single Inheritance Multiple Inheritance
7. 7. INHERITANCE: Properties/Characteristics?  Involve 2 classes :  Super class.  Sub class Rectangle Box Super class Sub class
8. 8. 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)
9. 9. INHERITANCE: keyword extends  extends is the keyword to implement inheritance in Java.  Syntax class SubClassName extends SuperClassName { // properties & methods }  E.g. class Box extends Rectangle { // properties and coding } Rectangle Box
10. 10. E.g. : Rectangle class public 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
11. 11. 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
12. 12. 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
13. 13. 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
14. 14.  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