1 / 18

Class Hierarchies, Inheritance and Interfaces

Class Hierarchies, Inheritance and Interfaces. CIS 304 Intermediate Java Programming for Business. Motivations for OO Programming. Coding that fosters reuse, multiple use, easier design, reliability, faster development, lower life-cycle cost brought about by … Abstraction Encapsulation

edan
Télécharger la présentation

Class Hierarchies, Inheritance and Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Hierarchies, Inheritance and Interfaces CIS 304 Intermediate Java Programming for Business

  2. Motivations for OO Programming Coding that fosters reuse, multiple use, easier design, reliability, faster development, lower life-cycle cost brought about by … • Abstraction • Encapsulation • Inheritance

  3. Motivations for OO Programming "Real World" doesn't "start from scratch" some objects resemble other objects some objects act the same (at least in some respects) as other objects • So, OO programming allows classes (from which objects are constructed) to inherit characteristics and behaviors from other classes

  4. What Do These Classes (objects) Have in Common? Savings Acct Bank Acct Checking Acct Now, in what ways do these differ?

  5. Need to Differentiate "is a" Relationship from a "has a" Relationship! Savings Acct Bank Acct Checking Acct Is a Bank Acct a Savings Acct? Is a Savings Acct a Bank Acct? Does a Bank Acct have a customer name? an origination date?

  6. Creating a Hierarchical Relationship between Classes in Java public class BankAcct { } public class SavingsAcct extends BankAcct { }

  7. What is Shared Between Superclass and Subclass -- instance variables public class BankAcct { String acctID; Date origDate; } public class SavingsAcct extends BankAcct { //each SavingsAcct object will have an acctID and // origDate instance variable }

  8. What is Shared Between Superclass and Subclass -- methods public class BankAcct { public void setAcctID(String acctID) {this.acctID = acctID;} public int closeAcct(String acctID) {… code here …} } public class SavingsAcct extends BankAcct { //each SavingsAcct object will be able to use the methods // setAcctID and closeAcct }

  9. Keyword super • We know that the keyword this refers within the code of an object to the current object itself • Similarly, the keyword super refers within the code of an object to the superclass of the current object

  10. Term: overloading • Recall that the signature of a method uniquely identifies a method • The signature consists of the name of the method and the number, type, and order of the arguments required of the method • Example: if you define the method public void trialMethod(int myInt, String myString) {… code here …} and then call it with trialMethod("Georgia Jones") then Java will say that you have not defined the method trialMethod(String)

  11. Term: overloading • Method overloading, usually just referred to as overloading, is when you have more than one method that uses the same method name • So methods public void sampleMethod() {…code here...} public void sampleMethod(double pay) {...} public void sampleMethod(int myInt, String a) {...} are overloaded • How does Java know which method to use?

  12. Term: overriding • When methods in a subclass and its superclass have the same signature, this is called method overriding • Before, the signature told Java which method to use, but here we have the chance of two methods with the same signature. So, what does Java do? • Where there would otherwise be ambiguity, Java will use the method of the subclass (i.e., the more specific prevails) • If you want to access the superclass' method with the same signature you can by using the keyword super. for example super.methodWithSameName()

  13. Overriding Each of these classes can have its own method named findBal() Savings Acct Bank Acct Checking Acct If you are in a SavingsAcct object and use findBal(), Java needs to know -- and does decide -- which findBal() method you are referring to?

  14. protected visibility If the instance variables in BankAcct are private, they can't be seen or changed by code in SavingsAcct or CheckingAcct Savings Acct Bank Acct Checking Acct If you make the instance variables of BankAcct protected and not private, then all the subclasses of BankAcct can access the instance variables directly

  15. Class Object Recall: this is the real hierarchy, with object at the top level Savings Acct Object Bank Acct Checking Acct

  16. Assignment Declarations: Object myObj, theObj; BankAcct aBankAcct; SavingsAcct mySavings; Which assignments statements are OK? myObj = aBankAcct; aBankAcct = mySavings; theObj = mySavings; mySavings = aBankAcct; aBankAcct = theObj;

  17. Casting • You can not make the assignment mySavings = aBankAcct; but if you know that the object aBankAcct is actually a SavingsAcct object, you can cast it down the hierarchy using mySavings = (SavingsAcct) aBankAcct; • Another example (if theObj is actually a BankAcct object) aBankAcct = (BankAcct) theObj;

  18. Casting -- Problems that Occur • If you do the cast mySavings = (SavingsAcct) aBankAcct; and aBankAcct is not actually a SavingsAcct object, a Class Cast Exception is thrown • this will bomb your program if the Exception is not caught

More Related