Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 14.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Classes and SubClasses Super Parent Sub Child IS - A.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance and Polymorphism
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
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.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Sections Inheritance and Abstract Classes
Overriding Method.
Inheritance and Polymorphism
One class is an extension of another.
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
One class is an extension of another.
Abstract Classes.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
Abstract Classes Page
Inheritance Inheritance is a fundamental Object Oriented concept
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Presentation transcript:

Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14

Note Set 14 Overview Inheritance & Polymorphism

Scenario You’re expanding your math practice program. It will: Create problems of various types Create problems of various difficulties Model the problems themselves as objects What do they have in common? How do each differ?

Inheritance Hierarchy Problem DivisionProblem MultiplicationProblem SubtractionProblem AdditionProblem Can treat a subclass object as if it were a superclass object Problem p = new AdditionProblem();

Polymorphism Problem … … AdditionProblem … … … … - Problem implements the getAnswer() method that returns the answer/solution -Because there is no operation associated, always returns 0. Each subclass overrides the getAnswer() - redefines or re-implements that method

Polymorphism Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); We can treat a subclass object as a superclass object.

Polymorphism Problem … … AdditionProblem … … … … AdditionProblem p = new Problem(); Can’t do this – Problem is NOT an AdditionProblem

Polymorphism Problem … … AdditionProblem … … … … Problem p = new Problem(); //call the getAnswer method in problem p.getAnswer(); //always returns 0

Polymorphism Problem … … AdditionProblem … … … … AdditionProblem ap = new AdditionProblem(); //Calls getAnswer method in AdditionProblem ap.getAnswer();//returns the correct answer

Polymorphism – The POWER!!! Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); p.getAnswer();//returns the correct answer // for an additionProblem - p is a Problem variable – but it references an AdditionProblem object - We can only do this because AdditionProblem extends Problem - Call getAnswer on p - At runtime – the correct version of getAnswer() will be called – the one that goes with AdditionProblem object

Polymorphism – The POWER!!! Problem … … AdditionProblem … … … … Problem p = new AdditionProblem(); p.getAnswer();//returns the correct answer // for an additionProblem Why is this so powerful? Easily Extensible – Can add other problems types without affecting current implementation.

Abstract Classes and Methods Sometimes it doesn’t make sense to instantiate objects of a particular type in an inheritance hierarchy. Consider class Problem Does it make sense to ever instantiate an object of type Problem? Why not? Class Problem could be made an abstract super class by making getAnswer() and getOperator() abstract methods

Abstract Superclass abstract classes cannot be instantiated used only as superclasses in inheritance hierarchies Purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design Class is declared abstract by using keyword abstract Abstract classes usually have 1 or more abstract methods No implementation is provided – left to subclasses to implement

Problem public abstract class Problem { private int op1; private int op2; //Constructors //Accessors and Mutators public abstract int getAnswer(); public abstract String getOperator(); public String toString() { return Integer.toString(getOp1()) + " " + this.getOperator() + " " + Integer.toString(getOp2()) + " = “ + Integer.toString(getAnswer()); } Notice: No implementation provided – will be specific to subclass Problem p = new Problem(); //COMPILE ERROR //Can’t instantiate abstract class

Abstract classes – some details A class must be declared abstract if it contains at least one abstract method An abstract method from a superclass must be overridden in a subclass unless the subclass is also declared abstract Abstract classes can contain concrete methods and instance variables. These are inherited as in normal inheritance. Is this legal? Problem [] p = new Problem [4];

Example 2 A company pays its employees on a weekly basis. There are four types of employees: 1.Salaried employees are paid a fixed weekly salary 2.Hourly employees are paid by the hour and receive 1.5 hourly rate for over 40 hours of work 3.Commission employees are paid a percentage of their sales Salaried and commission employees are eligible for a bonus based on the total profit generated for the company.