Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.

Similar presentations


Presentation on theme: "Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final."— Presentation transcript:

1 Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final and abstract F Casting Objects F Numeric Wrapper Classes F Interfaces F Inner Classes F Class Design Guidelines

2 Superclasses and Subclasses

3 Creating a Subclass Creating a subclass extends properties and methods from the superclass. You can also: F Add new properties F Add new methods F Override the methods of the superclass Cylinder Class

4 Using the Keyword super To call a superclass constructor To call a superclass constructor To call a superclass method To call a superclass method The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways:

5 Example 7.1 Testing Inheritance Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes. Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes. TestCylinderRun

6 Example 7.3 Overriding Methods in the Superclass The Cylinder class overrides the findArea() method defined in the Circle class. Test Modifying MethodsRun

7 The Keyword this The keyword this refers to the current object. The keyword this refers to the current object. The keyword can be used in two ways: The keyword can be used in two ways: F To call a class constructor F To pass the current class as an argument to a method

8 Example of Using this class Circle class Circle { private double radius; { private double radius; Circle(double radius) Circle(double radius) { this.radius = radius; this.radius = radius; } Circle() Circle() { this(1.0); this(1.0); } public double findArea() public double findArea() { return radius*radius*Math.PI; return radius*radius*Math.PI; } }

9 The Object Class The Object class is the root of all Java classes. The Object class is the root of all Java classes. The toString() method returns a string representation of the object. The toString() method returns a string representation of the object. The equals() method compares the contents of two objects. The equals() method compares the contents of two objects.

10 The protected Modifier The protected modifier can be applied on data and methods in a class. The protected modifier can be applied on data and methods in a class. A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package. A protected data or a protected method in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in a different package.

11 The final Modifier The final class cannot be extended: The final class cannot be extended: final class Math {...} final class Math {...} The final variable is a constant: The final variable is a constant: final static double PI = 3.14159; final static double PI = 3.14159; The final method cannot be modified by its subclasses. The final method cannot be modified by its subclasses.

12 The abstract Modifier The abstract class The abstract class  Cannot be instantiated  Should be extended and implemented in subclasses The abstract method The abstract method  Method signature without implementation

13 Casting Objects It is always possible to convert a subclass to a superclass. It is always possible to convert a subclass to a superclass. For this reason, explicit casting can be omitted. For example, For this reason, explicit casting can be omitted. For example, Circle myCircle = myCylinder Circle myCircle = myCylinder is equivalent to Circle myCircle = (Circle)myCylinder; Circle myCircle = (Circle)myCylinder;

14 Casting from Superclass to Subclass Explicit casting must be used when casting an object from a superclass to a subclass. Explicit casting must be used when casting an object from a superclass to a subclass. This type of casting may not always succeed. This type of casting may not always succeed. Cylinder myCylinder = (Cylinder)myCircle;

15 The instanceof Operator Use the instanceof operator to test whether an object is an instance of a class: Circle myCircle = new Circle(); if (myCircle instanceof Cylinder) { Cylinder myCylinder = (Cylinder)myCircle; Cylinder myCylinder = (Cylinder)myCircle;......}

16 Example 7.4 Casting Objects Objective: Use implicit casting to assign circles and cylinders to an array and then use explicit casting to access data and methods in the objects when processing the array. Objective: Use implicit casting to assign circles and cylinders to an array and then use explicit casting to access data and methods in the objects when processing the array. TestCastingRun

17 Class Inheritance Hierarchy

18 Numeric Wrapper Classes Boolean Boolean Character Character Short Short Byte Byte Integer Integer Long Long Float Float Double Double

19 The Integer Class and The Double Class Constructors Constructors Class Constants MAX_VALUE, MIN_VALUE Class Constants MAX_VALUE, MIN_VALUE Conversion Methods Conversion Methods

20 Class Design Guidelines Hide private data and private methods. Hide private data and private methods. Choose informative names and follow consistent styles. Choose informative names and follow consistent styles. A class should describe a single entity or a set of similar operations. A class should describe a single entity or a set of similar operations. Group common data fields and operations shared by other classes. Group common data fields and operations shared by other classes.

21 Example 7.5 Designing Abstract Classes Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. Objective: This example gives a generic class for matrix arithmetic. This class implements matrix addition and multiplication common for all types of matrices. GenericMatrix

22 Example 7.6 Extending Abstract Classes Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. Objective: This example gives two programs that utilize the GenericMatrix class for integer matrix arithmetic and rational matrix arithmetic. TestIntegerMatrixRun TestRationalMatrixRun

23 Interfaces What Is an Interface? What Is an Interface? Creating an Interface Creating an Interface Implementing an Interface Implementing an Interface

24 Creating an Interface modifier interface InterfaceName { constants declarations; constants declarations; methods signatures; methods signatures;}

25 Example of Creating an Interface public interface CompareObject { public static final int LESS = -1; public static final int LESS = -1; public static final int EQUAL = 0; public static final int GREATER = 1; public int compare(CompareObject otherObject); }

26 Example 7.7 Using Interfaces Objective: Use the generic sorting method defined in the CompareObject interface to sort an array of circles in increasing order of their radii and an array of cylinders in increasing order of their volumes. Objective: Use the generic sorting method defined in the CompareObject interface to sort an array of circles in increasing order of their radii and an array of cylinders in increasing order of their volumes.

27 Example 7.9 (cont.) TestSortCircleCylinderRun

28 Inner Classes Inner class: A class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple. An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class. An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class.

29 Inner Classes (cont.) Inner classes can make programs simple and concise. As you see, the new class is shorter and leaner. Inner classes can make programs simple and concise. As you see, the new class is shorter and leaner. Many Java development tools use inner classes to generate adapters for handling events. Event-driven programming is introduced in Chapter 8, "Getting Started with Graphics Programming.” Many Java development tools use inner classes to generate adapters for handling events. Event-driven programming is introduced in Chapter 8, "Getting Started with Graphics Programming.” An inner class is only for supporting the work of its containing outer class, and it cannot be used by other classes. An inner class is only for supporting the work of its containing outer class, and it cannot be used by other classes.


Download ppt "Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final."

Similar presentations


Ads by Google