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!

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
C HAPTER 7 Better Living in Objectville. O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods.
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.
Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance, Static and Dynamic Typing. Announcements  Project 0 due Fri 2/13 at 11:59pm 24 slip hours (unused hours roll over)  HW3 released tonight,
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Inheritance, Abstract & Interface Pepper With help from and
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
CSC 142 Computer Science II Zhen Jiang West Chester University
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Coming up: Inheritance
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Inheritance ndex.html ndex.htmland “Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 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.
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Coming up A quick word about abstract How a class interfaces
Advanced Java Topics Chapter 9
Week 14 - Monday CS 121.
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance.
Week 8 Lecture -3 Inheritance and Polymorphism
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
Chapter 10 Thinking in Objects
MSIS 670 Object-Oriented Software Engineering
Inheritance, Polymorphism, and Interfaces. Oh My
Inherited Classes in Java
Advanced Programming Behnam Hatami Fall 2017.
Inheritance, Superclasses, Subclasses
Interfaces and Abstract Classes
Chapter 9 Carrano Chapter 10 Small Java
Workshop for Programming And Systems Management Teachers
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

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!

Here is my class for cat import java.io.*;//importing java I/O package public class Cat1 { File imageFile; //The filename storing the image of the cat String food; //"meat" int hunger;//hunger level of the cat. It changes depending on when and how much the cat //eats int locationX;//X coordinate of the cat in space int locationY; //Y coordinate of the cat in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }

Here is my class for sheep import java.io.*;//importing java I/O package public class Sheep1 { File imageFile; //The filename storing the image of the sheep String food; //“grass" int hunger;//hunger level of the sheep. It changes depending on when and how much the //sheep eats int locationX;//X coordinate of the sheep in space int locationY; //Y coordinate of the sheep in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }

Do you notice anything?

Observations Sheep1 class and Cat1 class are very much alike. They have same instance variables They have same methods Can we save some typing and maintenance efforts by sharing? My supervisor asks me to write more classes for lion, hippo, tiger, cow, wolf, dog,… Where is my vacation???

Inheritance Put common codes in a super class. Subclass inherits from super class, and REUSE codes from super class. An inheritance relationship means subclass inherits the members (both instance variables and methods) of super class. Subclass can add new methods and variables of its own. Subclass can override the methods it inherits from super class. Instance variables are not overriden.

IS-A Relationship Inheritance is not just for reusing code. A subclass should be a specific kind of super class. E.g. A cat IS A specific kind of Animal.

What am I going to do? I need a vacation. I need to finish my work before going to vacation. I want to redesign my objects. I have identified IS-A relationship. Cat is a specific kind of Animal. So do Sheep, … I will use inheritance that I just learned.

Add a super class, Animal It contains all the common instance variables It contains all the common methods with a default implementation.

Here is my super class, Animal import java.io.*;//importing java I/O package public class Animal { File imageFile; //The filename storing the image of the animal String food; //"meat" or "grass" int hunger;//hunger level of the animal. It changes depending on //when and how much the animal eats int locationX;//X coordinate of the animal in space int locationY; //Y coordinate of the animal in space public void makeNoise(){} public void eat(){} public void sleep(){} public void roam(){} }

Simplify my Cat1 class Rename my cat class from Cat1 to Cat Cat class inherits all instance variables from Animal class, so I will not write it. Cat class inherits all methods from Animal class, so I overwrite methods that should be implemented differently. Cat makes noise differently, so I overwrite makeNoise() method. Cat eats meat, not grass, so I need to overwrite eat() method to specify this.

What is the syntax for inheritance? public class Cat extends Animal { }

Here is my Simplified Cat class public class Cat extends Animal { public void makeNoise() { //Cat's meow } public void eat() { //Eat meat }

Sheep, dog, tiger, ….??? I will do the same thing! I am going to vacation in one week!

Here is my simplified Sheep class public class Sheep extends Animal { public void makeNoise() { //Sheep's meeeeier ? } public void eat() { //Eat grass }

Which method is called actually? Both Animal (super class) and Cat (subclass) have makeNoise() method. After I instantiate a Cat object, I call makeNoise() method. Which method is actually called? Super class? Or subclass? Let’s test it out!

Which method is called, actually? The method of the most specific class/object. If a method is not overriden in subclass, the super class method is called. If a method is overriden in subclass, the subclass’s method is called.

Assignments Read “Java 2”, Chapter 7 Read “Head First Java”, Chapter 7