Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Carrano Chapter 10 Small Java

Similar presentations


Presentation on theme: "Chapter 9 Carrano Chapter 10 Small Java"— Presentation transcript:

1 Chapter 9 Carrano Chapter 10 Small Java
Advanced Java Topics Chapter 9 Carrano Chapter 10 Small Java © 2006 Pearson Addison-Wesley. All rights reserved

2 Inheritance Revisited
Allows a class to derive the behavior and structure of an existing class © 2006 Pearson Addison-Wesley. All rights reserved

3 Inheritance Revisited
Figure 9-1 Inheritance: Relationships among timepieces © 2006 Pearson Addison-Wesley. All rights reserved

4 Inheritance Revisited
Superclass or base class A class from which another class is derived Subclass, derived class, or descendant class A class that inherits the members of another class Benefits of inheritance It enables the reuse of existing classes It reduces the effort necessary to add features to an existing object © 2006 Pearson Addison-Wesley. All rights reserved

5 Inheritance Revisited
A subclass Can add new members to those it inherits Can override an inherited method of its superclass A method in a subclass overrides a method in the superclass if the two methods have the same declarations © 2006 Pearson Addison-Wesley. All rights reserved

6 Inheritance Revisited
Figure 9-2 The subclass Ball inherits members of the superclass Sphere and overrides and adds methods © 2006 Pearson Addison-Wesley. All rights reserved

7 Inheritance Revisited
A subclass inherits private members from the superclass, but cannot access them directly Methods of a subclass can call the superclass’s public methods Clients of a subclass can invoke the superclass’s public methods An overridden method Instances of the subclass will use the new method Instances of the superclass will use the original method © 2006 Pearson Addison-Wesley. All rights reserved

8 Inheritance Revisited
Figure 9-3 An object invokes the correct version of a method © 2006 Pearson Addison-Wesley. All rights reserved

9 access modifier (the default)
Java Access Modifiers If you don’t give an access modifier (the default) ‘Package-private’ Figure 9-4 Access to public, protected, package access, and private members of a class by a client and a subclass © 2006 Pearson Addison-Wesley. All rights reserved

10 Java Access Modifiers Public members
anyone Members declared without an access modifier (the default) “private – package” Methods of the class Methods of other classes in the same package Private only by methods of the class Protected Methods of the subclass © 2006 Pearson Addison-Wesley. All rights reserved

11 Is-a and Has-a Relationships
Two basic kinds of relationships Is-a relationship Has-a relationship © 2006 Pearson Addison-Wesley. All rights reserved

12 Is-a Relationship Inheritance should imply an is-a relationship between the superclass and the subclass Example: If the class Ball is derived from the class Sphere A ball is a sphere Figure 9-5 A ball “is a” sphere © 2006 Pearson Addison-Wesley. All rights reserved

13 Is-a Relationship Object type compatibility
An instance of a subclass can be used instead of an instance of the superclass, but not the other way around © 2006 Pearson Addison-Wesley. All rights reserved

14 Has-a Relationships Figure 9-6 A pen “has a” or “contains a” ball
© 2006 Pearson Addison-Wesley. All rights reserved

15 Has-a Relationships Has-a relationship Also called containment
Cannot be implemented using inheritance Example: To implement the has-a relationship between a pen and a ball Define a data field point – whose type is Ball – within the class Pen © 2006 Pearson Addison-Wesley. All rights reserved

16 Abstraction, Interfaces, and Polymorphism
Adpated from Material by Charles De Cuir © 2006 Pearson Addison-Wesley. All rights reserved

17 Abstract Classes Abstract classes Abstract methods
abstract keyword contain abstract methods. Abstract methods contain no implementation (a child class will fill it in) A class that contains all abstract methods is called an interface. It is not possible to create an instance of an abstract class or an interface, since some or all of their methods are not complete. © 2006 Pearson Addison-Wesley. All rights reserved

18 Abstract Classes Abstract classes and interfaces help organize.
Remember our OO principles Model an abstract concept without being able to instantiate it “Vehicles represents the abstract concept of what we can eat. It doesn't make sense for an instance of vehicle to exist.” Thinking about concepts abstractly allows for much more freedom in code. © 2006 Pearson Addison-Wesley. All rights reserved

19 Implementation: Abstract Classes
Create an abstract class when you are creating several objects that will share some functionality, and differ in some functionality. Methods that function the same way for every object Implemented in the abstract superclass, Methods that differ in functionality for each object declared abstract in the abstract superclass implemented in each of the subclasses. © 2006 Pearson Addison-Wesley. All rights reserved

20 Example public abstract class Electronic {       boolean powerOn = false;       public boolean isOn()       {            return powerOn;       }       public void turnOn()       {     powerOn = true;             System.out.println("Turned on");       }       public void turnOff()       {     powerOn = false;             System.out.println("Turned off");       }       public abstract void operate(); // no body } © 2006 Pearson Addison-Wesley. All rights reserved

21 Example Pt. 2 public class Toaster extends Electronic {       boolean hasBread = false;       public Toaster(boolean bread)       {            hasBread = bread;       }       public boolean checkForBread()       {            return hasBread;       }       public void addBread()       {            hasBread = true;       }       public void operate() // implements the abstract base method       {             if (isOn() && checkForBread())             {   System.out.println("Bread toasted");            }             else if (!isOn()) // isOn() method from base class             {   System.out.println("Turn power on");            }             else if (!checkForBread())             {    System.out.println("Must insert bread before toasting");     }       } } © 2006 Pearson Addison-Wesley. All rights reserved

22 Abstract Classes Abstract classes
A class that contains at least one abstract method must be declared as an abstract class A subclass of an abstract class must be declared abstract if it does not provide implementations for all abstract methods in the superclass To be of any use, an abstract class must be extended public abstract class Vehicle © 2006 Pearson Addison-Wesley. All rights reserved

23 Interfaces An interface is an abstract class that contains all abstract methods. interface keyword No implementation in the class. Every method is assumed abstract © 2006 Pearson Addison-Wesley. All rights reserved

24 Interface Example public interface Animal {       String talk();       int nuberOflegs();       int nuberOfarms();       boolean hasFur(); } Interfaces are not extended, they are implemented by other classes. © 2006 Pearson Addison-Wesley. All rights reserved

25 Interface Example public class Dog implements Animal // implements ALL methods! {       public Dog(){}       public String talk()       {      return "Woof“;      }       public int numberOfLegs()       {      return 4;       }       public int numberOfArms()       {      return 0;       }       public boolean hasFur()       {      return true;   } } © 2006 Pearson Addison-Wesley. All rights reserved

26 Abstract Class vs. Interface
act.html © 2006 Pearson Addison-Wesley. All rights reserved

27 Polymorphism Using a superclass variable to refer to a subclass object. I can have an array of ‘Animals’, and at runtime, calling myAnimal.speak will produce the appropriate sound for Dog, Cat, etc. © 2006 Pearson Addison-Wesley. All rights reserved

28 Polymorphism Imagine you are creating a game, in which the user chooses a certain door and behind each door is a different kind of animal. © 2006 Pearson Addison-Wesley. All rights reserved

29 public class AnimalGame {       public static void main(String[] args)       {             System.out.println(“Enter door #");             Scanner input = new Scanner(System.in);             int doorNumber = input.nextInt();             Animal animal = null; // not instantiated // cannot create an animal since it is an interface             if (doorNumber == 1)             {     animal = new Cat();  // late binding }             else if (doorNumber == 2)             {     animal = new Dog();            }             else if (doorNumber == 3)             {     animal = new TRex();           }             System.out.println("The animal behind your door says: " + animal.talk());      } } © 2006 Pearson Addison-Wesley. All rights reserved

30 Dynamic Binding and Abstract Classes
Figure 9-7a area is overridden: a) mySphere.DisplayStatistics( ) calls area in Sphere © 2006 Pearson Addison-Wesley. All rights reserved

31 Dynamic Binding and Abstract Classes
area is overridden: b) myBall.displayStatistics( ) calls area in Ball © 2006 Pearson Addison-Wesley. All rights reserved

32 Dynamic Binding and Abstract Classes
overriding a superclass method-in subclass final Prevents a method from being overridden by a subclass abstract Requires the subclass to override the method Early binding or static binding The appropriate version of a method is decided at compilation time Used by methods that are final or static Late binding Decided at runtime © 2006 Pearson Addison-Wesley. All rights reserved

33 Summary A subclass inherits all members of its previously defined superclass, but can access only the public and protected members Subclasses and superclasses A subclass is type-compatible with its superclass The relationship between superclasses and subclasses is an is-a relationship A method in a subclass overrides a method in the superclass if they have the same parameter declarations © 2006 Pearson Addison-Wesley. All rights reserved

34 Summary An abstract method in a class is a method that you can override in a subclass A subclass inherits The interface of each method that is in its superclass The implementation of each nonabstract method that is in its superclass An abstract class Specifies only the essential members necessary for its subclasses Can serve as the superclass for a family of classes © 2006 Pearson Addison-Wesley. All rights reserved

35 Summary Early (static) binding: compiler determines at compilation time the correct method to invoke Late (dynamic) binding: system determines at execution time the correct method to invoke When a method that is not declared final is invoked, the type of object is the determining factor under late binding © 2006 Pearson Addison-Wesley. All rights reserved


Download ppt "Chapter 9 Carrano Chapter 10 Small Java"

Similar presentations


Ads by Google