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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Lecture 5: Interfaces.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
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, 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.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 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.
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
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
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.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance.
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Chapter 8 Classes and Objects
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Inheritance and Polymorphism
Chapter 12 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract F Polymorphism and Object Casting F Interfaces F Inner Classes F Program Development and Class Design

Superclasses and Subclasses UML Diagram

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

Example 6.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

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 6.2 Overriding Methods in the Superclass The Cylinder class overrides the findArea() method defined in the Circle class. The Cylinder class overrides the findArea() method defined in the Circle class. TestModifyingMethods Run

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 clone() method copy objects The clone() method copy 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 protected Modifier, cont.

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

Abstract Classes GeometricObject Circle Cylinder Rectangle

Polymorphism and Dynamic Binding Polymorphism refers to the ability to determine at runtime which code to run, given multiple methods with the same name but different operations in the same class or in different classes. This ability is also known as dynamic binding.

Example 6.3 Testing Polymorphism Objective: This example creates two geometric objects: a circle, and a rectangle, invokes the equalArea method to check if the two objects have equal area, and invokes the displayGeometricObject method to display the objects. Objective: This example creates two geometric objects: a circle, and a rectangle, invokes the equalArea method to check if the two objects have equal area, and invokes the displayGeometricObject method to display the objects. TestPolymorphism Run

Casting Objects It is always possible to convert a subclass to a superclass. 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. 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 6.4 Casting Objects This example creates two geometric objects: a circle, and a cylinder, invokes the displayGeometricObject method to display the objects. The displayGeometricObject displays the area and perimeter if the object is a circle, and displays area and volume if the object is a cylinder. This example creates two geometric objects: a circle, and a cylinder, invokes the displayGeometricObject method to display the objects. The displayGeometricObject displays the area and perimeter if the object is a circle, and displays area and volume if the object is a cylinder. TestCastingRun

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

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

Example of Creating an Interface // This interface is defined in // java. lang package public interface Comparable { public int compareTo(Object o); public int compareTo(Object o);}

Generic max Method public class Max { // Return the maximum between two objects // Return the maximum between two objects public static Comparable max (Comparable o1, Comparable o2) { public static Comparable max (Comparable o1, Comparable o2) { if (o1.compareTo(o2) > 0) if (o1.compareTo(o2) > 0) return o1; return o1; else else return o2; return o2; } }

Example 6.5 Using Interfaces Objective: Use the max method to find a maximum circle between two circles and a maximum cylinder between two cylinders. Objective: Use the max method to find a maximum circle between two circles and a maximum cylinder between two cylinders.

Example 6.5, cont. TestInterface Run

Interfaces vs. Abstract Classes In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods. An abstract class must contain at least one abstract method or inherit from another abstract method.

Interfaces vs. Abstract Classes, cont. Since all the methods defined in an interface are abstract methods, Java does not require you to put the abstract modifier in the methods in an interface, but you must put the abstract modifier before an abstract method in an abstract class.

Interfaces vs. Abstract Classes, cont.

The Cloneable Interfaces public interface Cloneable { } Marker Interface: An empty interface. A marker interface does not contain constants or methods, but it has a special meaning to the Java system. The Java system requires a class to implement the Cloneable interface to become cloneable.

Example 6.6 Cloning Objects Objective: uses the Cloneable interface to mark classes cloneable and uses the clone method to copy objects. Objective: uses the Cloneable interface to mark classes cloneable and uses the clone method to copy objects. TestCloneable Run

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. ShowInnerClass

Inner Classes (cont.) 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.” 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.” 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.

Software Development Process

Class Design Guidelines Hide private data and private methods. Hide private data and private methods. A property that is shared by all the instances of the class should be declared as a class property. A property that is shared by all the instances of the class should be declared as a class property. Provide a public default constructor and override the equals method and the toString method defined in the Object class whenever possible. Provide a public default constructor and override the equals method and the toString method defined in the Object class whenever possible.

Class Design Guidelines, cont. 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.