Inheritance & Polymorphism Procedural vs. object oriented Designing for Inheritance Test your Design Inheritance syntax **Practical ** Polymorphism Overloading methods
Our First Example There will be shapes on a GUI, (lets assume the shapes needed for now are a circle, a square, and a triangle), when the user clicks on the shape, the shape will rotate clockwise 360 ( all the way around), and play a sound file specific to that shape.
Procedural programmer tools: Basic question: What are the things this program has to do?? Rotate && Playsound Loops: Statements: Branches
Procedural Programmer The initial design will look like this : rotate (shapenum) { // Make the shape rotate 360 playsound (shapenum) { // use shapenum to decide which sound file // to play and play it, you can use switch or if- // then statements here ( branching)
Object Oriented Programmer Basic questions: What are the things in the program? who are the key players?? Shapes The initial design will look like this: Square rotate() { //code to rotate a //square playsound() { // code to play the //Square sound file Circle rotate() { //code to rotate a //Circle playsound() { // code to play the //Circle sound file Triangle rotate() { //code to rotate a //triangle playsound() { // code to play the //Triangle sound file
One small modification We are going to add another shape, The new shape is called a cloud. The cloud shape is supposed to rotate around a point on one end. (all shapes used to rotate around the center point) Before After
Modifications applied: Procedural program: rotate (shapenum, xpt, ypt) { // if the shape is not a cloud //calculate the center point then //rotate //else // use the xpt, ypt as the //rotation point offset, then // rotate playsound (shapenum) { // use shapenum to decide which //sound file to play and play it Object oriented program: Add: int xpoint; int ypoint; Cloud rotate() { //code to rotate a //cloud using x, y playsound() { // code to play the //Cloud sound file
Choosing the final design Obviously both designs can do the job but when more modifications are required: the procedural program expands and a lot of code is effected. Testing, recompiling the whole program all over again, things that used to work, didn t. In the object oriented program, we maintained four different rotate methods!!! Cloud Square Circle Triangle int xpoint; int ypoint; rotate() { //code to rotate a //square playsound() { // code to play the //Square sound file rotate() { //code to rotate a //Circle playsound() { // code to play the //Circle sound file rotate() { //code to rotate a //triangle playsound() { // code to play the //Triangle sound file rotate() { //code to rotate a //cloud using x, y playsound() { // code to play the //Cloud sound file
Object Oriented final design: Super class (More abstract) Sub classes (more specific) Shape rotate() { //code to rotate playsound() { // code to play the //sound file Square Circle Triangle int xpoint; int ypoint; Cloud rotate() { //code to rotate a //cloud using x, y
Things you need to know Inheritance is one of the most important feature of Object Oriented programming Inheritance is :- Software reusability. creating new class by absorbing an existing class s members and embellishing them with new or modified capabilities.
Designing for Inheritance The animals kingdom example : Assumptions: We've been given a list of some of the animals that will be in the program,but not all of them. We want other programmers to be able to add new kinds of animals to the program at any time. The current list of animals:
First step: Variables : Food, Location, picture Methods: makenoise (), eat(), roam(), sleep(). Food Location Picture Animal Roam() Sleep() Wolf Dog Jaguar Lion tiger Cat rabbit
Which methods should we override??? No, but it seems that this programmer is just a beginner!!! Mom, Is this 4 real?? Are we supposed to eat plants?
Overriding Food Location Picture Animal Roam() Sleep() Wolf Dog Jaguar Lion tiger Cat rabbit
Looking for more inheritance opportunities Big cats: WAILAK YALLY ET3ADEENA!!!! maybe am a bit smaller but we belong to the same family
Design that makes sense: Food Location Picture Animal Roam() Sleep() Big cats Roam() rabbit Dogs Family Roam() Dog Jaguar Lion tiger Cat Wolf
Which method is called? Wolf w = new Wolf(); w.; w.roam(); Food Location Picture Animal Roam() Sleep() Dogs Family Roam() w.; w.sleep(); Wolf
Things you need to know Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance
Test your design (IS-A Test) Triangle extends shape // Triangle IS-A shape Lion extends BigCat //Lion IS-A Big Cat Oven extends Kitchen // sounds reasonable until you apply the IS-A test Oven and kitchen are related but not through inheritance, Oven and kitchen are joined by a HAS-A relationship.
Advantages of inheritance Minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. A better organization of code and smaller, simpler compilation units.
Inheritance syntax Public class <subclass name> extends <superclass name> Public class person {.. Public class employee extends person { person employee
Note: Private members superclass s private members are not inherited by its subclasses. I'm sure I declared it in the super class, but its not inherited by any of its subclasses!! Public & Protected members superclass s Public & Protected members are inherited by its subclasses.
You Try Person Student
Output: Private access example: \Student.java:15: name has private access in Person {return name; ^ 1 error
Class object Every class in Java extends an existing class called Object (i.e. Every class inherits Object s methods) New class implicitly extends Object if it does not extend another class The Java compiler sets the superclass of a class to Object when the class declaration does not explicitly extend a superclass.
Construction under Inheritance Since every object inherits from the Object class, the Object() constructor is always called implicitly. However, you can call a superclass constructor explicitly using the built in super keyword, as long as it is the first statement in a constructor.
You Try
Output:
Polymorphism comes from Greek Poly Many Polymorphism Morph forms or shapes
Polymorphism Polymorphism enables us to "program in the general" rather than "program in the specific. Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics.!!!!????!
Cat mycat = new Cat(); Back to the ZOo Declare a reference variable Cat Create an object Cat Object Link the object and the reference Cat Cat Object
Polymorphism The important point is that the reference type && the object type are the same. Cat Cat Object But with polymorphism, the reference type and the object can be different. Animal mycat = new Cat(); Animal Cat Object
Polymorphism example Animal[] animals = new animal[5]; animals [0] = new Dog(); animals [1] = new Cat(); animals [2] = new Rabbit(); animals [3] = new Wolf(); for( int i=0 ; i<animals.length ; i++) { animals[i].eat(); animals[i].roam();
Overloading a method Method overloading is nothing more than having two methods with the same name but different argument lists. It has nothing to do with inheritance and polymorphism. An overloaded method is not the same as an overridden method.
Another Polymorphism example:
TestPolymorphism.java Output: