logging in or signing up 01 L4InheritanceI GE 06 Breezy Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 73 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: November 17, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide1: Object-oriented Programming Overview The inheritance relationship: is-a Method overriding Visibility Modifiers Polymorphism and dynamic method binding The keyword super Variable shadowing Constructors under inheritance Single implementation inheritance Multiple interface inheritance and supertypes Assigning, and casting references The instanceof operatorSlide2: What is “inheritance”?Slide3: Inheritance Natural, hierarchical way of organizing things. Think in terms of “is a” relationships: A Lion is a Cat, and so is a Tiger A Labrador is a Retriever is a Dog is a Animal A Human is a(n) Biped is a Animal. (subclass of… ) (subclass of … ) (subclasses of Animal) (superclass) Animal Dog Cat Biped Retriever Guard Lion Tiger Human Labrador German ShepherdSlide4: Another hierarchy Light Light Bulb Tube Light SpotLight Bulb NeonLight Artificial Light Natural Light Inheritance defines the relationship is-a (also called superclass-subclass relationship) between a superclass and a subclass. Classes higher up are more generalized as they abstract the class behavior. Classes lower down are more specialized as they customize the inherited (abstract) behavior using additional properties and behavior.Slide5: Inheritance - Overview Class A Class B Class A Class B inherits from Class A, or Class B extends Class A Class A is the more general form while Class B is a specialization. Class B contains: All of the properties of A Its own methods, not found in A (EXTENDING class A) Methods of A that were redefined in B. (OVERRIDING) Class B must belong to the same family as A. You must be able to say: “B is a A”Slide6: The Java Object class All classes in Java Inherit from the Object class Any object is-a(n) Object The Object class is always the root of any inheritance hierarchySlide7: A simple example of Inheritance Foo f = new Foo(); System.out.println(f.getClass()); System.out.println(f.toString()); public class Foo{ public String toString(){ return “this is foo”; } } Java console java.lang.Foo this is foo inheritance overriding Reason possible UML notation for inheritance between classes. The arrow originates at the class that is inheriting (the child).Slide8: Method Overriding If a class, referred to as Child, inherits from another class, referred to as Parent, and if the Parent has a non-static,non-private, non-final method and the Child also has an identical method, then the Child’s method will replace (Override) that of the Parent in a Child object. This characteristic of replacing is called Method Overriding. public class Parent{ public void foo(){ System.out.println(“I am a parent”); } } public class Child extends Parent{ public void foo(){ System.out.println(“I am a child”); } } public class Test{ public … main(String[] args){ Child c = new Child(); c.foo(); } } Output I am a child Warning: Don’t get Overriding and Overloading mixed up! Reason Method OverridingSlide9: Method Overriding… A subclass may override non-static, non-private, and non-final methods inherited from a superclass. (So that also means that any static, private, or final method cannot be overridden by a subclass.) When a method is invoked on an object of the subclass, it is the new method definition in the subclass that is executed. The new method definition in the subclass must have the same method signature (i.e. method name and parameters), and the same return. The new definition cannot “narrow” the accessibility of the method, but it can widen it. The new method definition can only specify all or a subset of the exception classes specified by the throws clause of the overridden method. A subclass can use the keyword super to invoke an overridden method in the superclass. Slide10: Overriding verses Overloading - what's the difference? Method overriding requires the same method signature, the same return type and the original method was inherited from its superclass. Overloading requires different method signatures, but the method name should be the same. (To overload, the parameters must be different in type and/or number. The return type is not a part of the signature, changing it is not enough to overload methods.) A method can be overloaded in the class it is defined in, or in a subclass. To invoke an overridden method in the superclass from a subclass, requires the use of super.Slide11: Example Want to model relationships between the following classes: Animal Cat Dog Biped Lets assume that all of these objects have have a name make a sound and perform in some fashion Inheritance relationship: Group common properties high in the inheritance hierarchy so that all sub classes inherit these. Design goals: Even if implementation is unknown, include method stubs high in the inheritance hierarchy so that sub classes can inherit and override these.Slide12: Example … class Animal { protected String strName = “”; protected String strNoise = “”; protected int iNumTimesPerformed = 0; protected static int population = 0; //assume constructors public void setName(String strName){ this.strName = strName; } public String getName(){ return strName; } public void setNoise(String noise){…} … public void identifySelf( ) { System.out.println(“My name is “ + strName); } // of identifySelf public void perform ( ) { doYourThing( ); iNumTimesPerformed++; } // of perform public void doYourThing( ) { ; // ‘no-op’ method } // of doYourThing } // of Animal So, animals have a name and noise and they can identify themselves, perform and do their thing. Animal harpo = new Animal(); harpo.setName(“Harpo”); harpo.doYourThing(); // says nothingSlide13: Subclasses (Dog extends Animal i.e. “A dog is an animal” or “All dogs are animals”) class Dog extends Animal { public Dog() { strNoise = “Woof”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing } // of Dog Recall: The Animal class had a no-op method for doYourThing() Dog pickles = new Dog(); pickles.setName(“Pickles”); pickles.doYourThing(); // output: // “My name is Pickles” // “I am a dog” // “Woof” Example … Slide14: Subclasses (Cat extends Animal i.e. “A cat is an animal” or “All cats are animals”) class Cat extends Animal { public Cat() { strNoise = “Miaow”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a cat”); System.out.println(strNoise); } // of doYourThing } // of Cat Cat abby = new Cat(); abby.setName(“Abby”); abby.doYourThing(); // output: // “My name is Abby” // “I am a cat” // “Miaow” Example … Slide15: Subclasses (Biped extends Animal i.e. “A Biped is an animal” or “All Bipeds are animals”) class Biped extends Animal { public Biped() { strNoise = “I walk on two legs therefore I am”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I walk upright”); System.out.println(strNoise); } // of doYourThing } // of Human Biped descartes = new Biped(); Biped.setName(“Rene”); descartes.doYourThing(); // output: // “My name is Rene” // “I walk upright” // “I walk on two legs therefore I am” Example … Slide16: Inheritance and Scope Variables (e.g. strNoise): First examines current method, looks for local variable or parameter; Then examines current class (e.g. Dog); Then examines superclass (e.g. Animal); Continues up the class hierarchy until no more superclasses to examine. Methods (e.g. doYourThing() or identifySelf()): First examines current class; Then examines superclass; Continues up inheritance hierarchy until no more superclasses to examine.Slide17: “An object of a subclass can be used wherever an object of the superclass can be used.” Polymorphism and Dynamic method Binding What does this mean? Light l; TubeLight tl = new TubeLight(); l = tl; Why is this “reasonable”? Example: An object of TubeLight can be used whenever an object of the superclass, Light, can be used, since TubeLight is-a Light Polymorphism and Dynamic Method Binding The ability of a superclass reference to denote objects of its own class and those of its subclasses at runtime is called polymorphism. The method invoked is dependent on the actual type of the object denoted by the reference at runtime.Slide18: public class Parent{ protected int a; public Parent(){ a = 0; } public void doStuff(){ System.out.println(“Parent do stuff”); } public void parentOnlyMethod(){ System.out.println(“Parent only method”); } } public class Child extends Parent{ protected int b; public Child (){ a = 0; } public void doStuff(){ System.out.println(“Child do stuff”); } public void childOnlyMethod(){ System.out.println(“Child only method”); } } Observe Both classes have a “doStuff” method Both classes have methods that are unique to them Client Code Child child = new Child(); child.doStuff(); child.parentOnlyMethod(); child.childOnlyMethod(); Part 1: What happens now? Reasoning Constructor method overriding Inherited Extending parent Polymorphism … Slide19: public class Parent{ protected int a; public Parent(){ a = 0; } public void doStuff(){ System.out.println(“Parent do stuff”); } public void parentOnlyMethod(){ System.out.println(“Parent only method”); } } public class Child extends Parent{ protected int b; public Child (){ a = 0; } public void doStuff(){ System.out.println(“Child do stuff”); } public void childOnlyMethod(){ System.out.println(“Child only method”); } } Part 2: What happens now? Child child = new Child(); child.doStuff(); child.parentOnlyMethod(); child.childOnlyMethod(); Client Code Parent p = child; p.doStuff(); p.parentOnlyMethod(); p.childOnlyMethod(); child = (Child) p; Reasoning bc, Child is-a Parent- Polymorphism, Upcasting Method overriding Inheritance ERROR! Down Casting - Dangerous! Polymorphism … Slide20: Parent p = child; (or Parent p = new Child();) is possible because of inheritance. the “Child” is a “Parent”. Therefor, a parent reference can reference a child object. This is also known as Upcasting. p.doStuff(); Because of method overriding. The “doStuff” method is redefined in the “Child”. In order to get method overriding, the signature have to match. p.parentOnlyMethod(); Because of inheritance. The subclass “Child” inherits the method “parentOnlyMethod()” p.childOnlyMethod(); Not accessible through a Parent reference. Methods exclusively in the subclass cannot be accessed via a super class reference. Polymorphism … Slide21: child = (Child) p; Called Down casting. This requires an explicit cast. Why? Warning: May through a “ClassCastException” Polymorphism … Slide22: A second example - what’s the output? Reason through the statements class client{ public static void main(String args[]){ String stringRef = new String(“Java”); //(1) System.out.println(“(2): “+stringRef.getClass()); //(2) System.out.println(“(3): “+stringRef.length()); //(3) Object objRef = stringRef; //(4) System.out.println(“(5): “+objRef.equals(“Java”)); //(5) System.out.println(“(6): “+objRef.getClass()); //(6) stringRef = (String) objRef; //(7) System.out.println(“(8): “+stringRef.equals(“C++”); //(8) System.out.println(“(9): “+objRef.length()); //(9) } } Output from the program (2): class java.lang.String (3): 4 (4): (5): true (6): class java.lang.String (7) (8): false (9): Reasoning (2): Inheritance (3): Extending/Local method (4): Up casting/Polymorphism (5): Overriding (6): Inheritance (7): Down casting (8): Overriding (9): Error!Slide23: An object of a subclass can be used wherever an object of the superclass can be used. The inheritance relationship is Transitive: if class B extends class A, and class C extends B, then C inherits from A via B (An object of SpotLight is-a Light) The is-a relationship does not hold between peer classes. (A Dog is-NOT-a cat) Litmus test for using inheritance: if B is a(n) A, then let B inherit from A. Do not use inheritance unless all inherited behavior makes sense Implications of Inheritance You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
01 L4InheritanceI GE 06 Breezy Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 73 Category: Entertainment License: All Rights Reserved Like it (0) Dislike it (0) Added: November 17, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide1: Object-oriented Programming Overview The inheritance relationship: is-a Method overriding Visibility Modifiers Polymorphism and dynamic method binding The keyword super Variable shadowing Constructors under inheritance Single implementation inheritance Multiple interface inheritance and supertypes Assigning, and casting references The instanceof operatorSlide2: What is “inheritance”?Slide3: Inheritance Natural, hierarchical way of organizing things. Think in terms of “is a” relationships: A Lion is a Cat, and so is a Tiger A Labrador is a Retriever is a Dog is a Animal A Human is a(n) Biped is a Animal. (subclass of… ) (subclass of … ) (subclasses of Animal) (superclass) Animal Dog Cat Biped Retriever Guard Lion Tiger Human Labrador German ShepherdSlide4: Another hierarchy Light Light Bulb Tube Light SpotLight Bulb NeonLight Artificial Light Natural Light Inheritance defines the relationship is-a (also called superclass-subclass relationship) between a superclass and a subclass. Classes higher up are more generalized as they abstract the class behavior. Classes lower down are more specialized as they customize the inherited (abstract) behavior using additional properties and behavior.Slide5: Inheritance - Overview Class A Class B Class A Class B inherits from Class A, or Class B extends Class A Class A is the more general form while Class B is a specialization. Class B contains: All of the properties of A Its own methods, not found in A (EXTENDING class A) Methods of A that were redefined in B. (OVERRIDING) Class B must belong to the same family as A. You must be able to say: “B is a A”Slide6: The Java Object class All classes in Java Inherit from the Object class Any object is-a(n) Object The Object class is always the root of any inheritance hierarchySlide7: A simple example of Inheritance Foo f = new Foo(); System.out.println(f.getClass()); System.out.println(f.toString()); public class Foo{ public String toString(){ return “this is foo”; } } Java console java.lang.Foo this is foo inheritance overriding Reason possible UML notation for inheritance between classes. The arrow originates at the class that is inheriting (the child).Slide8: Method Overriding If a class, referred to as Child, inherits from another class, referred to as Parent, and if the Parent has a non-static,non-private, non-final method and the Child also has an identical method, then the Child’s method will replace (Override) that of the Parent in a Child object. This characteristic of replacing is called Method Overriding. public class Parent{ public void foo(){ System.out.println(“I am a parent”); } } public class Child extends Parent{ public void foo(){ System.out.println(“I am a child”); } } public class Test{ public … main(String[] args){ Child c = new Child(); c.foo(); } } Output I am a child Warning: Don’t get Overriding and Overloading mixed up! Reason Method OverridingSlide9: Method Overriding… A subclass may override non-static, non-private, and non-final methods inherited from a superclass. (So that also means that any static, private, or final method cannot be overridden by a subclass.) When a method is invoked on an object of the subclass, it is the new method definition in the subclass that is executed. The new method definition in the subclass must have the same method signature (i.e. method name and parameters), and the same return. The new definition cannot “narrow” the accessibility of the method, but it can widen it. The new method definition can only specify all or a subset of the exception classes specified by the throws clause of the overridden method. A subclass can use the keyword super to invoke an overridden method in the superclass. Slide10: Overriding verses Overloading - what's the difference? Method overriding requires the same method signature, the same return type and the original method was inherited from its superclass. Overloading requires different method signatures, but the method name should be the same. (To overload, the parameters must be different in type and/or number. The return type is not a part of the signature, changing it is not enough to overload methods.) A method can be overloaded in the class it is defined in, or in a subclass. To invoke an overridden method in the superclass from a subclass, requires the use of super.Slide11: Example Want to model relationships between the following classes: Animal Cat Dog Biped Lets assume that all of these objects have have a name make a sound and perform in some fashion Inheritance relationship: Group common properties high in the inheritance hierarchy so that all sub classes inherit these. Design goals: Even if implementation is unknown, include method stubs high in the inheritance hierarchy so that sub classes can inherit and override these.Slide12: Example … class Animal { protected String strName = “”; protected String strNoise = “”; protected int iNumTimesPerformed = 0; protected static int population = 0; //assume constructors public void setName(String strName){ this.strName = strName; } public String getName(){ return strName; } public void setNoise(String noise){…} … public void identifySelf( ) { System.out.println(“My name is “ + strName); } // of identifySelf public void perform ( ) { doYourThing( ); iNumTimesPerformed++; } // of perform public void doYourThing( ) { ; // ‘no-op’ method } // of doYourThing } // of Animal So, animals have a name and noise and they can identify themselves, perform and do their thing. Animal harpo = new Animal(); harpo.setName(“Harpo”); harpo.doYourThing(); // says nothingSlide13: Subclasses (Dog extends Animal i.e. “A dog is an animal” or “All dogs are animals”) class Dog extends Animal { public Dog() { strNoise = “Woof”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a dog”); System.out.println(strNoise); } // of doYourThing } // of Dog Recall: The Animal class had a no-op method for doYourThing() Dog pickles = new Dog(); pickles.setName(“Pickles”); pickles.doYourThing(); // output: // “My name is Pickles” // “I am a dog” // “Woof” Example … Slide14: Subclasses (Cat extends Animal i.e. “A cat is an animal” or “All cats are animals”) class Cat extends Animal { public Cat() { strNoise = “Miaow”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I am a cat”); System.out.println(strNoise); } // of doYourThing } // of Cat Cat abby = new Cat(); abby.setName(“Abby”); abby.doYourThing(); // output: // “My name is Abby” // “I am a cat” // “Miaow” Example … Slide15: Subclasses (Biped extends Animal i.e. “A Biped is an animal” or “All Bipeds are animals”) class Biped extends Animal { public Biped() { strNoise = “I walk on two legs therefore I am”; } // of constructor public void doYourThing ( ) { identifySelf(); System.out.println(“I walk upright”); System.out.println(strNoise); } // of doYourThing } // of Human Biped descartes = new Biped(); Biped.setName(“Rene”); descartes.doYourThing(); // output: // “My name is Rene” // “I walk upright” // “I walk on two legs therefore I am” Example … Slide16: Inheritance and Scope Variables (e.g. strNoise): First examines current method, looks for local variable or parameter; Then examines current class (e.g. Dog); Then examines superclass (e.g. Animal); Continues up the class hierarchy until no more superclasses to examine. Methods (e.g. doYourThing() or identifySelf()): First examines current class; Then examines superclass; Continues up inheritance hierarchy until no more superclasses to examine.Slide17: “An object of a subclass can be used wherever an object of the superclass can be used.” Polymorphism and Dynamic method Binding What does this mean? Light l; TubeLight tl = new TubeLight(); l = tl; Why is this “reasonable”? Example: An object of TubeLight can be used whenever an object of the superclass, Light, can be used, since TubeLight is-a Light Polymorphism and Dynamic Method Binding The ability of a superclass reference to denote objects of its own class and those of its subclasses at runtime is called polymorphism. The method invoked is dependent on the actual type of the object denoted by the reference at runtime.Slide18: public class Parent{ protected int a; public Parent(){ a = 0; } public void doStuff(){ System.out.println(“Parent do stuff”); } public void parentOnlyMethod(){ System.out.println(“Parent only method”); } } public class Child extends Parent{ protected int b; public Child (){ a = 0; } public void doStuff(){ System.out.println(“Child do stuff”); } public void childOnlyMethod(){ System.out.println(“Child only method”); } } Observe Both classes have a “doStuff” method Both classes have methods that are unique to them Client Code Child child = new Child(); child.doStuff(); child.parentOnlyMethod(); child.childOnlyMethod(); Part 1: What happens now? Reasoning Constructor method overriding Inherited Extending parent Polymorphism … Slide19: public class Parent{ protected int a; public Parent(){ a = 0; } public void doStuff(){ System.out.println(“Parent do stuff”); } public void parentOnlyMethod(){ System.out.println(“Parent only method”); } } public class Child extends Parent{ protected int b; public Child (){ a = 0; } public void doStuff(){ System.out.println(“Child do stuff”); } public void childOnlyMethod(){ System.out.println(“Child only method”); } } Part 2: What happens now? Child child = new Child(); child.doStuff(); child.parentOnlyMethod(); child.childOnlyMethod(); Client Code Parent p = child; p.doStuff(); p.parentOnlyMethod(); p.childOnlyMethod(); child = (Child) p; Reasoning bc, Child is-a Parent- Polymorphism, Upcasting Method overriding Inheritance ERROR! Down Casting - Dangerous! Polymorphism … Slide20: Parent p = child; (or Parent p = new Child();) is possible because of inheritance. the “Child” is a “Parent”. Therefor, a parent reference can reference a child object. This is also known as Upcasting. p.doStuff(); Because of method overriding. The “doStuff” method is redefined in the “Child”. In order to get method overriding, the signature have to match. p.parentOnlyMethod(); Because of inheritance. The subclass “Child” inherits the method “parentOnlyMethod()” p.childOnlyMethod(); Not accessible through a Parent reference. Methods exclusively in the subclass cannot be accessed via a super class reference. Polymorphism … Slide21: child = (Child) p; Called Down casting. This requires an explicit cast. Why? Warning: May through a “ClassCastException” Polymorphism … Slide22: A second example - what’s the output? Reason through the statements class client{ public static void main(String args[]){ String stringRef = new String(“Java”); //(1) System.out.println(“(2): “+stringRef.getClass()); //(2) System.out.println(“(3): “+stringRef.length()); //(3) Object objRef = stringRef; //(4) System.out.println(“(5): “+objRef.equals(“Java”)); //(5) System.out.println(“(6): “+objRef.getClass()); //(6) stringRef = (String) objRef; //(7) System.out.println(“(8): “+stringRef.equals(“C++”); //(8) System.out.println(“(9): “+objRef.length()); //(9) } } Output from the program (2): class java.lang.String (3): 4 (4): (5): true (6): class java.lang.String (7) (8): false (9): Reasoning (2): Inheritance (3): Extending/Local method (4): Up casting/Polymorphism (5): Overriding (6): Inheritance (7): Down casting (8): Overriding (9): Error!Slide23: An object of a subclass can be used wherever an object of the superclass can be used. The inheritance relationship is Transitive: if class B extends class A, and class C extends B, then C inherits from A via B (An object of SpotLight is-a Light) The is-a relationship does not hold between peer classes. (A Dog is-NOT-a cat) Litmus test for using inheritance: if B is a(n) A, then let B inherit from A. Do not use inheritance unless all inherited behavior makes sense Implications of Inheritance