Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
Lecture 7: Code Reuse Mustafa Emre İlal

2 Recap Last week: Recap of Arrays + Strings In-class coding session
Assignment 06

3 Today Assignment 06- Solution Reusing classes
Composition Inheritance Thinking in Java – Chapter 6, 7

4 Composition Nesting objects within other objects
Composing a whole from pieces Associations We already used in (Circle – Point) Circle Point

5 Inheritance Writing more detailed versions of a general class
Generalization – Specialization Super classes = “general" Sub classes= “special" Animal Mammal Horse Cat Dog

6 Differences Composition Inheritance You decide for your model
Part relationship "has a" Inheritance Further Classifying an existing class "is a" You decide for your model You will use both

7 Example - inheritance class Animal{ int age; public boolean isAlive();
} class Mammal extends Animal{ int numOfBirths; public int numOfChildren(); class Horse extends Mammal{ Stable stable; public void attachShoe(); class Cat extends Mammal{ int numOfMiceCaught; public void meow();

8 Example - inheritance Horse thunder = new Horse();
Cat garfield = new Cat(); thunder.age = 2; garfield.age = 12; if (thunder.isAlive()) { ... } int x = thunder.numOfBirths; int y = garfield.numOfChildren(); thunder.stable = "Karacabey"; garfield.numMiceCaught; garfield.stable = “Narlıdere DullArt" //error thunder.meow(); //error

9 Constructors class Animal{ Animal(int lifespan) { ... } }
class Mammal extends Animal{ Mammal(int averageLifeSpan) { super(Math.random*averageLifeSpan); .... } class Cat extends Mammal{ Cat() { super( 12 ) ; // super() HAS TO be .... } // the first line!! class Horse extends Mammal{ Horse() { super(20); ... }

10 protected Fields and methods that are: Private Protected
can only be used within the defining class. Protected can be used by that class and all its subclasses.

11 Cast Upcast Downcast 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";

12 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)

13 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"

14 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(".");

15 Example - overriding A a = new A(); B b = new B();
a.signature(); // result: A b.signature(); // result: B, A. ((A)b).signature(); // result: B, A.

16 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.

17 Example - polymorphism
class Shape{ area() } class Rectangle extends Shape{ area() {}; class Circle extends Shape{ class Triangle extends Shape{

18 Example - polymorphism
Shape[] shapes = new Shape[3]; shapes [0] = new Rectangle(); shapes [1] = new Circle(); shapes [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());

19 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.

20 Example - abstract abstract class Shape{
public abstract double area(); } class Rectangle extends Shape{ public double area() { ... }; class Circle extends Shape{ class Triangle extends Shape{

21 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

22 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

23 Next Week Interfaces Inner classes
Preperation: Thinking in Java Chapter 8 + 9


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google