Chapter 6 Class Hierarchies Inheritance and Interfaces This
- Slides: 55
Chapter 6 Class Hierarchies, Inheritance, and Interfaces ¡ ¡ ¡ This is a very good chapter for us at this point in time. We have been programming hard this semester and know how to use most java programming commands. Now we will take a look at inheritance. Some areas that I have been making you address in your programs and now we will look at these things again. I want to thank you for all your hard work thus far this semester.
Chapter 6 ¡ Object Oriented design l l Abstraction Encapsulation Polymorphism Inheritance Modular programming ¡ Inheritance, polymorphism, and interfaces ¡
6. 1 Class Hierarchies and Inheritance Extending existing classes ¡ New class is called subclass ¡ l l can add data fields and methods can override existing methods Original class is called Super Class ¡ See example page 360 Hierarchical in biology ¡
is a versus has a ¡ a car is a vehicle l ¡ a car has a wheel l ¡ car is subclass of vehicle attribute of car Not all vehicles have wheels l Snowmobile public class Car extends Vehicle { Wheels[ ] w = new Wheels[4] ¡ Keyword extends makes car subclass of vehicle
Case study: Hierarchy employee class ¡ Class called New. Employee stores basic employee data l l l l name SSN job title address phone number age start date total pay to date
Methods Besides standard methods (accessor, modifiers…. ¡ Also methods to ¡ l l l ¡ compute numbers of years with company years to retirement update total pay What if you need to differentiate between hourly and salaried
Analysis and Design See tables page 362 ¡ Now what do we need to do to add ¡ l l ¡ We inherit from New. Employee l ¡ Salary. Employee Hourly. Employee then add necessary methods and attributes See tables on pages 363 and 364
Implementation ¡ See pages 364 -366 for definition of base class l l l See attributes page 364 Notice use of this. page 365 Notice 3 constructors page 365 one default ¡ one only basic information ¡ one complete ¡ l Look at equals method bottom page 366
This used heavily ¡ Used this to differentiate names l l ¡ Used to call methods l ¡ this. name this. age not typically used New use bottom page 367 l l use to call constructor this(name, social);
Class Salary. Employee public class Salary. Employee extends New. Employee { ¡ Salary. Employee takes on all methods and attributes of super. Class ¡ So implementation on page 369 only includes the necessary additional data fields and methods
Implementation Salary. Employee See data field only need annual. Salary ¡ Notice constructor ¡ l l l ¡ calls super(name, social); same for larger constructor calls the constructor of the super class Can user super in other methods l see to. String
Implementation Hourly. Employee ¡ ¡ ¡ See Pages 372 – 373 Pages 373 – 374 test app, simply tests the classes See running code in JBuilder
Operations in Class Hierarchy Section 6. 1 Object New. Employee Salary. Employee Hourly. Employee
Design ¡ See tables pages 377 -378
Method Overloading Each class has 3 constructor methods ¡ Known as Overloading ¡ Java knows which to call based on the method signature. ¡ If none found get method not found error ¡
Method Overriding Each class has a to. String method ¡ Which one does it call ¡ The one in the subclass overrides the one in the superclass ¡ If none in sub class will call one in super class ¡ allows you to override functionality of methods in superclass if don’t like default functionality ¡
Protected Visibility Data fields defined as private in a super class can not be accessed in the subclass directly. ¡ Data fields defined as protected in a super class can be accessed directly in a subclass. ¡
Shadowing data fields ¡ If local variable (in method) has same name as class data field. l l l ¡ can not access class data field local field shadows it can use prefix this to access it Same applies to data fields in super class l can use prefix super to access it
Assignment in class Hierarchy ¡ See example 6. 2 page 381 l l l See declarations first Assignments obj = an. Emp can only access methods defined in obj ¡ to. String is one ¡ But will call to. String of New. Employee ¡
Assignment in class Hierarchy ¡ Similarly on page 382 – 383 l l l ¡ an. Emp = hour. Emp an. Emp was originally instantiated as New. Employee therefore will only have methods available that are a part of New. Employee When we assign within a class hierarchy l A variable of type super class can reference an object of sub class
Casting in Class hierarchy We can remedy not being able to access methods that are not a part of super class when made to reference sub class via casting ¡ ((Hourly. Employee) an. Emp). set. Hours(30. 0); ¡ Need to be careful with this, if not correctly can create run time errors. ¡
Passing Objects as arguments When passing arguments of different class types same rules apply as for assignment ¡ Remember that objects passed by reference ¡ Therefore change the state of the object in a method it is changed in the calling program ¡
instanceof Operator object instanceof Class. Name ¡ test to see whether object (instance variable) is and instance of class. Name (class type) ¡ See bottom of page 387 ¡ clerk instanceof Hourly. Employee ¡ When might you use this? ¡
Section 6. 3 Polymorphism Another aspect of Polymorphism would allow us to have an array of employee of any type. ¡ Create an array of New. Employee objects. ¡ l can then store any object type See methods bottom of page 389 ¡ See code page 391 ¡ l notice use of instanceof in compute. Payroll
Dynamic binding ¡ Notice bottom page 391 to. String loop l l inside we have result + employees[I]. to. String() java does not know which to. String method to call until run time because does not know what type of object employees[I] refers to until run time known as dynamic binding
6. 4 Interfaces An interface is very similar to a class ¡ It is used to specify the requirements for a class ¡ If a class implements an interface it must have a certain base functionality ¡ See simple example bottom page 395 ¡ Classes that implement this interface must provide a method: ¡
Abstract method ¡ The class in this interface: l public double calc. Weekly. Pay() is an abstract method since it has no body ¡ it is not defined in the interface, only the header ¡ l ¡ this does define the signature interfaces can only have: l l abstract methods constant definitions (static final)
Implements A class can only extend on class, but can implement several interfaces ¡ If our classes implement Payable it would simplify the use of this class ¡ See code middle of page 396 ¡ Method to call (which calc. Weekly. Pay) determined at runtime ¡
Steps for Payable Write Payable interface (file Payable. java) ¡ Add implements Payable to all classes that implement it. Verify that these classes contain complete definitions for method calc. Weekly. Pay() ¡ Use casting to ensure that all calls to method calc. Weekly. Pay() are applied only to type Payable references ¡
Comparable interface Java has a number of useful built in interfaces ¡ Comparable ¡ l l l requires those that implement interface to provide compare. To method See table page 398 Allows us to sort Comparable objects
Sorting Code page 399 allows sorting ¡ If employee types implements Comparable ¡ l l must provide compare. To bottom page 402 allows sort by SSN
Abstract Classes 6. 5 Inheritance is used to make it easier to re-use code. ¡ Additionally inheritance is used to provide structure to groups of related classes. ¡ A car is a vehicle and a car has a wheel ¡ l l A car is a subclass of vehicle A car has a data field of wheel
Case Study Page 405 Need to find the total area of a collection of geometric figures. ¡ For instance a painter looking to find exterior area of house to purchase paint. ¡ Would like to create an array of geometric shapes. ¡ l Array must be of like data types right?
Abstract class ¡ An abstract class provides an outline of a class. l l Leaves the complete definition to those classes that extend it. Contains classes that are defined by those classes that extend it. You can not create an instance of an abstract class. Abstract classes put the common members as high up in the hierarchy as possible
Geo. Figure case study Page 408 Rectangle extends Geo. Figure ¡ Defines the methods ¡ l l compute. Area compute. Perimeter Page 410 Circle extends Geo. Figure ¡ Defines the methods ¡ l l compute. Area compute. Perimeter
Geo. Figure case study Page 411 Triangle extends Geo. Figure ¡ Defines the methods ¡ l l compute. Area compute. Perimeter
Geo. Figure case study ¡ See implementation Page 412 l Can now create an array of Geo. Figure and put in it Triangle ¡ Circle ¡ Rectangle ¡ l Since they are all of the same type
Geo. Figure case study ¡ Can put in loop and use. to. String to print each type. l ¡ Since each shape class properly overrode the to. String we each prints in it’s own proper format Can put in loop and call the compute. Area method to add up all the areas. l Abstract class makes sure that all the method names are the same.
Drawing figures using an Abstract class and an Interface 6. 6 Geo. Figure Rectangle Circle Triangle Drawable. Rectangle Darwable. Circle Drawable. Triangle
New Geofigure Add new data fields and methods to Geofigure ¡ See table bottom of page 415 ¡
Drawable interface ¡ See Drawable interface top of page 416 l draw. Me draws the figures on the screen
Implementation ¡ See implementation pages 418 -421
Multiple inheritance Java does not support multiple inheritance ¡ Multiple inheritance in when a class extends more than one superclass. ¡ The use of multiple inheritance is and it’s appropriateness has been debated by object oriented developers. ¡
Typical example Toy Elephant Toy. Elephant
Interfaces also allow for a limited form of multiple inheritance ¡ An interface has all abstract methods ¡ It’s data fields can only be final class data fields ¡ A class does not extend an interface, rather it implements it. ¡
Packages 6. 7 ¡ The package to which a class belongs is declared at the top of the class. l ¡ Reserved word package followed by package name and semicolon All classes in the same package are stored in the same directory or folder
Packages ¡ Classes that are not a part of the package can only access the public members. l ¡ The complete name of a class is l ¡ ¡ Member is a data field or method defined in a class package. Name. class. Name See middle 425 Only need this until variable is “bound” to the instance. l Once this occurs java knows which class to use
Packages and default visibility ¡ There is a fourth kind of visibility we have not looked at yet. l l default visibility also called package visibility
Visibility ¡ Private l ¡ Protected l ¡ Only by child classes Public l ¡ Only by class members Anyone Default (none) l By anyone in the package
Section 6. 8 Testing a Program System ¡ Must learn to test in stages as the program develops
Top down Testing and stubs Use a stub when testing for all methods that are not yet complete ¡ A stub implements the interface but the body performs some minimal basic function ¡ Typically also displays when in stub ¡ See example middle page 428 ¡
Bottom up testing and drivers As method is completed it is substituted for it’s stub ¡ Before putting new method in class we typically preliminarily test it with a driver. ¡ Simple program to test a class ¡ l l has small main that instantiates object then calls the methods
Bottom up testing and drivers ¡ These drivers are typically inserted directly in the class l l That is the class includes a main method that is only used when testing the class called bottom-up testing these can then be used to test the class whenever changes are made allows for keeping previous tests made
Testing techniques ¡ Black box versus White box testing l l will cover this heavily in software engineering class black-box tester has no knowledge of the code being tested ¡ verifies that system meets the specifications ¡ l white-box (or glass-box) tester has knowledge of the code ¡ develops tests to test all the code ¡ l for instance all branches in an if statement
Common Programming Errors ¡ Be VERY conservative with visibility of data fields l ¡ all remain private except is very good reason not to Can use this and super to call constructors
- Issues for goal hierarchies
- Issues for goal hierarchies
- Rich feature hierarchies
- Chunking ap psychology definition
- Network software protocol hierarchies
- Misguided little unforgivable hierarchies
- Protocol hierarchies
- Difference between abstract classes and interfaces
- Interface and abstract class difference in java
- Designing interfaces and dialogues
- Gui event types in java
- Surface phenomena and colloidal system
- Chapter 11 complex inheritance and human heredity test
- Expressive interfaces
- Web based interface
- Uml interfaces are used to:
- Industrial interfaces
- Expressive interfaces
- Blueprint interfaces
- Which is not an objective of designing interfaces?
- Property management system interface
- Interfaces are syntactically similar to
- Joe hogan openet
- Toggle selection in hci
- Hlr interfaces
- Expressive interfaces
- Hardware interfaces in srs example
- Team interfaces
- Operacion de micros e interfaces
- Why are user interfaces hard to implement
- Heuristic evaluation of user interfaces
- Explain mdi
- Gui for r
- Interfaces inteligentes
- Abstract classes in java
- User interfaces design dc
- What is interface
- 2g architecture with interfaces
- Communication interface in embedded systems
- Chapter 11 section 2 complex patterns of inheritance
- Chapter 16 the molecular basis of inheritance
- Chapter 15 the chromosomal basis of inheritance
- A gene locus is
- Chapter 16 the molecular basis of inheritance
- Chapter 15: the chromosomal basis of inheritance
- The chromosomal basis of inheritance chapter 15
- Ap biology chapter 15
- Chapter 11 section 1 basic patterns of human inheritance
- Chapter 11 section 1 basic patterns of human inheritance
- Chapter 13 the molecular basis of inheritance
- Chapter 9 patterns of inheritance
- Chapter 15 the chromosomal basis of inheritance
- Maternal effect and maternal inheritance
- Advantages and disadvantages of inheritance in java
- Maternal effect and maternal inheritance
- Section 12-1 chromosomes and inheritance