CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Chapter 10 Classes Continued
Abstract Classes and Interfaces
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
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.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
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.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Programming II Polymorphism A.AlOsaimi.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
Presentation transcript:

CS 2430 Day 28

Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday The next two labs will be useful (if not necessary) for Prog6

Agenda When is inheritance good? Abstract classes

When is your inheritance good?

Liskov substitution principle (LSP) The essence can be paraphrased as: A child can be used wherever a parent is expected For every overridden method in a child class: require no more, promise no less 1.Pre-conditions (things true before method invoked) cannot be strengthened 2.Post-conditions (things true after method returns) cannot be weakened.

“Good” inheritance Use the notion of “is-a” and LSP to determine if your inheritance is “good” Don’t overdo inheritance!!

Some consequences The class Circle can’t inherit from Oval (even though it “is-a”) Why not? Would have to restrict overridden “resize” method (strengthen pre-conditions: focus points equal) Or we don’t have restricted “resize”, but then we would have to allow for non-perfect circles (weaken post-conditions)

More consequences The BagOfApples is NOT a BagOfFruit Why not? Either restrict overridden “add” (strengthen pre-conditions) to disallow adding other types of fruit OR, we would have to allow Banana s in the BagOfApples (weaken post-conditions)

The solution The classes Circle and Oval should each extend Shape Good “is-a” relationship; can substitute Circle s and Oval s in where Shape s are expected The Shape class will have methods like “draw”, “rotate” and “translate”, which do not have to be restricted in Circle or Oval

Speaking of Shape s…

Shape hierarchy public class Shape {... public void draw(Graphics g) {... } } public class Line extends Shape public void draw(Graphics g) {... } } public class Oval extends Shape public void draw(Graphics g) {... } } public class Square extends Shape public void draw(Graphics g) {... } }...

Review polymorphic code public void paintComponent(Graphics g) { Shape s; if (selection == LINE) // selection is a class variable s = new Line(); else if (selection == OVAL) s = new Oval(); else if (selection == SQUARE) s = new Square();... s.draw(g); // g is a Graphics object } Which shape gets drawn? The correct one, which is determined at run-time!

What if? public void paintComponent(Graphics g) { Shape s = new Shape();... s.draw(g); } What should happen if this code is executed? What does a shape look like? It doesn’t really make sense to let “abstract” Shape s draw themselves. How to prevent this???

Shape is the perfect candidate for a class that should never be instantiated

Classes that can’t be instantiated: abstract

abstract methods An abstract method has no body, not even an empty body Example: public abstract void draw(Graphics g); Constructors cannot be abstract

abstract class A class with one or more abstract methods An abstract class CANNOT be instantiated (would generate a syntax error)

abstract and inheritance Must derive from abstract classes (can’t use them directly) –If subclass doesn’t provide bodies for all abstract methods in superclass, then the subclass is also abstract An abstract method in a superclass is a way to “force” the child to provide its own implementation

public abstract class Shape { protected int x, y; // bad names! public Shape(int inX, int inY) {... } public abstract void draw(Graphics g); // no way to draw it!... } public class Circle extends Shape { protected int radius; public Circle(int inX, int inY, int inRadius) { super(inX, inY); radius = inRadius; public void draw(Graphics g) {... }... }

Good? public void paintComponent(Graphics g) { Shape s; int x, y;... s = new Shape(x, y); // NO! Shape is abstract s.draw(g); // Doesn't make sense } NOT good!

Good? public void paintComponent(Graphics g) { Shape s; int x, y, radius;... s = new Circle(x, y, radius); s.draw(g); } Good!

Any questions?