Object Oriented Programming Lecture 7 Code Reuse Mustafa
Object Oriented Programming Lecture 7: Code Reuse Mustafa Emre İlal emreilal@iyte. edu. tr
Recap • Last week: – Recap of Arrays + Strings – In-class coding session – Assignment 06
Today • Assignment 06 - Solution • Reusing classes – Composition – Inheritance • Thinking in Java – Chapter 6, 7
Composition – – Nesting objects within other objects Composing a whole from pieces Associations We already used in (Circle – Point) Circle Point
Inheritance – – Writing more detailed versions of a general class Generalization – Specialization Super classes = “general" Sub classes= “special" Animal Mammal Horse Cat Dog
Differences • Composition – Part relationship – "has a" • Inheritance – Further Classifying an existing class – "is a" • You decide for your model • You will use both
Example - inheritance class Animal{ int age; public boolean is. Alive(); } class Mammal extends Animal{ int num. Of. Births; public int num. Of. Children(); } class Horse extends Mammal{ Stable stable; public void attach. Shoe(); } class Cat extends Mammal{ int num. Of. Mice. Caught; public void meow(); }
Example - inheritance Horse thunder = new Horse(); Cat garfield = new Cat(); thunder. age = 2; garfield. age = 12; if (thunder. is. Alive()) {. . . } int x = thunder. num. Of. Births; int y = garfield. num. Of. Children(); thunder. stable = "Karacabey"; garfield. num. Mice. Caught; garfield. stable = “Narlıdere Dull. Art" //error thunder. meow(); //error
Constructors class Animal{ Animal(int lifespan) {. . . } } class Mammal extends Animal{ Mammal(int average. Life. Span) { super(Math. random*average. Life. Span); . . } } class Cat extends Mammal{ Cat() { super( 12 ) ; // super() HAS TO be. . } // the first line!! } class Horse extends Mammal{ Horse() { super(20); . . . } }
protected • Fields and methods that are: – Private • can only be used within the defining class. – Protected • can be used by that class and all its subclasses.
Cast • Upcast – Generalization – From Horse to Animal – Every Horse is an Animal therefore automatic • Downcast – Specialization – From Animal to Horse – Since not all Animals are Horses, the programmer has the responsibility for the conversion Animal a = new Horse(); ((Horse)a). stable = "Karacabey";
final • final can have various effects depending on context • final Fields – Once initialized, cannot be altered – Useful for constant values (Pi) – References can never be changed to point to another object, but the object pointed to can be changed – Parameters can also be declared final • final Methods – Cannot be redefined by the subclasses (can be "overridden") – All private methods are already final • final classes – Cannot be specialized (will never have subclasses)
overriding • A method that has been developed in the superclass can be changed in the subclass • When rewriting methods for the subclass, you can call the superclass’s method with the super. method() call that can be at any line. • Should not confuse "Overriding“ with"overloading"
Example - overriding class A { void signature() { System. out. print("A"); } } class B extends A { void signature(int i) {} //overload void signature() { //override System. out. print("B, "); super. signature(); System. out. print(". "); } }
Example - overriding A a = new A(); B b = new B(); a. signature(); b. signature(); ((A)b). signature(); // result: A // result: B, A.
polymorphism • Means “many forms” • Which method body to execute is decided by Java during execution (at run time) not while the code is being compiled (at compile time) - this is called "late-binding" • The class of an object is discovered and the method in that class is executed. Even casting the object to another class will not change this. • Thanks to this mechanism, you can group objects from different classes together and still call (invoke) the specialized methods while referring to them as an object of the most general class.
Example - polymorphism class Shape{ area() } class Rectangle extends Shape{ area() {}; } class Circle extends Shape{ area() {}; } class Triangle extends Shape{ area() {}; }
Example - polymorphism Shape[] shapes = new Shape[3]; [0] = new Rectangle(); [1] = new Circle(); [2] = new Triangle(); void report(Shape s) { System. out. println( s. area() ); } for (i=0; i< shapes. length; i++) { report(shapes[i] ); System. out. println(shapes[i]. area()); }
abstract • Abstract classes and concepts • Have no instances. Never need Objects of these classes! • Are designed to be specialized by sub classes • Abstract classes have abstract methods that have no bodies. Concrete sub classes have to override these and provide a body for each.
Example - abstract class Shape{ public abstract double area(); } class Rectangle extends Shape{ public double area() {. . . }; } class Circle extends Shape{ public double area() {. . . }; } class Triangle extends Shape{ public double area() {. . . }; }
Assignment 07 • Improve Assignment 5 – Create 100 Shapes • • – – Randomly decide if it will be a Circle, Rectangle, or Triangle Names: Circle_xxx; Rectangle_xxx; Triangle_xxx Find the total area and average area of all shapes Count how many of each class under each color Sort the red shapes according to their areas Find the shape with the largest circumference
Assignment 06 • Add new Classes – Shape(abstract) • Abstract methods – area() – circumference() • protected variables and public methods that are NOT abstract – Rectangle, Triangle (as sub class) • Provide a body for existing abstract methods – Circle • Alterations that are suitable for rectangle and circles
Next Week • Interfaces • Inner classes • Preperation: Thinking in Java Chapter 8 + 9
- Slides: 23