Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance allows you to derive new classes from existing classes known as inheritance

2 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Using Classes Effectively with Polymorphism Polymorphism allows a single variable to refer to objects from different classes. Also, polymorphism denotes the principle that behavior (methods) can vary (be called) depending on the actual type of an object. The actual type of the object determines the method to be called.

3 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods Abstract classes are like regular classes with data and methods, but you cannot create instances (objects) of abstract classes using the new operator. An abstract method cannot be placed in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract..

4 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods A classes that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. A subclass can be abstract even if its superclass is concrete..

5 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods When we define a superclass, we often do not need to create any instances of the superclass. Depending on whether we need to create instances (objects) of the superclass, we must define the class differently. We will study examples based on the Animal and Fruit super classes defined later.

6 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Abstract Superclasses and Abstract Methods An abstract class is a class defined with the modifier abstract. No instances can be created from an abstract class.

7 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface Java interface and inheritance both model an IS-A relationship: class ButtonHandler implements ActionListener Class SavingsAccount extends Account We say “ButtonHandler is an ActionListener” and “SavingsAccount is an Account.”

8 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface However, their uses are very different. The Java interface is used to share common behavior (only method headers) among the instances of different classes. Inheritance is used to share common code (including both data members and methods) among the instances of related classes.

9 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Inheritance versus Interface In your program designs, remember to use the Java interface to share common behavior. Use inheritance to share common code. If an entity A is a specialized form of another entity B, then model them by using inheritance. Declare A as a subclass of B.

10 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces What is an interface? Creating an interface Implementing an Interface What is a Marker interface?

11 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface An interface is a classlike construct. It contains only constants and abstract methods. An interface is treated like a a special class in Java. Each interface is compiled into a separate bytecode file same as a regular class. You cannot create an instance (object) from an interface using the new operator. Multiple classes can implement the same interface type The java.lang.Comparable interface defines the compareTo method. Many classes in the Java library implement Comparable.

12 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 13.6 Define an Interface The data must be constants. Each method in an interface has only a signature without implementation. Syntax: public interface NewInterface { /** Constants declarations */ public static final int CONSTANT_J = 2; /** Method signatures*/ public abstract void AbstractMethod(); }

13 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Define an Interface Java allows only single inheritance for class extensions. Java allows multiple extensions for interfaces. public class SubClassName extends SuperClass implements Interface1, … InterfaceN { }

14 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface Comparable //Interface for comparing objects, defined in java.lang // The compareTo method determines the order of this // object with the specified/ object o, and returns a // negative integer, zero, or a positive integer if this // object is less than, equal to, or greater than the specified // object o package java.lang; public interface Comparable { public int compareTo(Object o); }

15 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interface Comparable // Max.java: Find the maximum object of the //two objects. Note to use this method, you // must first implement the Comparable interface for // the class of these two objects. 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; }

16 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. The Cloneable Interface –Marker Interface An interface contains constants and abstract methods, but the Cloneable interface is a special case: public interface Cloneable { } This interface is empty. An interface with an empty body is known as a marker interface.

17 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. TestEdible (Driver) Animal ElephantTigerChicken > Fruit AppleCherry Orange

18 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Programs to Use Programs are stored on the M: drive in the Interface folder : TestEdible (Driver) Edible (an interface) Animal (super class) Chicken Tiger Elephant Fruit (super class) Apple Orange M:\Courses\OCP\CIS Classes\CIS 225 Advanced Java\FordeFolder\Interface Folder

19 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces vs. Abstract Classes InterfaceAbstract Constant Data Yes(only kind) Yes All Types of Data NoYes Abstract Methods Yes (only)Yes (at least one) Concrete Methods NoYes


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance."

Similar presentations


Ads by Google