CompSci 100 4.1 Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape,

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Programming Logic and Design Fourth Edition, Introductory
CS 211 Inheritance AAA.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ITEC200 – Week03 Inheritance and Class Hierarchies.
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,
Object Oriented System Development with VB .NET
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Transformation of Java Card into Diet Java Semester Project Presentation Erich Laube.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Object Oriented Software Development
CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.
MVC pattern and implementation in java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 See online syllabus (also available through BlueLine): Course goals:
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
CPS 100, Spring Conventions in Compsci 100 projects l We want you to concentrate on algorithms and data structures  Not on rendering fonts, interacting.
Choose between Access and Excel Right questions, right program If you’re having trouble choosing between Access and Excel, take a moment to answer an important.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
Object-Oriented Programming (OOP) CSC-2071 (3+1=4 Credits) Lecture No. 1 MBY.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Programming in Java CSCI-2220 Object Oriented Programming.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
M1G Introduction to Programming 2 5. Completing the program.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
Interfaces and Polymorphism CS 162 (Summer 2009).
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
Object-Oriented Programming: Polymorphism Chapter 10.
Lecture 12 Implementation Issues with Polymorphism.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
9NL Ayomi Hasenclever.  You cant touch a software  It is stored in a computer or laptop  Allows the hardware to do something useful, without the software.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Object-Oriented Design
CSC 221: Computer Programming I Fall 2005
Compsci 201 Priority Queues & Autocomplete
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Interfaces.
Workshop for Programming And Systems Management Teachers
Java Basics – Arrays Should be a very familiar idea
Presentation transcript:

CompSci Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …  Write general programs to understand the abstraction, advantages? void doShape(Shape s) { System.out.println(s.area()); System.out.println(s.perimeter()); s.expand(2.0); }  But a dog is also a quadruped, how can we deal with this?

CompSci Single inheritance in Java  A class can extend only one class in Java  All classes extend Object --- it's the root of the inheritance hierarchy tree  Can extend something else (which extends Object), why?  Why do we use inheritance in designing programs/systems?  Facilitate code-reuse (what does that mean?)  Ability to specialize and change behavior o If I could change how method foo() works, bar() is ok  Design methods to call ours, even before we implement o Hollywood principle: don't call us, …

CompSci Multiple Interfaces  Classes (and interfaces) can implement multiple interfaces  A dog is a mammal, a quadruped, a pet  How come canine is different?  What behavior do quadrupeds have? Pets have?  An interface specifies the name (and signature) of methods  No implementation, no state/fields  Yes for constants  In this course, by convention, we'll usually use interfaces  Emphasize design before implementation  Use abstract/default classes for code reuse, state

CompSci Alan Kay: Turing Award 2004 "Simple things should be simple. Complex things should be possible". "The best way to predict the future is to invent it" Kay, because of his experience with children, his love of education, his diverse interests, and his genius, recognized that users can and should interact with a computer in different ways and should not be limited to only text. He was among the first to represent objects in a computer as pictures -- a metaphor that he further extended by developing the concept of object orientation. He is, clearly, one of the fathers of the modern PC

CompSci MVC: Model, View, Controller  A model is the state and brains of a system  In a game it's all the pieces and where they are  In a spreadsheet it's the data and the formulae  The view is how we look at the model  Spread sheet has graphs, charts, cells, text, …  Game has board, number of opponents, hit-points, …  When the model changes, the views reflect the changes  The model tells the views how/if it has changed  Model sends information to views OR  View asks model for information

CompSci MVC: interfaces and inheritance  A model might have multiple views  Tell all the views "I've changed"  Who manages the views? This requires state: store views  Why can't we keep this state in an interface?  See IModel and AbstractModel  One specifies behavior, the other provides default  Don’t rewrite code if we don't have to, maintaining views will be the same for all models  See IView and SimpleView  No default/shared view state/behavior: text and GUI

CompSci Does SimpleViewer know Anagrams?  What does the SimpleViewer know about its model?  If we look at code, is there any application-specific logic?  What if we wanted to play a game, start a new game?  Control in MVC with SimpleViewer and IModel  Loading a file calls initialize()  Entering text calls process()  Model calls view with messages, errors, and complete update  This isn't completely general, but it's pretty generic  For this input, here's the output

CompSci Comparable and Comparator  Both are interfaces, there is no default implementation  Contrast with.equals(), default implementation?  Contrast with.toString(), default?  Where do we define a Comparator?  In its own.java file, nothing wrong with that  Private, used for implementation and not public behavior o Use a nested class, then decide on static or non-static o Non-static is part of an object, access inner fields  How do we use the Comparator?  Sort, Sets, Maps (in the future)