Inheritance and Polymorphism

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.
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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Inheritance and Polymorphism Dr. M.M. van Baal Verdugo Hills High.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Mr Barton’s Maths Notes
Introduction While it may not be efficient to write out the justification for each step when solving equations, it is important to remember that the properties.
LECTURE 07 Programming using C# Inheritance
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
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.
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!
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
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.
Advanced Stuff Learning by example: Responding to the mouse.
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.
Slide 0. Inheritance and Polymorphism Mr. Landa South East High.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
JAVA CLASSES, METHODS AND VARIABLES.  Defined Noun: A set or category of things having some property or attribute in common and differentiated from others.
How Tall Are You? Introducing Functions for Alice 3 By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
GridWorld.
Lecture 5:Interfaces and Abstract Classes
Mr Barton’s Maths Notes
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Object-Orientated Programming
Reinforcement & Punishment
Abstract Classes and Interfaces
Mr F’s Maths Notes Number 7. Percentages.
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Have you ever conducted an experiment before
Learning to program with Logo
The Little Crab Scenario
Pakitang-turo Dr. Lakangiting Garcia.
Stacks.
Have you ever conducted an experiment before
Inheritance and Polymorphism
Becky’s Bright Idea Becky’s Bright Idea
Questions I need to ask myself when doing a lab write-up
Game Loop Update & Draw.
Java Inheritance.
What do you think the word identical means?
Stacks.
Mr Barton’s Maths Notes
How Tall Are You? Introducing Functions
Debugging “Why you were up till 2AM”
Workshop for Programming And Systems Management Teachers
Chapter 14 Abstract Classes and Interfaces
3 March 2016 Note Cards Today you will learn what these note cards are all about & how they will help keep you from plagiarizing. You will create some.
What happens when you joke around with a truck driver
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
creating a ecosystems model in net logo
Take A Chance Resources: 6 cards with awards on the back, ranging from jump the dinner queue, bag of sweets, pen, to a detention. Make them different.
Presentation transcript:

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

What does Inheritance mean in English? Ask students to give their answers.

What about Polymorphism? Umm… Break down into poly and morph.

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 girlfriend):

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 Student’s drawings probably also have the tires, doors, etc.

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 Automobile shows up, I can pump gas into it. Java will ask the Automobile (regardless of what type it is) to pumpGas() and it will do the right thing.

Another polymorphic example. Consider the class: 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 pustSelfInGrid() Replaces act() Adds canMove() class BoxBug extends Bug Inherits canMove()

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(); }