Darrell Bethea June 6, 2011

Similar documents
CMSC 132: Object-Oriented Programming II. Inheritance

ECE 122. Engineering Problem Solving with Java

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Object Oriented Programming COP3330 / CGS5409

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Homework 2 Implementing the University Application Due: November 7, 2004 (Sunday midnight)

Inheritance CSC9Y4. Inheritance

Abstract class & Interface

Inheritance. Chapter 7. Chapter 7 1

Lecture 18 CSE11 Fall 2013 Inheritance

Overriding Variables: Shadowing

Introduction to Computation and Problem Solving

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Do not turn over this examination paper until instructed to do so. Answer all questions.

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

Inheritance and Polymorphism

8. Polymorphism and Inheritance

OVERRIDING. 7/11/2015 Budditha Hettige 82

Chapter 7. Inheritance

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

1.00 Lecture 14. Exercise: Plants

Do not turn over this examination paper until instructed to do so. Answer all questions.

Week 11: Class Design

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

5.6.1 The Special Variable this

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CSC 1214: Object-Oriented Programming

Object Oriented Relationships

Inheritance. Transitivity

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Week 5-1: ADT Design

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance -- Introduction

ENCAPSULATION AND POLYMORPHISM

CIS 110: Introduction to computer programming

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Practice for Chapter 11

Java Classes, Inheritance, and Interfaces

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

CS Programming I: Inheritance

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CS 113 PRACTICE FINAL

CS 251 Intermediate Programming Inheritance

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Programming Exercise 14: Inheritance and Polymorphism

An example from Zoology

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Comp 249 Programming Methodology

Implements vs. Extends When Defining a Class

CS-202 Introduction to Object Oriented Programming

Object Orientated Programming Details COMP360

9/10/2018 Programming Data Structures Inheritance

Programming in C# Inheritance and Polymorphism

Lecture 11. Demonstration #1. More Inheritance, More Constructors. Absolute C++ Chapter 15. Virtual Functions. Overriding

Inheritance, and Polymorphism.

Lecture 15: Inheritance II

More On inheritance. What you can do in subclass regarding methods:

Lecture 4: Extending Classes. Concept

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA

What is Inheritance?

Exam Duration: 2hrs and 30min Software Design

Final Examination Semester 3 / Year 2012

CSE 113 A. Announcements - Lab

INHERITANCE. Spring 2019

Programming II (CS300)

Object-Oriented Programming in Java

CSCE 145 Exam 2 Review No Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Classes and Inheritance Extending Classes, Chapter 5.2

Object-Oriented Concepts

Programming II (CS300)

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Deitel chapter 9)

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

EKT472: Object Oriented Programming. Overloading and Overriding Method

CS1150 Principles of Computer Science Objects and Classes

CSCE 145 Midterm 2 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

CS100J Prelim I, 29 Sept. 2003

CSCI 104 Inheritance. Mark Redekopp David Kempe

Computer Science 225 Advanced Programming Siena College Spring Topic Notes: Inheritance

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CSC1322 Object-Oriented Programming Concepts

Arrays Classes & Methods, Inheritance

Logistics. Final Exam on Friday at 3pm in CHEM 102

Transcription:

Darrell Bethea June 6, 2011

Program 4 due Friday Testing/debugging help online Final exam Comprehensive Monday, 6/13, 8-11 AM SN014 2

3

Inheritance 4

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 5

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

What are some properties of a Person? Name, height, weight, age How about a Student? ID, major Does a Student have a name, height, weight, and age? Yes, Student inherits these properties from Person 7

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! 8

Our general class is called a base class Also called a parent class or a superclass Examples: Person, Transportation 9

A specialized class that inherits properties from a base class is called a derived class Also called a child class or a subclass Examples: Student is-a Person Employee is-a Person Car is-a form of Transportation Animal is-a form of Transportation Person Student Undergrad Grad Faculty Employee Staff Masters Doctoral Nondegree 10

Student is a child class of Person Student is also the parent class of Undergrad and Grad Person Student Employee Undergrad Grad Faculty Staff Masters Doctoral Nondegree 11

Enables you to define shared properties and actions once Derived classes can perform the same actions as base classes without having to redefine the actions If desired, the actions can be redefined more on this later 12

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 13

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 14

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 15

public class SmileyMain extends JApplet The SmileyMain class inherits the public instance variables and public methods of the JApplet class 16

private instance variables and private methods in the base class are NOT inherited by derived classes This would not work: public Student(String stdname, int idnumber) name = stdname; // ERROR! name is private to Person setid(idnumber); 17

private instance variables of the base class CAN be accessed by derived classes using the base class public methods This works: public Student(String stdname, int idnumber) setname(stdname); // OK! setname is a public method in Person setid(idnumber); 18

Used to call a constructor of the base class (remember, a base class is also known as a superclass) More details later 19

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); 20

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

If Student inherits the printinfo() method and defines its own printinfo() method, it would seem that Student has two methods with the same signature We saw before that this is illegal, so what s the deal? 22

Java handles this situation as follows: If a derived class defines a method that matches one in the base class in: Name, Number and types of parameters, and Return type Then 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 23

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 24

If a derived class defines a method of the same name, same number and types of parameters, and same return type as a base class method, this is overriding You can still have another method of the same name in the same class, as long as its number or types of parameters are different: overloading 25