308-203A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
1 Inheritance and Polymorphism. 2 This section is not required material!!!!  A note about inheritance… It’s not normally covered in 101 It will be gone.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CSSE221: Software Dev. Honors Day 6 Announcements Announcements Questions? Questions? Hint on BallWorlds’ changing the x and y coords in 2 nd half of lecture.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Inheritance and Polymorphism Chapter 11 Spring 2007 CS 101 Aaron Bloomfield.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Programming in Java: lecture 7
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Objects as a programming concept
Inheritance and Polymorphism
Object-Oriented Programming
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance and Polymorphism
Chapter 10 Thinking in Objects
Advanced Java Programming
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Testing with OO OO has several key concepts:
Java Programming, Second Edition
Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
CIS 110: Introduction to computer programming
Computer Science II for Majors
Presentation transcript:

A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000

Interfaces More than one class may perform the same service e.g. Planes, trains and automobiles Interfaces describe what classes can do

Example interface Vehicle { float costOfTrip( );... }

Example interface Vehicle { float costOfTrip( );... } class Car implements Vehicle { float costOfTrip( ) { … } } class Train implements Vehicle { float costOfTrip( ) { … } }

Example Vehicle chooseLeastExpensive(Vehicle[] choices) { … for (int j = 0; j < choices.length; j++) if (choices[j].costOfTrip( ) < best) { bestVehicle = choices[j]; best = choices[j].costOfTrip( ); } … }

What’s the point We can manipulate objects without knowing their exact class Code we write will work, even with new kinds of objects We’ve abstracted a kind of behavior

Polymorphism Definition: Polymorphism is the ability of a method call to invoke an underlying method apropriate to the type of the object.

Any questions?

Interface shortcomings What if two classes share not only behavior but data? E.g. all vehicles have wheels What if two classes have methods which are implemented in the same way? What if we want to change the behavior of a class slightly?

Inheritance Make new classes based on a previous class Behavior remains the same unless overriden Code can be modified incrementally, without restarting from scratch for each class Code can be shared

Terminology Superclass - a class on which another class is based Subclass - a class derived from another class Override - the replacement of a method inherited from a superclass by some new method

Related Java Keywords extends - what class do we build on this - lets an object refer to itself super - lets an object refer to methods as defined in its superclass final - prevents a method from being overriden abstract - indicates that a method is undefined and must be overriden in the subclass

Public/private/protected Public - visible by any other class Private - visible only by this class (not subclasses!) Protected - visible to this class and subclasses Keywords to restrict access to methods/data

A Simple Example public class Workaholic extends Worker { public doWork( ) { super.doWork( ); drinkCoffee( ); super.doWork( ); }

Any questions?