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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance and Access Control CS 162 (Summer 2009)
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Inheritance.
Interfaces Professor Evan Korth.
Chapter 9 Inheritance and Polymorphism
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

Superclasses and Subclasses

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

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:

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

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

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

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

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.

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.

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 = ; final static double PI = ; The final method cannot be modified by its subclasses. The final method cannot be modified by its subclasses.

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

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;

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;

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

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

Class Inheritance Hierarchy

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

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

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.

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

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

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

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

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

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.

Example 7.9 (cont.) TestSortCircleCylinderRun

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.

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.