Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 7 Inheritance, Polymorphism, and Scope.

Similar presentations


Presentation on theme: "1 Chapter 7 Inheritance, Polymorphism, and Scope."— Presentation transcript:

1 1 Chapter 7 Inheritance, Polymorphism, and Scope

2 2 Chapter 7 Topics l Inheritance l Inheritance and the Object-Oriented Design Process l How to Read a Class Hierarchy l Derived Class Syntax l Scope of Access l Implementing a Derived Class l Copy Constructors l Output and Input of Objects

3 3 Inheritance l Inheritance A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class. l Superclass The class being inherited from l Derived class The class that inherits l The derived class is specialized by adding properties specific to it

4 4 Inheritance Hierarchy Example Vehicle WheeledVehicleBoat BicycleCar FourDoor Every Car “is a” WheeledVehicle. TwoDoor

5 5 Address object hierarchy Object CompanyAddressHomeAddress BoxAddressWorkAddress Address

6 6 Blank CRC Card Class Name: Superclass: Subclasses: CompanyAddressAddressBoxAddress ResponsibilitiesCollaborations

7 7 Hierarchy of Component Classes Component Container Object abstract classes JComponent JTextComponen t JTextField

8 8 More Definitions l Override When an instance method in a derived class has the same form of heading as an instance method in its superclass, the method in the derived class overrides (redefines) the method in the superclass l Hide When a field in a derived class has the same name as one in its superclass or a class method has the same form of heading as a class method in its superclass, the field or class hide the corresponding component in the superclass Say again?

9 9 An example public class Example { char letter; public static String lineIs(); … } public class ExtExample extends Example { char letter; public static String lineIs(); … } Hiding or overriding?

10 10 Another Example public class Example { char letter; public String lineIs(); … } public class ExtExample extends Example { String letter; public String lineIs(); … } Hiding or overriding?

11 11 Overriding vs. Hiding l We override an instance method of a superclass by providing an instance method in a derived class with the same form of heading l We hide a data field of a superclass by providing a field in a derived class with the same name

12 12 Polymorphism l Polymorphism is the ability of a language to have duplicate method names in an inheritance hierarchy and to decide which method is appropriate to call depending on the class of the object to which the method is applied.

13 13 OOP Characteristics Characteristic Meaning Benefit New classes created from existing classes Software resusabiltiy Inheritance Write programs to handle variety of existing and future related classes Add new capabilities Polymorphism

14 14 Class Syntax Derived Class Syntax ClassModifier class Identifier extends ClassName { ClassDeclaration... }

15 15 Scope of Identifier l The scope of an identifier is the portion of program code where it is legal to use that identifier for any purpose l That is, who knows what, where, and when

16 16 Shadowing l Shadowing A scope rule specifying that a local identifier declaration blocks access to an identifier declared with the same name outside the block containing the local declaration A shadowed class member can be accessed by using keyword this together with the class member

17 17 Four Levels of Class Member Access public protected ( no access modifier ) “package access” private public private protected package

18 18 Member Accessibility External access public protected (default) private package Same package yes yes yes no Derived class in yes yes no no another package (inheritance only) User code yes no no no

19 19 Subclass Constructor A subclass constructor always calls the superclass constructor first (either as a first statement call to super or automatically when there is no call to super ) to create and initialize members inherited from the superclass.

20 20 Method Signature l The signature of a method consists of the method’s name, together with the number and type(s) of its parameters in their given order l Method overloading is the use of a method name more than once, each time with different signatures

21 21 Method Overloading Means l Several methods of the same name are defined with different sets of parameters (based on number of parameters, types of parameters, or order of parameters)

22 22 Addresses in Memory number ch l When a variable of primitive type is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. The value of the variable is stored at this location double number; char ch; ch = ‘X’; 2002 2006 ‘X’

23 23 Reference Types: A Review title book String title ; String book ; title = “Problem Solving”; book = title ; 2003 “Problem Solving” Memory Location 2003 2003 l A variable of reference type stores the address of the location where the object can be found.

24 24 Class SavingsAccount getNumber getBalance. withdraw getName deposit SavingsAccount Private data: number 423511 name 2003 balance 537.65 activation 3000 “Nell Dale” month day year 13 “June” 2003 3000

25 25 Shallow Copy. SavingsAccount Private data: number 423511 name 2003 balance 537.65 activation 3000. SavingsAccount Private data: number 423511 name 2003 balance 537.65 activation 3000 “Nell Dale” month day year 13 “June” 2003 3000 shallow copy

26 26 Shallow Copy vs. Deep Copy l Shallow copy All class data fields, including references are copied; any objects referred to by data fields are not copied l Deep copy All class data fields are copied, and all objects referred to are copied

27 27 What’s the difference? l A shallow copy shares nested objects with the original class object l A deep copy makes its own copy of nested objects at different locations than in the original class object

28 28 Separate deep copy deep copy. SavingsAccount Private data: number 423511 name 2003 balance 537.65 activation 3000 “Nell Dale” month day year 13 “June” 2003 3000. SavingsAccount Private data: number 423511 name 6000 balance 537.65 activation 4000 “Nell Dale” month day year 13 “June” 2003 6000 4000

29 29 Copy Constructor A copy constructor is a constructor that creates a deep copy of an object that can be used for other purposes, such as creating a new instance of an immutable object from an old one public SavingsAccount(SavingsAccount oldAcct, String changeOfAddress) {... // create deep copy of oldAcct } // call account = new Savings Account(oldAcct, newAddress);

30 30 Output and Input of Objects l Output and Input? Don’t you mean Input and Output? No! l An object can be written out as a stream of bytes l The stream of bytes can be read back in and type cast back into the original object

31 31 O/I Definitions l Serializing Translating an object into a stream of bytes l Deserializing Translating a serialized stream of bytes back into the original object How? You don’t need to know; Java does it for you when The class implements Serializable

32 32 How to Write Objects public class Name implements Serializable {...}... public class ObjectFileWrite { private static ObjectOutputStream outObject; public static void main(String[] args) throws IOException { Name person;... // Define the field of person outObject = new ObjectOutputStream(new FileOutputStream("outObject.dat")); outObject.writeObject(person); outObject.close(); }

33 33 How to Read Objects import Name; public class ObjectFileRead { private static ObjectInputStream inObject; //Input file public static void main(String[] args) throws IOException, ClassNotFoundException { Name person; inObject = new ObjectInputStream(new FileInputStream("outObject.dat")); person = (Name)inObject.readObject();... inObject.close();} }


Download ppt "1 Chapter 7 Inheritance, Polymorphism, and Scope."

Similar presentations


Ads by Google