Object Oriented Programming

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Peyman Dodangeh Sharif University of Technology Fall 2014.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Polymorphism.
Advanced Programming in Java
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Inheritance, Polymorphism, and Interfaces
More inheritance, Abstract Classes and Interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces. Oh My
Java Inheritance.
Polymorphism.
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Advanced Programming in Java
Presentation transcript:

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

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();

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

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); ... }

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.

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

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(); // result: A b.signature(); // result: B, A. ((A)b).signature(); // 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{ class Triangle extends Shape{

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());

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 abstract class Shape{ public abstract double area(); } class Rectangle extends Shape{ public double area() { ... }; class Circle extends Shape{ class Triangle extends Shape{

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