CMSC 132: Object-Oriented Programming II. Inheritance

Similar documents
Darrell Bethea June 6, 2011

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

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

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

ECE 122. Engineering Problem Solving with Java

C08: Inheritance and Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

An example from Zoology

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

Implements vs. Extends When Defining a Class

CS-202 Introduction to Object Oriented Programming

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

Overriding Variables: Shadowing

C08: Inheritance and Polymorphism

Inheritance and Polymorphism

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

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

Abstract class & Interface

Week 5-1: ADT Design

Inheritance Motivation

CIS 110: Introduction to computer programming

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Person class. A class can be derived from an existing class by using the form

Person class. A class can be derived from an existing class by using the form

Java Object Oriented Design. CSC207 Fall 2014

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

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

Lecture 18 CSE11 Fall 2013 Inheritance

Introduction to Computation and Problem Solving

Week 11: Class Design

Polymorphism CSCI 201 Principles of Software Development

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

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

Lecture 4: Extending Classes. Concept

Classes and Inheritance Extending Classes, Chapter 5.2

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

1.00 Lecture 14. Exercise: Plants

Distributed Systems Recitation 1. Tamim Jabban

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CS1150 Principles of Computer Science Objects and Classes

Programming in C# Inheritance and Polymorphism

CS 251 Intermediate Programming Inheritance

CS111: PROGRAMMING LANGUAGE II

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Object Oriented Programming

Object Oriented Relationships

OVERRIDING. 7/11/2015 Budditha Hettige 82

INHERITANCE. Spring 2019

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Learn more about our research, discover data science, and find other great resources at:

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

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

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

Chapter 12: Inheritance

Inheritance (Deitel chapter 9)

Inheritance and Polymorphism

Making New instances of Classes

Full file at Chapter 2 - Inheritance and Exception Handling

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Programming overview

Exam Duration: 2hrs and 30min Software Design

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Create a Java project named week10

1.00 Lecture 13. Inheritance

What is Inheritance?

Informatik II (D-ITET) Tutorial 6

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

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Inheritance and Polymorphism

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

CS 149. Professor: Alvin Chao

Java Fundamentals (II)

Distributed Systems Recitation 1. Tamim Jabban

JAVA Programming Language Homework I - OO concept

Inheritance. Chapter 7. Chapter 7 1

Inheritance -- Introduction

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

Object-Oriented Programming (Java)

Introduction. Introduction. Introduction

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Object-Oriented Concepts

Inheritance, Polymorphism, and Interfaces

CMSC 132: Object-Oriented Programming II

CS 113 PRACTICE FINAL

Inheritance (continued) Inheritance

Data Abstraction. Hwansoo Han

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

F I N A L E X A M I N A T I O N

ENCAPSULATION AND POLYMORPHISM

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

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

CSC 1214: Object-Oriented Programming

Transcription:

CMSC 132: Object-Oriented Programming II Inheritance 1

Mustang vs Model T Ford Mustang Ford Model T 2

Interior: Mustang vs Model T 3

Frame: Mustang vs Model T Mustang Model T 4

Compaq: old and new Price: US$3590 Weight: 28 pounds CPU: Intel 8088, 4.77MHz RAM: 128K, 640K max 5

Inheritance Classes can be derived from other classes, thereby inheriting fields and methods from those classes. A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class). Derived (Child) class can be base (parent) class 6

Inheritance Motivation: In real life objects have a hierarchical structure: Shape Circle Triangle Rectangle Right -Triangle Equilateral - Triangle Square 7

Inheritance Define a general class Later, define specialized classes based on the general class These specialized classes inherit properties from the general class Person Student Employee Undergrad Grad Faculty Staff 8

Inheritance Person Student Employee Undergrad Grad Faculty Staff Person: name, address, phone, email Student: college, major, gpa Employee: Salary, datehired, office Faculty: rank, officehours Staff: title Undergrad: freshman,sophomore, junior, or senior) Grad: advisor, level (ms or phd) 9

Inheritance cont. What are some properties of a Person? name, height, weight, age How about a Student? ID, major, gpa Does a Student have a name, height, weight, and age? Student inherits these properties from Person 10

is-a relationship This inheritance relationship is known as an is-a relationship A Grad student is a Student A Student is a Person. Is a Person a Student? Not necessarily! 11

Why inheritance is useful 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 method overriding 12

Person Class public class Person { private String name; public Person(){ name = "noname"; public Person(String name){ this.name = name; public void setname(string newname){ name = newname; public String getname(){ return name; @Override public String tostring(){ return "Name:"+name; -name Person +Person() +Person(String name):void +setname(string name) : void +getname() : String 13

Student Class public class Student extends Person{ private int id; public Student() { id = 0; public Student(String name, int id) { super(name); this.id = id; public void setid(int idnumber) { id = idnumber; public int getid(){ return id; @Override public String tostring(){ return "Id:"+ id +"\tname:" + getname(); Person -name +Person() +Person(String name):void +setname(string name) : void +getname() : String Student 14 -id +Student() +Student(String name, int id) : void +setid(int id) : void +getid(): int +tostring() : String

Dissecting the Student Class Extends: To specify that Student is a derived class (subclass) of Person we add the descriptor extends to the class definition: public class Student extends Person { Notice that a Student class Inherits everything from the Person class A Student IS-A Person (wherever a Person is needed, we can use a Student). 15

Super() super( ): When initializing a new Student object, we need to initialize its base class (or superclass). This is done by calling super( ). For example, super( name) invokes the constructor Person( name) super( ) must be the first statement of your constructor If you do not call super( ), Java will automatically invoke the base class s default constructor What if the base class s default constructor is undefined? Error You must use super( ), not Person( ). 16

Memory Layout and Initialization Order When you create a new derived class object: Java allocates space for both the base class instance variables and the derived class variables Java initializes the base class variables first, and then initializes the derived class variables Example: Person ted = new Person( "Ted Goodman"); Student bob = new Student( "Bob Goodstudent", 100); ted name Ted Goodman bob name id Bob Goodstudent 100 17

Inheritance Inheritance: Since Student is derived from Person, a Student object can invoke any of the Person methods, it inherits them Student bob = new Student("Bob Goodstudent", 100); String bobsname = bob.getname( ) ); bob.setname( "Robert Goodstudent" ); System.out.println( "Bob's new info: " + bob.tostring( ) ); 18

Inheritance A Student is a Person: By inheritance a Student object is also a Person object. We can use a Student reference anywhere that a Person reference is needed Person robert = bob; // Okay: A Student is a Person We cannot reverse this. (A Person need not be a Student.) Student bob2 = robert; // Error! Cannot convert Person to Student 19

Overriding Methods New Methods: A derived class can define entirely new instance variables and new methods (e.g. gpa and getgpa()) Overriding: A derived class can also redefine existing methods public class Person { public String tostring() { public class Student extends Person { public String tostring() { The derived class can redefine this method. Student bob = new Student( "Bob Goodstudent", 100); System.out.println("Bob's info: " + bob); Since bob is of type Student, this invokes the Student tostring( ) 20

Overriding and Overloading Don t confuse method overriding with method overloading. Overriding: occurs when a derived class defines a method with the same name and parameters as the base class. Overloading: occurs when two or more methods have the same name, but have different parameters (different signature). Example: public class Person { public void setname(string n) { name = n; public class Faculty extends Person { public void setname(string n) { super.setname( The Evil Professor + n); public void setname(string first, String last) { super.setname(first + + last); The base class defines a method setname( ) Overriding: Same name and parameters; different definition. Overloading: Same name, but different parameters. 21

Quiz 1: Output of following program class Test { int i; class Main { public static void main(string args[]){ Test t; System.out.println(t.i); A. 0 B. garbage value C. compiler error D. runtime error 22

Quiz 1: Output of following program class Test { int i; class Main { public static void main(string args[]){ Test t; System.out.println(t.i); A. 0 B. garbage value C. compiler error: variable not initialized. D. runtime error 23

Quiz 2: Output of following program class Test { int i; class Main { public static void main(string args[]){ Test t = null; System.out.println(t.i); A. 0 B. garbage value C. compiler error D. runtime error 24

Quiz 2: Output of following program class Test { int i; class Main { public static void main(string args[]){ Test t = null; System.out.println(t.i); A. 0 B. garbage value C. compiler error D. runtime error: Null pointer exception 25

Quiz 3: Output of following program class Base{ void display() {System.out.print( Base "); class Child extends Base{ void display(){system.out.print( Child "); Base b= new Base(); Child c = new Child (); Base ref = b; ref.display(); ref = c; ref.display(); A. Compilation error B. Base Child C. Child Base D. Runtime error 26

Quiz 3: Output of following program class Base{ void display() {System.out.print( Base "); class Child extends Base{ void display(){system.out.print( Child "); Base b= new Base(); Child c = new Child (); Base ref = b; ref.display(); ref = c; ref.display(); A. Compilation error B. Base Child C. Child Base D. Runtime error 27