COMP 110 More about inheritance

Slides:



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

COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
1 Inheritance Classes and Subclasses Or Extending a Class.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
CPSC150 Abstract Classes and Interfaces Chapter 10.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Inheritance and Polymorphism CS351 – Programming Paradigms.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. 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.
EE2E1. JAVA Programming Lecture 3 Inheritance. Contents Base classes and derived classes Base classes and derived classes Protected scope Protected scope.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
COMP Inheritance Basics Yi Hong June 09, 2015.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Web Design & Development Lecture 9
Polymorphism.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
COMP 110 More arrays, 2D arrays, Program 4
Abstraction, Interface, Inheritance, Polymorphism, Override / Overload
Overriding Method.
Chapter 7- Inheritance.
Advanced Programming in Java
ATS Application Programming: Java Programming
CS Week 14 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CSC 113 Tutorial QUIZ I.
COMP 110 Information hiding and encapsulation
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
Polymorphism and access control
CS1316: Representing Structure and Behavior
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Announcements & Review
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Michele Weigle - COMP 14 - Spr 04
Polymorphism.
An Example of Inheritance
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Chapter 11 Inheritance and Polymorphism Part 1
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

COMP 110 More about inheritance Michele Weigle - COMP 14 - Spr 04 COMP 110 More about inheritance Catie Welsh April 13, 2011

Michele Weigle - COMP 14 - Spr 04 Announcements Program 4 early code check due Friday, April 15th in class Bring a print out of your code with you – whatever you have so far Also include a copy of any pseudocode you’ve written out To earn the 25 points, I’d like to see a significant amount of code written that does 1 of the following Game board code written – numbers randomly assigned, 2 of each # Mouse clicking code written so that your shapes are centered in the correct square of the grid You can NOT use late days for this! I must have a copy of your code by 1pm on Friday!

Michele Weigle - COMP 14 - Spr 04 Questions? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Lab 8 Some review More inheritance

Silly exercise time What’s the point? Well…

The difference between return and printing Returning a value from a method does NOT print it out to the screen The returned value is given to the caller of the method It is up to the method caller to do something with this value

The difference between return and printing public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() return rnd.nextInt(20) + 1; } public class Tester public static void main(String[] args) NumberUtils.roll20SidedDie(); // what does this do? Generates a random die roll, but it ignores the value returned from the roll20SidedDie method Have we printed anything to the screen?

The difference between return and printing public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() return rnd.nextInt(20) + 1; } public class Tester public static void main(String[] args) int num = NumberUtils.roll20SidedDie(); // what does this do? Puts the random number returned by roll20SidedDie into the variable num Have we printed anything to the screen?

The difference between return and printing public class NumberUtils { private static Random rnd = new Random(); public static int roll20SidedDie() return rnd.nextInt(20) + 1; } public class Tester public static void main(String[] args) int num = NumberUtils.roll20SidedDie(); // what does this do? System.out.println(num); Puts the random number returned by roll20SidedDie into the variable num Then prints the value of num to the screen

Review: Inheritance What is a base class? What is a derived class?

Review: Inheritance We have discussed before how classes of objects can have relationships Person Transportation Student Employee Car Airplane Animal Undergrad Grad Faculty Staff Horse Elephant Camel Masters Doctoral Nondegree

Put these classes into an inheritance heirarchy Crocodile Human Reptile Animal Mammal Whale Animal Reptile Mammal Crocodile Human Whale

Review: 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!

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

Review: How does this work in Java? public class Person { private String name; public Person() name = “No name yet”; } public void setName(String newName) name = newName; public String getName() return name; Person - name + setName(String newName) : void + getName() : String

Review: How does this work in Java? public class Student extends Person { private int id; public Student() super(); id = 0; } public Student(String stdName, int idNumber) setName(stdName); setID(idNumber); public void setID(int idNumber) id = idNumber; public int getID() return id; Person - name + setName(String newName) : void + getName() : String Student - id + setID(int idNumber) : void + getID() : int

Overriding methods What if the class Person had a method called printInfo? public class Person { // a bunch of other stuff // ... public void printInfo() System.out.println(name); }

Overriding methods What if the class Student also had a method called printInfo? public class Student extends Person { // a bunch of other stuff // ... public void printInfo() System.out.println("Name: " + getName()); System.out.println("ID: " + getID()); }

Overriding methods: example Both Person and Student have a printInfo() method Student std = new Student("John Smith", 37183); std.printInfo(); // calls Student’s printInfo method, // not Person’s Output would be: Name: John Smith ID: 37183

Overriding methods: why is it useful? You often want derived classes to perform custom behavior For example, drawing shapes 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…); }

Type compatibilities Given this inheritance heirarchy… Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

Is this code legal? Yes! Person p = new Person(); Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

Is this code legal? Yes! HighJumper h = new HighJumper(); Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

Is this code legal? Yes! An Athlete is a Person, so this is okay Person p = new Athlete(); Yes! An Athlete is a Person, so this is okay Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

Is this code legal? Skydiver s = new Person(); No! A Person is not necessarily a Skydiver, so this is illegal Person Athlete HighJumper ExtremeAthlete Skydiver XGamesSkater

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 ExtremeAthlete Skydiver XGamesSkater

What’s going on? We are creeping up to the idea of polymorphism Enables the substitution of one object for another as long as the objects have the same interface More details later

The class Object Every class in Java is derived from the class Object Every class in Java is an Object Object Animal Person Reptile Mammal Student Employee Crocodile Human Whale

Friday Program 4 help – In-class code check