Week 14 - Monday CS 121.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

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.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
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.
Week 11: Class Design 1.  Most classes are meant to be used more than once  This means that you have to think about what will be helpful for future.
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.
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.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 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.
Computer Science I Inheritance Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSC 142 Computer Science II Zhen Jiang West Chester University
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!
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Advanced Programming in Java
Lecture 12 Inheritance.
Objects as a programming concept
Inheritance and Polymorphism
Lecture 9-2: Interacting with the Superclass (super);
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Class Inheritance (Cont.)
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Recitation 7 October 7, 2011.
Java Programming, Second Edition
Object Oriented Programming
Chapter 10: Method Overriding and method Overloading
CMSC 202 Generics.
Inheritance.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Building Java Programs
Method Overriding and method Overloading
Topics OOP Review Inheritance Review Abstract Classes
Classes and Methods 15-Aug-19.
Presentation transcript:

Week 14 - Monday CS 121

Last time What did we talk about last time? Image manipulation Lab 13

Questions?

Project 5

Concept of Inheritance

Inheritance The idea of inheritance is to take one class and generate a child class This child class has everything that the parent class has (members and methods) But, you can also add more functionality to the child The child can be considered to be a specialized version of the parent

Code reuse The key idea behind inheritance is safe code reuse You can use old code that was designed to, say, sort lists of Vehicles, and apply that code to lists of Cars All that you have to do is make sure that Car is a subclass (or child class) of Vehicle

Subclass relationship Java respects the subclass relationship If you have a Vehicle reference, you can store a Car object in that reference A subclass (in this case a Car) is a more specific version of the superclass (Vehicle) For this reason, you can use a Car anywhere you can use a Vehicle You cannot use a Vehicle anywhere you would use a Car

Subclass example As long as Car is a subclass of Vehicle, we can store a Car in a Vehicle reference Even in an array is fine Storing a Vehicle into a Car doesn’t work Vehicle v = new Car("Lancer Evolution"); // perfectly okay Vehicle[] vehicles = new Vehicle[100]; for( int i = 0; i < vehicles.length; i++ ) vehicles[i] = new RocketShip(); // cool Car c = new Vehicle(); // gives error

Inheritance Mechanics

Creating a subclass All this is well and good, but how do you actually create a subclass? Let’s start by writing the Vehicle class public class Vehicle { public void travel(String destination) { System.out.println("Traveling to " + destination); }

Extending a superclass We use the extends keyword to create a subclass from a superclass A Car can do everything that a Vehicle can, plus more public class Car extends Vehicle { private String model; public Car(String s) { model = s; } public String getModel() { return model; } public void startEngine() { System.out.println("Vrooooom!"); }

Power of inheritance There is a part of the Car class that knows all the Vehicle members and methods Car car = new Car("Camry"); System.out.println( car.getModel() ); //prints "Camry" car.startEngine(); //prints "Vrooooom!" car.travel( "New York City" ); //prints "Traveling to New York City"

A look at a Car Each Car object actually has a Vehicle object buried inside of it If code tries to call a method that isn’t found in the Car class, it will look deeper and see if it is in the Vehicle class The outermost method will always be called Car model getModel() startEngine() Vehicle travel()

Overriding Methods

Adding to existing classes is nice… Sometimes you want to do more than add You want to change a method to do something different You can write a method in a child class that has the same name as a method in a parent class The child version of the method will always get called This is called overriding a method

Mammal example We can define the Mammal class as follows: public class Mammal { public void makeNoise() { System.out.println("Grunt!"); }

Mammal subclasses From there, we can define the Dog, Cat, and Human subclasses, overriding the makeNoise() method appropriately public class Dog extends Mammal { public void makeNoise() { System.out.println("Woof"); } } public class Cat extends Mammal { public void makeNoise() { System.out.println("Meow"); } } public class Human extends Mammal { public void makeNoise() { System.out.println("Hello"); } }

Inheritance Examples

Hunters and prey simplified Everyone seemed to like the hunters and prey example Let’s use inheritance to make it easier to program Hunters and prey are very similar, except that hunters move toward prey and prey move away With inheritance we can reuse code We only need to change the behavior portion

Prey class The Prey class has: x Location: double y Location: double Aliveness: boolean Speed: double Color: StdDraw.Color Size: double We give the Prey class distance() methods and draw() methods

Hunter class The Hunter class now extends the Prey class, getting everything that it had before We call the Prey constructor with different size, color, and speed parameters Then we just need to change the update() method to search for Prey instead of running from Hunters

getClass() method To make the Hunter and Prey classes work, we now need to know if a specific object belongs to a given type Every object has a getClass() method We can test to see if an object’s getClass() method gives back Hunter.class or Prey.class By doing so, we can tell the class of a given object

Further expansion of hunters and prey What if we create a super hunter? Super hunters are larger and faster than hunters, and try to eat everything, including each other We make SuperHunter extend Hunter We call the Hunter constructor with the right speed, size, and color Then, we change the update() method to reflect the right behavior

Quiz

Upcoming

Next time… File I/O

Reminders Keep working on Project 5