Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.

Slides:



Advertisements
Similar presentations
GridWorld Case Study Part 2 Bug Variations A Summary by Jim Mims.
Advertisements

1. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
An alternative view of class inheritance and interfaces (Chapter 8) The topics in Chapter 8 of Liang’s book are difficult and important. Here I’m going.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
There has been a fire! 3 fire engines are called to a fire. Every time 3 fire engines are called to a fire, 4 police cars follow. Whenever 4 police cars.
LECTURE 07 Programming using C# Inheritance
Strategy 1: Act It Out Using yourself or objects (scraps of paper, counters etc.) that can be moved around to solve problems. Helps to develop visual images.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
March 31, 2000CS102-01Lecture 1.3 Introduction to Object-Oriented Programming CS Lecture 1-3.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Power Point Sight Words
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 1 - Getting to know Greenfoot
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Advanced Stuff Learning by example: Responding to the mouse.
High Frequency Words August 31 - September 4 around be five help next
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Sight Words.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
The GridWorld Case Study Program Section 1 - Overview of the Class Hierarchies Section 2 - The Actor Class Section 3 - The Rock and Flower Classes Section.
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
Interfaces and Polymorphism CS 162 (Summer 2009).
Creating a GUI Class An example of class design using inheritance and interfaces.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Slide 0. Inheritance and Polymorphism Mr. Landa South East High.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
JAVA CLASSES, METHODS AND VARIABLES.  Defined Noun: A set or category of things having some property or attribute in common and differentiated from others.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6: Composition and Inheritance CS202 Fall 2013.
Lecture 5:Interfaces and Abstract Classes
Object-Orientated Programming
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Have you ever conducted an experiment before
Learning to program with Logo
Chapter 10 Thinking in Objects
CS2102: Lecture on Abstract Classes and Inheritance
The Little Crab Scenario
Inheritance and Polymorphism
Inheritance and Polymorphism
Becky’s Bright Idea Becky’s Bright Idea
Java Inheritance.
How Tall Are You? Introducing Functions
Chapter 9 Carrano Chapter 10 Small Java
Workshop for Programming And Systems Management Teachers
Chapter 14 Abstract Classes and Interfaces
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Presentation transcript:

Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High

What does Inheritance mean in English?

What about Polymorphism? Umm…

Before the Java definitions… Take out a piece of paper and draw an automobile.

My next automobile. (Anybody draw something like this?):

My next automobile. (According to my wife):

They look a little different Would you agree they both have… –4 tires –An engine –Windows –Doors –Seats –Can start their engines –Refuel by putting gas in the tank Therefore they must both be part of the same class: Automobile

Therefore… Since they are both the same class, they must be able to do the following the same way too –Accelerate –Make turns –Weave through traffic –Attract attention (both of the police and the opposite sex) Do you agree?

Not quite… There are a lot of similarities, both are indeed automobiles But one is a sports car and one is a minivan Are those different classes?

More like different subtypes Think about how sports cars were invented –Did they start all over from scratch? –Someone took the automobile and made a more specific type

How does this work in java? We take the class: public class Automobile –Members (= fields = instance variables): public String engine public int doors public int seats –Methods (= to “do” things): startEngine() refuel() accelerate()

And extend its definition We will add and replace what we need –public class SportsCar extends Automobile makeTurns() weaveThroughTraffic() attractAttention() accelerate() –Notice we’re replacing accelerate() that is defined in Automobile. That’s because the SportsCar needs to expel a very loud purr from the engine as it accelerates. We will keep (= inherit) everything else: –startEngine() –refuel() We get this for free and don’t have to write the code again

Inheritance in terms of Java Put it in your own words!

So, Polymorphism? 1 Command, implemented differently by different classes (here: different cars). Example pumpGas( ) method: –Sports car (at back of car). –Minivan (on right side of car). –Luxury car (on left side of car). –Pickup truck (behind rear license plate).

An auto is an auto In other words, whatever kind of Automobile you have, you can pump gas into it. Java will ask the Automobile (regardless of what kind of car it is) to pumpGas() and all cars fill gas in their own way e.g. side of the car, rear of the car, etc.

Another polymorphic example. Consider the superclass: public class Animals. Animals has an eat() method. There are several classes that inherit from Animals: –public class Bear extends Animals –public class Chicken extends Animals –public class Tarantula extends Animals –public class Cockroach extends Animals

eat() method: You can call the eat() method in Animals. However, every animal eats differently => each animal defines the eat() method in Animals differently. How do they eat(): –Bear? –Chicken? –Tarantula? –Cockroach? Did you know a cockroach can live for about a week without its head until it finally dies of hunger? Just info: nothing to do with Java!!!!

Inheritance in GridWorld class Bug extends Actor –Inherits putSelfInGrid() –Replaces act() –Adds canMove() class BoxBug extends Bug –Inherits canMove() –Replaces act()

Polymorphism in action For each Actor (whether Actor, Bug, etc) in the grid, act() is called in each step: for (Actor a : actors) { // only act if another actor hasn't removed a if (a.getGrid() == gr) a.act(); }