Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Programming

Description:

Object Oriented Programming Ders 7: Code Reuse Mustafa Emre lal emreilal_at_iyte.edu.tr Recap Last week: Arrays + Strings Assignment 05 Today Assignment 05- Solution ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 24
Provided by: Ersan6
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming
  • Ders 7 Code Reuse
  • Mustafa Emre Ilal
  • emreilal_at_iyte.edu.tr

2
Recap
  • Last week
  • Arrays Strings
  • Assignment 05

3
Today
  • Assignment 05- 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"

6
Differences
  • Composition
  • 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 numOfCatsCaught
  • 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"
  • garield.numCatsCaught
  • garfield.stable Narlidere DullArt" //error
  • thunder.meow() //error

9
Constructors
  • class Animal
  • Animal(int lifespan) ...
  • class Mammal extends Animal
  • Mammal(int averageLifeSpan)
  • super(Math.randomaverageLifeSpan)
  • ....
  • 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
  • can only be used within the defining class.
  • Protected
  • can be used by that class and all its subclasses.

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

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 superclasss 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 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
  • area()
  • class Triangle extends Shape
  • area()

18
Example - polymorphism
  • Shape shapes new Shape3
  • shapes 0 new Rectangle()
  • shapes 1 new Circle()
  • shapes 2 new Triangle()
  • void report(Shape s)
  • System.out.println( s.area() )
  • for (i0 ilt shapes.length i)
  • report(shapesi )
  • System.out.println(shapesi.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
  • public double area() ...
  • class Triangle extends Shape
  • public double area() ...

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 07
  • 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
Write a Comment
User Comments (0)
About PowerShow.com