180 likes | 336 Views
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
E N D
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 • Inheritance
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
What Do These Classes (objects) Have in Common? Savings Acct Bank Acct Checking Acct Now, in what ways do these differ?
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?
Creating a Hierarchical Relationship between Classes in Java public class BankAcct { } public class SavingsAcct extends BankAcct { }
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 }
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 }
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
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)
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?
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()
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?
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
Class Object Recall: this is the real hierarchy, with object at the top level Savings Acct Object Bank Acct Checking Acct
Assignment Declarations: Object myObj, theObj; BankAcct aBankAcct; SavingsAcct mySavings; Which assignments statements are OK? myObj = aBankAcct; aBankAcct = mySavings; theObj = mySavings; mySavings = aBankAcct; aBankAcct = theObj;
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;
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