COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
Object Oriented Programming with Java
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
1 Cathay Life Insurance Ltd. (Vietnam) 27/11/20091.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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,
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Chapter 71 Inheritance Chapter 7. 2 Reminders Project 4 was due last night Project 5 released: due Oct 10:30 pm Project 2 regrades due by midnight.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
COMP Inheritance Basics Yi Hong June 09, 2015.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance and Polymorphism
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
COMP 110 More about inheritance
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 9 Inheritance and Polymorphism
Inheritance Chapter 7 Chapter 7.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Inheritance, Polymorphism, and Interfaces. Oh My
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Lab 8 is due tomorrow by midnight

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Go over Program 4 More Inheritance Programming Demo

COMP 110: Spring Program 4

COMP 110: Spring Inheritance Section 8.3 in text

COMP 110: Spring Inheritance Define a general class Later, define specialized classes based on the general class These specialized classes inherit properties from the general class

COMP 110: Spring Inheritance Hierarchies Put these classes into an inheritance hierarchy AnimalReptileMammalHumanCrocodileWhale Animal ReptileMammal HumanCrocodileWhale

COMP 110: Spring The is-a relationship This inheritance relationship is known as an is-a relationship A Doctoral student is a Grad student A Grad student is a Student A Student is a Person Is a Person a Student? Not necessarily!

COMP 110: Spring When to Use Inheritance Use inheritance when an is-a relationship is present A Student is a Person, Student inherits from Person Do not confuse an is-a relationship with a has-a relationship A Student has a date of enrollment, so an object of class Date should be an instance variable of the Student class, and should not inherit from it

COMP 110: Spring The Keyword extends public class Derived_Class_Name extends Base_Class_Name { Declaration_of_Added_Instance_Variables Definitions_of_Added_And_Overridden_Methods } public class Student extends Person { // stuff goes here } A derived (child) class inherits the public instance variables and public methods of its base (parent) class

COMP 110: Spring Method Overriding public class Person { //... public void printInfo() { System.out.println("Name: " + name); } public class Student extends Person { //... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); } Overridden The printInfo method in the class Student overrides the printInfo method inherited from the class Person

COMP 110: Spring Overriding Methods Java handles this situation as follows: If a derived class defines a method with the same name, number and types of parameters, and return type as a method in the base class, the derived class method overrides the base class method The method definition in the derived class is the one that is used for objects of the derived class

COMP 110: Spring Overriding Methods Why is it useful? You often want derived classes to perform custom behavior public class Shape { public void draw(Graphics g) { } public class Circle extends Shape { public void draw(Graphics g) { g.drawOval(…arguments…); } public class Rectangle extends Shape { public void draw(Graphics g) { g.drawRect(…arguments…); }

COMP 110: Spring The Keyword super Last time we saw that the keyword super could be used to invoke the constructor of the base class public class Person { private String name; public Person() { name = "No name yet"; } public class Student extends Person { private int id; public Student() { super(); id = -1; } Student student = new Student(); //name is set to No name yet, and id is set to -1

COMP 110: Spring The Keyword super The use of super must always be the first action taken in the constructor Not required to be specified, but if it is, it must come first Java will automatically call the default constructor of the base class if super is not specified public Student() { super(); id = -1; } public Student() { id = -1; } public Student() { id = -1; super(); } OK ERROR Equivalent

COMP 110: Spring The Keyword super You cannot use repeated supers If specified, specify only once public Student() { super(); id = -1; } public Student() { super(); super("No name yet"); id = -1; } OKERROR

COMP 110: Spring The Keyword super What if the base class is a derived class? public class Person { private String name; public Person() { name = "No name yet"; } public class Student extends Person { private int id; public Student() { super(); id = -1; } public class Undergraduate extends Student { private int level; public Undergraduate() { super(); //calls constructor of Student, not Person level = -1; }

COMP 110: Spring Method Overriding public class Person { //... public void printInfo() { System.out.println("Name: " + name); } public class Student extends Person { //... public void printInfo() { System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); } Overridden Is there a way to call the printInfo() method of the class Person from within the class Student? Yes

COMP 110: Spring Calling Overridden Methods public class Person { //... public void printInfo() { System.out.println("Name: " + name); } public class Student extends Person { //... public void printInfo() { super.printInfo(); System.out.println("ID: " + getID()); } } The keyword super can also be used to call overridden methods of a base class

COMP 110: Spring Inheritance and Overloading public class Person { private String name; public void setData(String newName) { name = newName; } public class Student extends Person { private int id; public void setData(String newName, int newID) { setData(newName); id = newID; } Overloaded Methods with a different number or type of parameters are overloaded, not overridden

COMP 110: Spring Type Compatibilities Given this inheritance heirarchy… Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? Person p = new Person(); Yes! Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? HighJumper h = new HighJumper(); Yes! Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? Person p = new Athlete(); Yes! An Athlete is a Person, so this is okay Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? Person p = new SkyDiver(); Yes! A SkyDiver is a Person, so this is okay Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? Skydiver s = new Person(); No! A Person is not necessarily a Skydiver, so this is illegal Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Is This Code Legal? Athlete ath = new Athlete(); XGamesSkater xgs = ath; No! An Athlete is not necessarily an XGamesSkater, so this is illegal Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Dynamic Binding Person person = new Person("John Smith"); person.printlnfo(); //calls printInfo of Person class Student student = new Student("John Smith", ); student.printlnfo(); //calls printInfo of Student class Person p = new Student(Tom Jones", ); p.printInfo(); //which printInfo method is called? The printInfo method of the Student class is called because p is really a student!

COMP 110: Spring The Operator instanceof We can test whether an object is of a certain class type using the instanceof operator: public void objectType(Person p) { if(p instanceof Student) { System.out.println("p is an instance of the class Student"); } else if(p instanceof GradStudent) { System.out.println("p is an instance of the class GradStudent"); } … } Syntax: object instanceof Class_Name

COMP 110: Spring The Class Object Every class in Java is derived from the class Object Every class in Java is an Object Animal ReptileMammal HumanCrocodileWhale Object Person StudentEmployee

COMP 110: Spring The Class Object The Java class Object provides methods that are inherited by every class For example equals, toString These methods should be overridden with methods appropriate for the classes you create

COMP 110: Spring Printing Objects to Screen The method println is overloaded to accept objects as an argument System.out.println(int i) System.out.println(double d) System.out.println(Object o) When printing an object with System.out.println(Object o), the objects toString() method is called and the result is printed public void println(Object o) { System.out.println(o.toString()); //dynamic binding }

COMP 110: Spring Overriding toString public class Person { private String name; public Person() { name = "No name yet"; } public void setName(String newName) { name = newName; } //converts a Person object to a String, overrides toString in the class Object public String toString() { return "Name: " + name + "\n"; } public static void main(String[] args) { Person person = new Person(); person.setName("John Smith"); System.out.println(person); //prints "Name: John Smith" to screen (without quotes) }

COMP 110: Spring Overriding toString public class Student extends Person { private int id; public Student() { super(); id = -1; } public void setID(String newName) { name = newName; } //converts a Person object to a String, overrides toString in the class Person public String toString() { return "Name: " + name + "\nID: " + id + "\n"; } public static void main(String[] args) { Student student = new Student(); student.setName("John Smith"); student.setID( ); System.out.println(student); //prints "Name: John Smith" // "ID: " to screen (without quotes) }

COMP 110: Spring Programming Demo Programming with inheritance Sports Define the following classes Person Athlete HighJumper Skydiver ExtremeAthlete XGamesSkater

COMP 110: Spring Sports Each class has a method called jump Simply prints a message to screen Person System.out.println("Whee!"); Athlete System.out.println("I jump well!"); ExtremeAthlete System.out.println("EXTREMEEEEEEEEEE JUMP!"); HighJumper System.out.println("I jump REALLY HIGH!"); ShotPutter System.out.println("I don't jump very much."); XGamesSkater System.out.println("360 nollie to frontside air");

COMP 110: Spring Sports Write a class called Sports that demonstrates Type compatibility Dynamic binding instanceof operator

COMP 110: Spring Friday No recitation, enjoy the Holidays