Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007.

Slides:



Advertisements
Similar presentations
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Advertisements

Starting Out with Java: From Control Structures through Objects
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and 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.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance, Polymorphism, and Virtual Functions
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Chapter 11.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 11: Inheritance Starting Out with Java: From Control Structures through Data Structures Second.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
Inheritance and Polymorphism
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Lecture 12 Inheritance.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Inheritance, Polymorphism, and Virtual Functions
by Tony Gaddis and Godfrey Muganda
ATS Application Programming: Java Programming
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
CSC 143 Inheritance.
Class Inheritance (Cont.)
Inheritance, Polymorphism, and Virtual Functions
Advanced Programming Behnam Hatami Fall 2017.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
C++ Inheritance.
Fundaments of Game Design
Object-Oriented Programming
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007

Learning Outcomes Write programs that are easily extensible and modifiable by applying polymorphism in program design Define reusable classes based on inheritance and abstract classes and abstract methods Differentiate the abstract classes and Java interface Define methods, using the protected modifier

What is Inheritance? Generalization vs. Specialization Real-life things are typically specialized versions of other more general objects. The term “insect” describes a very general type of creature with numerous characteristics. grasshoppers and bumblebees are insects they share the general characteristics of an insect. However, they have special characteristics of their own. grasshoppers have a jumping ability, and bumblebees have a stinger. Grasshoppers and bumblebees are specialized versions of an insect.

Inheritance Insect GrasshopperBumbleBee Contains those attributes and methods that are shared by all insects. Contains those attributes and methods that are specific to a Bumble Bee. Contains those attributes and methods that are specific to a Grasshopper.

Inheritance and Polymorphism 5 Example class Pet { private String name; public String getName() { return name; } public void setName(String petName) { name = petName; } public String speak() { return “I’m your cuddly pet.”; } Pet myPet = new Pet(); System.out.println(myPet.speak());

Inheritance and Polymorphism 6 class Cat extends Pet{ public String speak() { return “Don’t give me orders.\n” + “I speak only when I want to.”; } Cat myCat = new Cat(); myCat.setName(“Cha Cha”); System.out.println(Hi, my name is “ + myCat.getName()); Cat myCat = new Cat(); myCat.setName(“Puff Puff”); System.out.println(myCat.getName( ) + ” says: “); System.out.println(myCat.speak( ) );

Inheritance and Polymorphism 7 class Dog extends Pet{ public String fetch() { return “Yes, master. Fetch I will.”; }

Inheritance and Polymorphism 8 class Pet { private String name; public String getName() { return name; } public void setName(String petName) { name = petName; } public String speak() { return “I’m your cuddly pet.”; } class Cat extends Pet{ public String speak() { return “Don’t give me orders.\n” + “I speak only when I want to.”; } class Dog extends Pet{ public String fetch() { return “Yes, master. Fetch I will.”; } Pet p; p = new Dog( ); System.out.println( p.speak() ); p = new Cat( ); System.out.println( p.speak() ); Pet p; p = new Dog( ); // invalid System.out.println( p.fetch() ); p = new Dog( ); System.out.println( (Dog)p.fetch() );

Inheritance and Polymorphism 9 Which is valid? 1) Pet p = new Cat( ); 2) Cat c = new Pet();

Inheritance and Polymorphism 10 Is the following code valid? Pet p = new Dog(); System.out.println( p.fetch() );

The “is a” Relationship The relationship between a base class and an inherited class is called an “is a” relationship. A Grasshopper “is a” insect. A poodle “is a” dog. A car “is a” vehicle. A specialized object has: all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an “is a” relationship among classes.

The “is a” Relationship We can extend the capabilities of a class. Inheritance involves a base class and a derived class. The base class is the general class and the derived class is the specialized class. The derived class is based on, or derived from, the base class. Base classes are more commonly called super classes, and derived classes are more commonly called sub classes. The relationship of classes can be thought of as parent classes and child classes.

Inheritance The derived class inherits fields and methods from the base class without any of them being rewritten. New fields and methods may be added to the derived class. The Java keyword, extends, is used on the class header to define the base class. public class FinalExam extends GradedActivity{…}

The GradedActivity Example Example: GradedActivity.java, GradedActivity.java GradeDemo.java FinalExam.java, FinalExam.java FinalExamDemo.java GradedActivity - score : double + setScore(s : double) : void + getScore() : double + getGrade() : char FinalExam - numQuestions : int - pointsEach : double - numMissed : int + FinalExam(questions : int, missed : int) : + getPointsEach() : double + getNumMissed() : int Contains those attributes and methods that are shared by all graded activities. Contains those attributes and methods that are specific to the FinalExam class. Inherits all non-private attributes and methods from the GradedActivity class.

Inheritance, Fields and Methods Members of the super class that are marked private: are not inherited by the sub class, and may only be accessed from the sub class by public methods of the super class. Members of the super class that are marked public: are inherited by the sub class, and may be directly accessed from the sub class.

Inheritance, Fields and Methods When an instance of the sub class is created, the non-private methods of the super class are available through the sub class object. FinalExam exam = new FinalExam(); exam.setScore(85.0); System.out.println(“Score = “ + exam.getScore()); Non-private methods and fields of the super class are available in the sub class. setScore(newScore);

Inheritance and Constructors Constructors are not inherited. When a derived class is instantiated, the base class default constructor is executed first. Example: SuperClass1.java, SuperClass1.java SubClass1.java ConstructorDemo1.java B Smith: Start here wed with sec01. Rate: 3 so far. 50 min. Diversion: classpath, code reuse, library creation B Smith: Start here wed with sec01. Rate: 3 so far. 50 min. Diversion: classpath, code reuse, library creation

BlueJ Example what happens when we change from “abstract” to “public”?