Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes.

Slides:



Advertisements
Similar presentations
Phil Campbell London South Bank University Java 1 First Steps.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Phil Campbell London South Bank University Using Java (2)
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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,
Problem!! demoCounter = new LimitedCounter( 10, 12); demoCounter.unCount(); System.out.println( “The value of demoCounter should “ + “always be between.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
RolloverCounter - class diagram LimitedCounter RolloverCounter package counters RolloverCounter count unCount maxToCount minToCount The count() and unCount()
LimitedCounter - extension & inheritance BasicCounter LimitedCounter Every LimitedCounter instance inherits every resource (attributes & methods) which.
Starting Software Development A Beginning. You Learn Software Development By Doing Software development.
Temperature class diagram. Temperature instances.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Class Diagram Notation Lecture Oo12 Classes Packages.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Java Classes Using Java Classes Introduction to UML.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
ArrayList, Multidimensional Arrays
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
10-Nov-15 Java Object Oriented Programming What is it?
Information Systems Engineering
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
Arithmetic, Class Variables and Class Methods Week 11
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CompSci 230 S Programming Techniques
Lecture 8 D&D Chapter 9 Inheritance Date.
Topic: Classes and Objects
Inheritance ITI1121 Nour El Kadri.
03/10/14 Inheritance-2.
Polymorphism 2nd Lecture
Chapter 3: Using Methods, Classes, and Objects
CompSci 230 Software Construction
Java LESSON 7 Objects, Part 1
Inheritance 2nd Lecture
Polymorphism 2nd Lecture
Lecture 22 Inheritance Richard Gesick.
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Inheritance 2nd Lecture
Tonga Institute of Higher Education
Chapter 8 Inheritance Part 2.
Chapter 5 Classes.
Presentation transcript:

Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes may not quite do what you want them to. Extending an existing class will allow you to make it do exactly what you want it to. Developing a class hierarchy from the start makes it more likely that you will find an existing class suitable to extend.

Managing Complexity Building five small classes in a hierarchy is simpler that building one large class (although it may not seem so at first!). a b c d e f a b c de f

Design, build, test... Production can be divided into a number of stages. In each stage a class is designed, built and tested before next class in the hierarchy is started. a design build test b design build test c design build test d design build test

The Counters hierarchy The Counters hierarchy supplies the functionality of a hand held counter. The BasicCounter supplies the essential counting behaviour, the LimitedCounter adds limits to the range it can count in and the three *Counter classes provide specialised counting behaviour. BasicCounter LimitedCounter RolloverCounterStoppingCounterWarningCounter RoomMonitorMoneyRegister

BasicCounter class diagram Object BasicCounter number CountedIs BasicCountercountunCountreset set CountTo initialCount occurrencessetTo theInitialCount counted package counters

BasicCounter header 0001 // Filename counters/BasicCounter.java // Root class of the Counters hierarchy providing the 0003 // essential counting functionality // 0005 // This version written for SDO lecture // Fintan Culwin, v0.1, February package counters; public class BasicCounter extends Object { Object BasicCounter package counters

BasicCounter - data attributes private int counted = 0; 0013 private int theInitialCount = 0; 0014 These are encapsulated instance attributes, of the primitive int type. Every instance of the class will have their own copy of them. theInitialCount counted

BasicCounter - constructors 0015 public BasicCounter() { 0016 this( 0); 0017 } // End default constructor public BasicCounter( int initialCount) { 0020 counted = initialCount; 0021 theInitialCount = initialCount; 0022 } // End alternative constructor. The purpose of a constructor is to place the new instance of the class into a well defined initial state. (This usually means setting the state of the instance attributes.) BasicCounter initialCount

BasicCounter - counting methods 0024 public void count() { 0025 counted++; 0026 } // End count public void unCount() { 0029 counted--; 0030 } // end unCount public void reset() { 0033 counted = theInitialCount; 0034 } // End numberCountedIs. These methods change the state of the instance by changing the values of its instance attributes. countunCountreset

BasicCounter - inquiry method 0036 public int numberCountedIs() { 0037 return counted; 0038 } // End numberCountedIs. This is an inquiry method which return s some information about the internal state of the instance. number CountedIs occurrences

BasicCounter - setCountTo() method 0040 protected void setCountTo( int setTo) { 0041 counted = setTo; 0042 } // End setCountTo } // End BasicCounter. This protected method can only be seen, and so used, by other classes in the same package of classes. set CountTo setTo

BasicCounterDemo - instance diagram Instance diagrams show the roles and relationships relationships between the class instances which make up a program. main instance of BasicCounterDemo demoCounter instance of BasicCounter demonstrates is a Object

BasicCounterDemo - constructors 0008 package counters; class BasicCounterDemonstration { public static void main( String argv[]) { BasicCounter aCounter; System.out.println( "\n\n\t Basic Counter Demonstration"); System.out.print( "\n\nConstructing an instance with “); 0019 System.out.println( "the initial value 4."); 0020 aCounter = new BasicCounter( 4); 0021 System.out.print( "Instance created... "); Basic Counter Demonstration Constructing an instance with the initial value 4. Instance created...

BasicCounterDemo - NumberCountedIs() System.out.print( "\n\nDemonstrating numberCountedIs()” “, it should be 4... "); 0026 System.out.println( aCounter.numberCountedIs()); 0027 Demonstrating numberCountedIs(), it should be

BasicCounterDemo - counting methods 0028 System.out.println( "\n\nDemonstrating count()."); 0029 aCounter.count(); 0030 System.out.print( "Showing the changed value, it should be 5... "); 0031 System.out.println( aCounter.numberCountedIs()); System.out.println( "\n\nDemonstrating unCount(). "); 0034 aCounter.unCount(); 0035 System.out.print( "Showing the changed value, it should be 4... "); 0036 System.out.println( aCounter.numberCountedIs()); Demonstrating count(). Showing the changed value, it should be Demonstrating unCount(). Showing the changed value, it should be 4...4

BasicCounterDemo - setCountTo() 0038 System.out.println( "\n\nDemonstrating setCountTo()”+ “, setting to 10."); 0039 aCounter.setCountTo( 10); 0040 System.out.print( "Showing the changed value, “ + “it should be "); 0041 System.out.println( aCounter.numberCountedIs()); As setCountTo() is protected, it can only be demonstrated if it is temporarily made public. Demonstrating setCountTo(), setting to 10. Showing the changed value, it should be

BasicCounterDemo - reset() 0043 System.out.println( "\n\nDemonstrating reset()."); 0044 aCounter.reset(); 0045 System.out.print( "Showing the reset value, it should be 4... "); 0046 System.out.println( aCounter.numberCountedIs()); Demonstrating reset(). Showing the reset value, it should be

BasicCounterDemo - second instance 0049 System.out.print( "\n\nConstructing a new instance “); 0050 System.out.println( “with the default value 0."); 0051 aCounter = new BasicCounter(); 0052 System.out.println( "New instance created..."); 0053 System.out.print( "\n\nShowing the value of the new instance,“); 0054 System.out.print( "it should be 0... "); 0055 System.out.println( aCounter.numberCountedIs()); System.out.println( "\n\nDemonstration finished.\n\n"); 0058 } // End main } // End class BasicCounterDemonstration. Constructing a new instance with the default value 0. New instance created... Showing the value of the new instance, it should be Demonstration finished.