Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
Advertisements

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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
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.
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.
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Outline §Abstract class and interface §Inner classes and initialization blocks §Equality §Exception §Threads.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
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
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
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)
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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
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.
Chapter 15 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 8 Classes and Objects
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 13 Abstract Classes and Interfaces
Chapter 9 Inheritance and Polymorphism
Chapter 9 Inheritance and Polymorphism
Chapter 12 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected, final and abstract F Abstract classes F Polymorphism and Object Casting F Interfaces F Inner Classes

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 8.1 Testing Inheritance  Objective: Create a Cylinder object and explore the relationship between the Cylinder and Circle classes. TestCylinderRun

Using the Keyword super F To call a superclass constructor F 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:

CAUTION You must use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. Java requires that the statement that uses the keyword super appear first in the constructor.

NOTE A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's default constructor is automatically invoked.

Example on Calling Superclass’s Constructors public class C1 extends C2 { public static void main(String[] args) { new C1(); } public C1() { System.out.println("C1's default constructor is invoked"); } class C2 extends C3 { public C2() { System.out.println("C2's default constructor is invoked"); } class C3 { public C3() { System.out.println("C3's default constructor is invoked"); }

NOTE If a superclass defines constructors other than a default constructor, the subclass cannot use the default constructor of the superclass, because in this case the superclass does not have a default constructor.

Example on the Impact of a Superclass with no Default Constructor public class A extends B { } class B { public B(String name) { System.out.println("B's constructor is invoked"); }

Example 8.2 Overriding Methods in the Superclass The Cylinder class overrides the findArea() method defined in the Circle class. TestOverrideMethod Run Cylinder

NOTE An instance method can be overridden only if it is accessible. Thus a private method cannot be overridden, because it is not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

NOTE Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

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

The Object Class, cont. The equals() method compares the contents of two objects. The default implementation of the equals method in the Object class is as follows: public boolean equals(Object obj) { return (this == obj); }

NOTE The == comparison operator is used for comparing two primitive data type values or for determining whether two objects have the same references. The equals method is intended to test whether two objects have the same contents, provided that the method is modified in the defining class of the objects. The == operator is stronger than the equals method, in that the == operator checks whether the two reference variables refer to the same object.

The Object Class, cont.  The toString() method returns a string representation of the object. The default implementation returns a string consisting of a class name of which the object is an instance, the at sign and a number representing this object. The clone() method copy objects

The Object Class, cont. To create a new object with separate memory space, you need to use the clone() method, as follows: newObject = someObject.clone(); NOTE: Not all objects can be cloned. For an object to be cloneable, its class must implement the java.lang.Cloneable interface. Interfaces are introduced in the section "Interfaces," in this chapter.

The protected Modifier  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.

The protected Modifier, cont.

NOTE A subclass may override a protected method in its superclass and change its visibility to public. However, a subclass cannot weaken the accessibility of a method defined in the superclass. For example, if a method is defined as public in the superclass, it must be defined as public in the subclass.

NOTE The modifiers are used on classes and class members (data and methods), except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.

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

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

Abstract Classes GeometricObjectCircleCylinderRectangle

NOTE An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass.

NOTE An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class.

NOTE A class that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass.

NOTE A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract.

NOTE A subclass can override a method from its superclass to declare it abstract. This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be declared abstract.

NOTE You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct. GeometricObject[] geo = new GeometricObject[10];

NOTE Cylinder inherits the findPerimeter method from Circle. If you invoke this method on a Cylinder object, the perimeter of a circle is returned. This method is not useful for Cylinder objects. It would be nice to remove or disable it from Cylinder, but there is no good way to get rid of this method in a subclass once it is defined as public in its superclass. If you define the findPerimeter method abstract in the Cylinder class, then the Cylinder class must be declared abstract.

Polymorphism, Dynamic Binding and Generic Programming Consider the following code: Test

Polymorphism Method m (Line 9) takes a parameter of the Object type. You can invoke m with any objects (e.g. new A(), new B(), new C(), and new Object())in Lines (3-6). An object of a subclass can be used by any code designed to work with an object of its superclass. This feature is known as polymorphism (from a Greek word meaning “many forms”).

Dynamic Binding When the method m is executed, the argument x’s toString method is invoked. x may be an instance of A, B, C, or Object. Classes A, B, C, and Object have their own implementation of the toString method. Which implementation is used will be determined dynamically by the Java Virtual Machine at runtime. This capability is known as dynamic binding.

Dynamic Binding, cont. Dynamic binding works as follows: Suppose an object o is an instance of classes C 1, C 2,..., C n-1, and C n, where C 1 is a subclass of C 2, C 2 is a subclass of C 3,..., and C n-1 is a subclass of C n. That is, C n is the most general class, and C 1 is the most specific class. In Java, C n is the Object class. If o invokes a method p, the JVM searches the implementation for the method p in C 1, C 2,..., C n-1 and C n, in this order, until it is found. Once an implementation is found, the search stops and the first-found implementation is invoked.

Generic Programming Polymorphism allows methods to be used generically for a wide range of object arguments. This is known as generic programming. If a method’s parameter type is a superclass (e.g., GeometricObject), you may pass an object to this method of any of the parameter’s subclasses (e.g., Circle or Rectangle). When an object (e.g., a Circle object or a Rectangle object) is used in the method, the particular implementation of the method of the object that is invoked (e.g., findArea) is determined dynamically.

Example 8.3 Testing Polymorphism F 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

NOTE Matching a method signature and binding a method implementation are two issues. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime.

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 is equivalent to 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;... }

Example 8.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. TestCastingRun

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

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

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

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

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

Example 8.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 8.6 Cloning Objects  Objective: uses the Cloneable interface to mark classes cloneable and uses the clone method to copy objects. TestCloneable Run

Example 8.6 Cloning Objects, cont.

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. F 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.) F 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.” F An inner class is only for supporting the work of its containing outer class, and it cannot be used by other classes.

Initialization Block Initialization blocks can be used to initialize objects along with the constructors. An initialization block is a block of statements enclosed inside a pair of braces. An initialization block appears within the class declaration, but not inside methods or constructors. It is executed as if it were placed at the beginning of every constructor in the class.

Initialization Block public class Book { { numOfObjects++; }

Static Initialization Block A static initialization block is much like a nonstatic initialization block except that it is declared static, can only refer to static members of the class, and is invoked when the class is loaded. The Java runtime system loads a class when it is needed. A superclass is loaded before its subclasses.

Static Initialization Block class A extends B { static { System.out.println("A's static initialization block is invoked"); } class B { static { System.out.println("B's static initialization block is invoked"); }