A Code Reuse Approach to Inheritance. Reusing Code – Lowest Level Copy/paste parts/all into your program –Maintenance problem Need to correct code in.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
DRY. 11: DRY—Don’t Repeat Yourself Every piece of knowledge should have a single, unambiguous, authoritative representation within a system Duplication.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Object Oriented Software Development
Inheritance using Java
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Agenda Object Oriented Programming Reading: Chapter 14.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Inheritance ITI1121 Nour El Kadri.
Chapter 11 Object-Oriented Design
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Classes and Objects, State and Behavior
Lecture 22 Inheritance Richard Gesick.
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
Classes, Encapsulation, Methods and Constructors (Continued)
Topic 29 classes and objects, part 3
Building Java Programs
Building Java Programs
Fundaments of Game Design
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Topic 29 classes and objects, part 3
CSG2H3 Object Oriented Programming
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

A Code Reuse Approach to Inheritance

Reusing Code – Lowest Level Copy/paste parts/all into your program –Maintenance problem Need to correct code in multiple places Too much code to work with (lots of versions) –High risk of error during process –May require knowledge about how the used software works –Requires access to source code

Using Java Classes A class is an atomic unit of code resuse Source code not necessary, class file or jar/zip Just need to include in the classpath Can use javap tool to get a class’s public method headers Documentation very important (Java API) Encapsulation helps reuse Less code to manage Versioning, backwards-compatibility still problem Need to package related classes together

Using Classes The simplest form of using classes is calling its methods This form of relationship between 2 classes is called “uses-a” relationship Uses (in which one class makes use of another without actually incorporating it as a property -it may, for example, be a parameter or used locally in a method) classA uses classB

Reusing Classes - Composition A closer form of reuse is composition Composition (or has_a in which one class has another as a property/instance variable) classA <> classB

Position/Ball example public class Position { private double x,y; public Position(double x, double y) { this.x = x; this.y = y; } // getter (accessor methods) public double getX() { return x; } public double getY() { return y; } // setter methods public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; }

public class Ball { private Position position; private double vx, vy; // speed per unit time private double radius; public Ball(Position pos, double vx, double vy) { position = pos; this.vx = vx; this.vy = vy; } // moves the ball specified number of time units public void move(int t) { double newx = position.getX() + vx * t; double newy= position.getY() + vy * t; position.setX(newX); position.setY(newY); }

Composition A Ball object has a Position associated with it, or Ball has-a Position Ball Position

ShrinkingBall Suppose we need a new Ball class that shrinks a certain amount every time unit. It is clear that we should use some of the code from the Ball class since they are so similar Let’s look at the alternatives of code- resue: copy-paste, uses relation, composition

public class ShrinkingBall { private Position position; private double vx, vy, radius; private double shrinkRate; //constructor public Ball(Position pos, double vx, double vy, double shRate) { position = pos; this.vx = vx; this.vy = vy; shrinkRate = shRate; } // moves the ball specified number of time units public void move(int t) { position.setX(position.getX() + vx * t); position.setY(position.getY() + vy * t); radius -= t * shrinkRate; } Copy-paste method :

Using the Ball class Since we don’t know how to act in some situations, we can create a ball object, see how it behaves, and mimic its responses

public class ShrinkingBall { private Position position; private double vx, vy, radius; private double shrinkRate; //constructor public ShrinkingBall(Position pos, double vx, double vy, double shRate) { Ball ball = new Ball(pos, vx, vy); position = ball.getPosition(); this.vx = ball.getVx(); this.vy = ball.getVy(); shrinkRate = shRate; } // moves the ball specified number of time units public void move(int t) { Ball ball = new Ball(position, vx, vy); ball.move(t); position.setX(ball.getPosition().getX()); position.setY(ball.getPosition().getY()); radius -= tu * shrinkRate; }

Using Composition It seems strange to create a new instance of Ball every time we need it. Besides, our properties and the Ball’s are very similar. We can just keep an internal Ball object around that will capture all our ball-related properties

Using Composition public class ShrinkingBall { private Ball ball; private double shrinkRate; public void move(int tu) { ball.move(tu); ball.radius -= tu * shrinkRate; // ? } public Position getPosition() { return ball.getPosition(); } public double getRadius() { return ball.getRadius(); } …

ShrinkingBall example In our solution, we hide a Ball inside each ShrinkingBall For each Ball method that we also need for ShrinkingBall, we need to write a new method that passes the job to the hidden Ball object (seems like a boring job) ShrinkingBall behaves a lot like a regular Ball except the way it moves

Inheritance Java provides another way of code reuse named Inheritance In inheritance, classes extend the properties/behavior of existing classes In addition, they might override/redefine existing behavior

ShrinkingBall with Inheritance Public class ShrinkingBall extends Ball { private double shrinkRate; // new property public void move(int tu) { // move overriden super.move(tu); radius -= tu * shrinkRate; } … }

Inheritance No need to put dummy methods that just forward or delegate work Captures the real world better Usually need to design inheritance hierarchy before implementation Cannot cancel out properties or methods, so must be careful not to overdo it

Protected Access What about the line “radius -= …. ?” Since radius is private, cannot modify it It wouldn’t make sense ShrinkingBall not to be able to modify its own radius Can use protected access for radius, so that it is private for all classes except ones that extend it.