ObjectOriented Principles in Java Part III Inheritance Overloading
Object-Oriented Principles in Java: Part III Inheritance Overloading methods Shadowing States A return to exceptions: creating new exceptions Interfaces and abstract classes Packages Object-Oriented Principles in Java: Part III James Tam
What Is Inheritance? Creating new classes that are based on existing classes. Existing class Object-Oriented Principles in Java: Part III James Tam
What Is Inheritance? Creating new classes that are based on existing classes. All non-private data and methods are available to the new class (but the reverse is not true). The new class is composed of information and behaviors of the existing class (and more). Existing class New class Object-Oriented Principles in Java: Part III James Tam
Inheritance Terminology Superclass Generalization Parent class Subclass Specialization Child class Object-Oriented Principles in Java: Part III James Tam
When To Employ Inheritance If you notice that certain behaviors or data is common among a group of candidate classes The commonalities may be defined by a superclass What is unique may be defined by particular subclasses Generic user Engineer Object-Oriented Principles in Java: Part III Management Technical support James Tam
Why Employ Inheritance To allow for code reuse It may result in more robust code Existing class New class Object-Oriented Principles in Java: Part III James Tam
Inheritance: Format Class <Name of Subclass > extends <Name of Superclass> { // Definition of subclass – only what is unique to subclass } Object-Oriented Principles in Java: Part III James Tam
Inheritance: An Example class Dragon extends Monster { public void display. Special () { System. out. println("Breath weapon: "); } } Object-Oriented Principles in Java: Part III James Tam
The Parent Of All Classes Class Object is at the top of the inheritance hierarchy (includes Class Array) All other classes inherit it’s data and methods For more information about this class see the url: http: //java. sun. com/j 2 se/1. 3/docs/api/java/lang/Object. html Object-Oriented Principles in Java: Part III James Tam
Review: Relations Between Classes Composition (“has-a”) Association (“knows-a”) Inheritance (“is-a”) Object-Oriented Principles in Java: Part III James Tam
Composition (“Has-a”) A composition relation exists between two classes if one classes’ field(s) consist of another class e. g. , A car has an (has-a) engine Class Foo { private Bar b; } Foo Bar -b: Bar Object-Oriented Principles in Java: Part III James Tam
Association (“Knows-a”) A composition relation exists between two classes if within one class’ method(s), there exists as a local variable an instance of another class e. g. , A car uses (knows-a) gasoline Class Foo { public void method () { Bar b = new Bar (); } } Foo Bar +method () Object-Oriented Principles in Java: Part III James Tam
Inheritance (“Is-a”) A composition relation exists between two classes if one class is the parent class of another class e. g. , A car is a type of (is-a) vehicle Class Foo { } Foo Class Bar extends Bar { Bar } Object-Oriented Principles in Java: Part III James Tam
Inheritance (“Is-a”) A composition relation exists between two classes if one class is the parent class of another class e. g. , A car is a type of (is-a) vehicle Class Foo { } Foo Class Bar extends Bar { Bar } Instances of the subclass can be used in place of instances of the super class Object-Oriented Principles in Java: Part III James Tam
Method Overloading • Different versions of a method can be implemented by different classes in an inheritance hierarchy. • Methods have the same name and parameter list (signature) • i. e. , <method name> (<parameter list>) Object-Oriented Principles in Java: Part III James Tam
Method Overloading Vs. Method Overriding Method Overloading • Multiple method implementations for the same class • Each method has the same name but different parameters (type, number or order) • The method that is actually called is determined at compile time. • i. e. , <reference name>. <method name> (parameter list); Example: Class Foo { display (); display (int i); display (char ch); : } Foo f = new Foo (); f. display(10); f. display(‘c’); Object-Oriented Principles in Java: Part III James Tam
Method Overloading Vs. Method Overriding Method Overloading • Multiple method implementations for the same class • Each method has the same name but different parameters (type, number or order) • The method that is actually called is determined at compile time. • i. e. , <reference name>. <method name> (parameter list); Example: Class Foo { display (); display (int i); display (char ch); : } Distinguishes overloaded methods Foo f = new Foo (); f. display(10); f. display(‘c’); Object-Oriented Principles in Java: Part III James Tam
Method Overloading Vs. Method Overriding • Multiple method implementations between the parent and child classes • Each method has the same return value, name and parameters (type, number or order) • The method that is actually called is determined at run time (Polymorphism) • i. e. , <reference name>. <method name> (parameter list); Object-Oriented Principles in Java: Part III James Tam
Method Overloading Vs. Method Overriding • Multiple method implementations between the parent and child classes • Each method has the same return value, name and parameters (type, number or order) • The method that is actually called is determined at run time (Polymorphism) • i. e. , <reference name>. <method name> (parameter list); Type of reference distinguishes overridden methods Object-Oriented Principles in Java: Part III James Tam
Method Overloading Vs. Method Overriding (2) Example: Class Foo { display (); : } Class Foo. Child { display (); } Foo f = new Foo (); f. display(); Foo. Child fc = new Foo. Child (); fc. display (); Object-Oriented Principles in Java: Part III James Tam
A Blast From The Past Dungeon Master Weapons Monsters Broadsword Scorpion Longbow Dragon Mummy Ghost Screamer Object-Oriented Principles in Java: Part III Knight Rapier Armour : James Tam
The Inheritance Hierarchy For The Monsters Monster Undead Robber Dragon Stone. Based Object-Oriented Principles in Java: Part III James Tam
The Inheritance Hierarchy For The Monsters Monster Undead Stone. Based Object-Oriented Principles in Java: Part III Giggler Dragon James Tam
The Dragon Sub-Hierarchy Dragon Red Dragon Object-Oriented Principles in Java: Part III Blue Dragon Halitosis Dragon James Tam
The Dragon Sub-Hierarchy Dragon Red Dragon Object-Oriented Principles in Java: Part III Blue Dragon Halitosis Dragon James Tam
Class Monster The complete program can be found in the directory: class Monster { protected int protection; protected int damage. Receivable; protected int damage. Inflictable; protected int speed; protected String name; Object-Oriented Principles in Java: Part III James Tam
Class Monster (2) public String to. String () { String s = new String (); s = s + "Protection: " + protection + "n"; s = s + "Damage receivable: " + damage. Receivable + "n"; s = s + "Damage inflictable: " + damage. Inflictable + "n"; s = s + "Speed: " + speed + "n"; s = s + "Name: " + name + "n"; return s; } public void display. Special. Ability () { System. out. println("No special ability"); } Object-Oriented Principles in Java: Part III James Tam
Class Dragon class Dragon extends Monster { public void display. Special () { System. out. print("Breath weapon: "); } } Object-Oriented Principles in Java: Part III James Tam
Class Blue. Dragon class Blue. Dragon extends Dragon { public void display. Special () { super. display. Special (); System. out. println("Lightening"); } } Object-Oriented Principles in Java: Part III James Tam
Class Halitosis. Dragon class Halitosis. Dragon extends Dragon { public void display. Special () { super. display. Special(); System. out. println("Stinky"); } } Object-Oriented Principles in Java: Part III James Tam
Class Red. Dragon class Red. Dragon extends Dragon { public void display. Special () { super. display. Special(); System. out. println("Fire"); } } Object-Oriented Principles in Java: Part III James Tam
Class Dungeon. Master class Dungeon. Master { public static void main (String [] argv) { Blue. Dragon electro = new Blue. Dragon (); Red. Dragon pinky = new Red. Dragon (); Halitosis. Dragon stinky = new Halitosis. Dragon () ; electro. display. Special (); pinky. display. Special (); stinky. display. Special (); } } Object-Oriented Principles in Java: Part III James Tam
Inheritance: A Second Example The source code for this example can be found in the directory: /home/profs/tamj/233/examples/inheritence/second. Example First. Foo -field. One: int // Accessors, constructors Second. Foo Driver -field. Two: int // Accessors, constructors Object-Oriented Principles in Java: Part III James Tam
The Driver Class class Driver { public static void main (String [] argv) { Second. Foo sf 1 = new Second. Foo (); System. out. println(); Second. Foo sf 2 = new Second. Foo (20); System. out. println(); Second. Foo sf 3 = new Second. Foo (100, 200); System. out. println(); } } Object-Oriented Principles in Java: Part III James Tam
Class Second. Foo class Second. Foo extends First. Foo { private int field. Two; public Second. Foo () { super(); System. out. println("Calling default constructor for class Second. Foo"); field. Two = 1; } public Second. Foo (int f 2) { super(); System. out. println("Calling one-argument constructor for class Second. Foo"); field. Two = f 2; } Object-Oriented Principles in Java: Part III James Tam
Class Second. Foo (2) public Second. Foo (int f 1, int f 2) { super(f 1); System. out. println("Calling two-argument constructor for class Second. Foo"); field. Two = f 2; } public int get. Field. Two () {return field. Two; } public void set. Field. Two (int i) {field. Two = i; } } Object-Oriented Principles in Java: Part III James Tam
Class First. Foo class First. Foo { private int field. One; public First. Foo () { System. out. println("Calling default constructor for class First. Foo"); field. One = 0; } public First. Foo (int f 1) { System. out. println("Calling one-argument constructor for class First. Foo"); field. One = 0; } public int get. Field. One () {return field. One; } public void set. Field. One (int i) {field. One = i; } } Object-Oriented Principles in Java: Part III James Tam
Levels Of Access Permissions Private “-” • Can only access field in the methods of the class where the field is originally listed. Protected “#” • Can access field in the methods of the class where the field is originally listed or the subclasses of that class Public “+” • Can access field anywhere in the program Object-Oriented Principles in Java: Part III James Tam
Levels Of Access Permissions (Tabular Form) Accessible to Access level Same class Subclass Not a subclass Public Yes Yes Protected Yes No No Private Object-Oriented Principles in Java: Part III James Tam
Levels Of Access Permissions (Graphical Representation) Foo Rest of the program : -private int num 1; #protected int num 2; +public int num 3; : Can directly access num 3 Must use methods of Foo to access/modify num 1 and methods of Foo. Child to access/modify num 2 Foo. Child Can directly access num 2 Can directly access num 3 Must use methods of Foo to access/modify num 1 Object-Oriented Principles in Java: Part III James Tam
Inheritance: A Third Example The source code for this example can be found in the directory: /home/profs/tamj/233/examples/inheritence/third. Example First. Foo + CONSTFIELD: int # field. One: int - field. Two: int Second. Foo Driver # field. Three - field. Four Object-Oriented Principles in Java: Part III James Tam
The Driver Class class Driver { public static void main (String [] argv) { Second. Foo f 1 = new Second. Foo (); System. out. println(); Second. Foo f 2 = new Second. Foo (10, 20, 30, 40); System. out. println(); First. Foo f 3 = (First. Foo) new Second. Foo (); System. out. println(f 3. get. Field. Two ()); //System. out. println(f 3. get. Field. Three ()); // Second. Foo f 4 = (Second. Foo) new First. Foo (); } } Object-Oriented Principles in Java: Part III James Tam
Class Second. Foo class Second. Foo extends First. Foo { protected int field. Three; private int field. Four; public Second. Foo () { super(); System. out. println("Calling default constructor for class Second. Foo"); field. Three = 3; field. Four = 4; } public Second. Foo (int f 1, int f 2, int f 3, int f 4) { super (f 2); System. out. println("Calling four-argument constructor for class Second. Foo"); field. One = f 1; field. Three = f 3; field. Four = f 4; } : Object-Oriented Principles in Java: Part III James Tam
Class First. Foo class First. Foo { public static final int CONSTFIELD = 1; protected int field. One; private int field. Two; Object-Oriented Principles in Java: Part III James Tam
Class First. Foo (2) public First. Foo () { System. out. println("Calling default constructor for class First. Foo"); field. One = 0; field. Two = 0; } public First. Foo (int f 2) { System. out. println("Calling one-argument constructor for class First. Foo"); field. One = 0; field. Two = f 2; } public First. Foo (int f 1, int f 2) { System. out. println("Calling two-argument constructor for class First. Foo"); field. One = f 1; field. Two = f 2; } Object-Oriented Principles in Java: Part III James Tam
Shadowing Local variables in a method or parameters to a method have the same name as instance fields Fields of the subclass have the same name as fields of the superclass Object-Oriented Principles in Java: Part III James Tam
Local Variables Shadowing Instance Fields class Integer. Wrapper { private int num; public Integer. Wrapper () { num = (int) (Math. random() * 100); } public Integer. Wrapper (int no) { int num = no; } : } Object-Oriented Principles in Java: Part III James Tam
Fields Of The Subclass Have The Same Names As The Super. Classes’ Fields class Foo { private int num; public Foo () { num = 1; } public int get. Num () { return num; } public void set. Num (int no) {num = no; } } class Bar extends Foo { public Bar () { num = 10; } } Object-Oriented Principles in Java: Part III James Tam
Fields Of The Subclass Have The Same Names As The Super. Classes’ Fields class Foo { private int num; public Foo () { num = 1; } public int get. Num () { return num; } public void set. Num (int no) {num = no; } } class Bar extends Foo { public Bar () { num = 10; } } Object-Oriented Principles in Java: Part III Insufficient access permissions: Won’t compile James Tam
Fields Of The Subclass Have The Same Names As The Super. Classes’ Fields (2) class Foo { private int num; public Foo () { num = 1; } public int get. Num () { return num; } public void set. Num (int no) {num = no; } } class Bar extends Foo { private int num; public Bar () { num = 1; } Object-Oriented Principles in Java: Part III } James Tam
Fields Of The Subclass Have The Same Names As The Super. Classes’ Fields (2) class Foo { private int num; public Foo () { num = 1; } public int get. Num () { return num; } public void set. Num (int no) {num = no; } } NO! class Bar extends Foo { private int num; public Bar () { num = 1; } Object-Oriented Principles in Java: Part III } James Tam
The Result Of Attribute Shadowing class Bar extends Foo { private int num; public Bar () { num = 10; } public int get. Second. Num () { return num; } } class Driver { public static void main (String [] arv) { Bar b = new Bar (); System. out. println(b. get. Num()); System. out. println(b. get. Second. Num()); } } Object-Oriented Principles in Java: Part III James Tam
Changing Permissions Of Overridden Methods The overridden method must have equal or stronger (less restrictive) access permissions in the child class. Parent #method() Parent +method() Object-Oriented Principles in Java: Part III Parent #method() Parent -method() James Tam
The Final Modifier (Inheritance) Methods preceded by the final modifier cannot be overridden e. g. , public final void display. Two () Classes preceded by the final modifier cannot be extended • e. g. , final class Parent. Foo Object-Oriented Principles in Java: Part III James Tam
Classes And State The state of an object is determined by the values of it’s attributes. The states of objects can be modeled by State diagrams Not all attributes are modeled • The attribute can only take on a limited range of values • The attribute has restrictions that determine which values that it may take on. Object-Oriented Principles in Java: Part III James Tam
Example Class: Adventurer Class Adventurer { private boolean okay; private boolean poisoned; private boolean confused; private boolean dead; : } Object-Oriented Principles in Java: Part III James Tam
Class Adventurer: The Set Of States Injected with poison Okay Receive cure Receive antidote Poisoned After (10 minutes) Hit by confusion spell Confused Resurrected Object-Oriented Principles in Java: Part III Dead James Tam
Class Adventurer: State Diagram Injected with poison Okay Poisoned Receive antidote Receive cure Confused After (10 minutes) Resurrected Hit by confusion spell Object-Oriented Principles in Java: Part III Dead James Tam
Creating Your Own Exceptions Throwable Error Virtual. Machine. Error Exception … IOException … ? ? ? Run. Time Exception Out. Of. Memory. Error Object-Oriented Principles in Java: Part III James Tam
Class Exception: Local Inheritance Hierarchy Exception Class. Not. Found IOException Clone. Not. Found Exception EOFException File. Not. Found Malformed. URL Unknown. Host Exception Object-Oriented Principles in Java: Part III James Tam
Creating New Exceptions: An Example The full example can be found in the directory: /home/profs/tamj/233/examples/exceptions/writing. Exceptions Object-Oriented Principles in Java: Part III James Tam
The Driver Class import tio. *; class Driver { public static void main (String [] argv) { int new. Age; Person jim = new Person (); System. out. print("Enter age: "); new. Age = Console. in. read. Int(); Object-Oriented Principles in Java: Part III James Tam
The Driver Class (2) try { jim. set. Age(new. Age); } catch (Exception e) { System. out. println(e. get. Message()); } } } Object-Oriented Principles in Java: Part III James Tam
Class Person class Person { private int age; public static int MINAGE = 0; public static int MAXAGE = 120; public Person () { age = 0; } Object-Oriented Principles in Java: Part III James Tam
Class Person (2) public Person (int a) throws Invalid. Age. Exception { if ((a < MINAGE) || (a > MAXAGE)) throw new Invalid. Age. Exception("Invalid Age Exception: Age must be between “ + MINAGE + " & " + MAXAGE); else age = a; } Object-Oriented Principles in Java: Part III James Tam
Class Person (3) public void set. Age (int a) throws Invalid. Age. Exception { if ((a < MINAGE) || (a > MAXAGE)) throw new Invalid. Age. Exception("Invalid Age Exception: Age must be between “ + MINAGE + " & " + MAXAGE); else age = a; } public int get. Age () { return age; } } Object-Oriented Principles in Java: Part III James Tam
Class Invalid. Age. Exception class Invalid. Age. Exception extends Exception { Invalid. Age. Exception () { } Invalid. Age. Exception (String s) { super(s); } } Object-Oriented Principles in Java: Part III James Tam
Java Interfaces Similar to a class Provides a design guide rather than implementation details Specifies what methods should be implemented but not how << interface >> Interface method specification Realization / Implement Class method implementation Object-Oriented Principles in Java: Part III James Tam
Java Interfaces: Lollipop Notation Similar to a class Provides a design guide rather than implementation details Specifies what methods should be implemented but not how Interface Class method implementation Object-Oriented Principles in Java: Part III James Tam
Interfaces: Format for specifying the interface Interface <name of interface> { constants methods to be implemented } Format for realizing / implementing the interface class <name of class> implements <name of interface> { data fields methods actually implemented } Object-Oriented Principles in Java: Part III James Tam
Interfaces: A Checkers Example Regular rules Basic board Variant rules Object-Oriented Principles in Java: Part III James Tam
Interface Board interface Board { public static final int SIZE = 8; public void display. Board (); public void initialize. Board (); public void move. Piece (); boolean move. Valid (int x. Source, int y. Source, int x. Destination, int y. Destination); : : } Object-Oriented Principles in Java: Part III James Tam
Class Regular. Board class Regular. Board implements Board { public void display. Board () { : } public void initialize. Board () { : } Object-Oriented Principles in Java: Part III James Tam
Class Regular. Board (2) public void move. Piece () { // Get (x, y) coordinates for the source and destination if (move. Valid == true) // Actually move the piece else // Don’t move piece and display error message } public boolean move. Valid (int x. Source, int y. Source, int x. Destination, int y. Destination) { if (moving diagonally forward) return true; else return false; } } Object-Oriented Principles in Java: Part III James Tam
Class Variant. Board class Variant. Board implements Board { public void display. Board () { : } public void initialize. Board () { : } Object-Oriented Principles in Java: Part III James Tam
Class Variant. Board (2) public void move. Piece () { // Get (x, y) coordinates for the source and destination if (move. Valid == true) // Actually move the piece else // Don’t move piece and display error message } public boolean move. Valid (int x. Source, int y. Source, int x. Destination, int y. Destination) { if (moving straight-forward or straight side-ways) return true; else return false; } } Object-Oriented Principles in Java: Part III James Tam
Interfaces: Recapping The Example Interface Board • No state (data) or behavior (method bodies) listed • Specifies the behaviors that a board should exhibit • This is done by listing the methods that must be implemented by classes that implement the interface. Class Regular. Board and Variant. Board • Can have state and methods • They must implement all the methods specified by interface Board (can also implement other methods too) Object-Oriented Principles in Java: Part III James Tam
Implementing Multiple Interfaces Interface 1 Interface 2 Interface 3 Class Object-Oriented Principles in Java: Part III James Tam
Implementing Multiple Interfaces Format: Class <class name> implements <interface name 1>, <interface name 2>, <interface name 3> { } Object-Oriented Principles in Java: Part III James Tam
Multiple Implementations Vs. Multiple Inheritance A class can implement all the methods multiple interfaces Classes in Java cannot extend more than one class This is not possible in Java (but is possible in some other languages such as C++): Class <class name 1> extends <class name 2>, <class name 3>… { } Object-Oriented Principles in Java: Part III James Tam
Multiple Implementations Vs. Multiple Inheritance (2) A class can implement all the methods multiple interfaces Classes in Java cannot extend more than one class This is not possible in Java: Parent class 1 Parent class 2 Parent class 3 Child class Object-Oriented Principles in Java: Part III James Tam
Abstract Classes that cannot be instantiated A hybrid between regular classes and interfaces Some methods may be implemented while others are only specified Used when the parent class cannot define a default implementation (must be specified by the child class). Format: abstract class <class name> { <public/private/protected> abstract method (); } Object-Oriented Principles in Java: Part III James Tam
Abstract Classes (2) Example: abstract class Bank. Account { private float balance; public void display. Balance () { System. out. println("Balance $" + balance); } public abstract void deduct. Fees () ; } Object-Oriented Principles in Java: Part III James Tam
Packages A collection of related classes that are bundled together To allow for some implementation details to be exposed only to other classes in the package Used to avoid naming conflicts for classes org. omg. CORBA java. lang Object Exception Error String. Buffer System Object-Oriented Principles in Java: Part III James Tam
Fully Qualified Names: Matches Directory Structure package. Example. pack 1. Open. Foo. to. String() package name class name Object-Oriented Principles in Java: Part III method name James Tam
Fully Qualified Names: Matches Directory Structure pack 3. Open. Foo. to. String() : package name tamj class name 233 method name examples package. Example pack 3 Open. Foo. java Object-Oriented Principles in Java: Part III Closed. Foo. java James Tam
Matching Classes To Packages 1. The classes that belong to a package must reside in the directory with the same name as the package (previous slide). 2. The definition of the class must indicate the package that the class belongs to. Format: package <package name>; <visibility – public or package> class <class name> { } Object-Oriented Principles in Java: Part III James Tam
Matching Classes To Packages (2) Example public class Open. Foo { : } class Closed. Foo { : } Object-Oriented Principles in Java: Part III James Tam
Matching Classes To Packages (2) Example package pack 3; public class Open. Foo { : } package pack 3; class Closed. Foo { : } Object-Oriented Principles in Java: Part III Public: Class can be instantiated by classes that aren’t a part of package pack 3 Package (default): Class can only be instantiated by classes that are members of package pack 3 James Tam
Sun’s Naming Conventions For Packages Based on Internet domains (registered web addresses) e. g. , www. tamj. com. tamj. games. productivity Object-Oriented Principles in Java: Part III James Tam
Sun’s Naming Conventions For Packages Alternatively it could be based on your email address e. g. , tamj@cpsc. ucalgary. ca ca. ucalgary. cpsc. tamj Object-Oriented Principles in Java: Part III James Tam
Graphically Representing Packages In UML Package name +Classes visible outside the package -Classes not visible outside the package (protected class) Object-Oriented Principles in Java: Part III James Tam
Packages An Example The complete example can be found in the directory: /home/profs/tamj/examples/package. Example (But you should have guessed that from the previous slides) package. Example pack 1 Integer. Wrapper pack 2 Integer. Wrapper Object-Oriented Principles in Java: Part III pack 3 Closed. Foo Driver Open. Foo James Tam
Graphical Representation Of The Example pack 1 +Integer. Wrapper (Unnamed) -Driver pack 2 +Integer. Wrapper pack 3 +Open. Foo -Closed. Foo Object-Oriented Principles in Java: Part III James Tam
The Driver Class import package. Example. pack 3. *; class Driver { public static void main (String [] argv) { pack 1. Integer. Wrapper iw 1 = new pack 1. Integer. Wrapper (); pack 2. Integer. Wrapper iw 2 = new pack 2. Integer. Wrapper (); System. out. println(iw 1); System. out. println(iw 2); Open. Foo of = new Open. Foo (); System. out. println(of); of. manipulate. Foo(); } } Object-Oriented Principles in Java: Part III James Tam
Package Pack 1: Integer. Wrapper package pack 1; public class Integer. Wrapper { private int num; public Integer. Wrapper () { num = (int) (Math. random() * 10); } // Also includes one argument constructor, accessor and mutator methods public String to. String () { String s = new String (); s = s + num; return s; } } Object-Oriented Principles in Java: Part III James Tam
Package Pack 2: Integer. Wrapper package pack 2; public class Integer. Wrapper { private int num; public Integer. Wrapper () { num = (int) (Math. random() * 100); } // Also includes one argument constructor, accessor and mutator methods public String to. String () { String s = new String (); s = s + num; return s; } } Object-Oriented Principles in Java: Part III James Tam
Package Pack 3: Class Open. Foo package pack 3; public class Open. Foo { private boolean bool; public Open. Foo () { bool = true; } public void manipulate. Foo () { Closed. Foo cf = new Closed. Foo (); System. out. println(cf); } // Also includes accessor and mutator methods along with a to. String () method. } Object-Oriented Principles in Java: Part III James Tam
Package Pack 3: Class Closed. Foo package pack 3; class Closed. Foo { private boolean bool; public Closed. Foo () { bool = false; } // Also includes accessor and mutator methods along with a to. String () method. } Object-Oriented Principles in Java: Part III James Tam
Classes That Aren’t Declared As Part Of A Package Classes that aren’t explicitly associated with a particular package are implicitly part of the same package as the other classes that reside in the same directory as that class. : tamj 233 examples file. IO Integer. Wrapper Object-Oriented Principles in Java: Part III Simple. IO James Tam
Classes That Aren’t Declared As Part Of A Package Classes that aren’t explicitly associated with a particular package are implicitly part of the same package as the other classes that reside in the same directory as that class. : tamj 233 examples file. IO Integer. Wrapper Object-Oriented Principles in Java: Part III Simple. IO James Tam
Updated Levels Of Access Permissions Private “-” • Can only access field in the methods of the class where the field is originally listed. Protected “#” • Can access field in the methods of the class where the field is originally listed or the subclasses of that class Package - no UML symbol for this permission level • Can access the field or method from classes within the same package • If the level of access is unspecified this is the default level of access Public “+” • Can access field anywhere in the program Object-Oriented Principles in Java: Part III James Tam
Updated Levels Of Access Permissions (Tabular Form) Accessible to Same class Class in same package Access level Public Protected Package Private Subclass in a Not a different subclass, package different package Yes Yes No Yes No No No Object-Oriented Principles in Java: Part III James Tam
Summary You should now know: • Inheritance • Why use inheritance? When to use inheritance? • How does inheritance work in Java? • What other types of relations (besides inheritance) can exist between classes? • Method overloading vs. method overriding • Protected levels of access permissions • Shadowing data fields • Casting classes • Calling methods of the parent class vs. calling methods of the child • The effect of the final modifier in terms of inheritance Object-Oriented Principles in Java: Part III James Tam
Summary (2) • State • What determines the state of a class? • How are states represented with State diagram? • Creating new exceptions by inheriting existing exception classes • Interfaces vs. Classes • How Interfaces are used in the design process? • Similarities and differences between abstract classes and interfaces • Packages • What is the purpose of using packages? • How are classes associated with particular packages? • Updated levels of access permissions (4 levels). Object-Oriented Principles in Java: Part III James Tam
- Slides: 105