Implements vs. Extends When Defining a Class

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

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

Exercise: Singleton 1

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

CMSC 132: Object-Oriented Programming II. Inheritance

Practice for Chapter 11

Inheritance -- Introduction

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Lecture 4: Extending Classes. Concept

Java Fundamentals (II)

25. Generic Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Example: Count of Points

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Inheritance and Polymorphism

First IS-A Relationship: Inheritance

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

More Relationships Between Classes

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Week 5-1: ADT Design

What is Inheritance?

Chapter 5 Object-Oriented Programming

Java Object Oriented Design. CSC207 Fall 2014

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

CO Java SE 8: Fundamentals

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

Inheritance (continued) Inheritance

Class, Variable, Constructor, Object, Method Questions

CH. 2 OBJECT-ORIENTED PROGRAMMING

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CISC370: Inheritance

CS 251 Intermediate Programming Inheritance

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

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

Polymorphism. return a.doublevalue() + b.doublevalue();

Java: introduction to object-oriented features

CIS 110: Introduction to computer programming

CS-202 Introduction to Object Oriented Programming

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CMSC 132: Object-Oriented Programming II. Interface

Rules and syntax for inheritance. The boring stuff

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Java and C# in Depth

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

COP 3330 Final Exam Review

CS260 Intro to Java & Android 03.Java Language Basics

Objects and Iterators

1 Shyam sir JAVA Notes

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: Based on slides from Skrien Chapter 2

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming in C# Inheritance and Polymorphism

Abstract Classes and Interfaces

Computer Science II (20073) Week 1: Review and Inheritance

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

Lecture 18 CSE11 Fall 2013 Inheritance

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Operators and Expressions

Questions Answer Key Questions Answer Key Questions Answer Key

ECE 122. Engineering Problem Solving with Java

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CMSC 132: Object-Oriented Programming II

OBJECT ORİENTATİON ENCAPSULATİON

Type Hierarchy. Lecture 6: OOP, autumn 2003

Compaq Interview Questions And Answers

COMP 401 Spring 2013 Midterm 2

Overriding Variables: Shadowing

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

COMP 401 Spring 2013 Midterm 2

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

Example: Count of Points

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

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Java Magistère BFA

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?


CLASS DESIGN. Objectives MODULE 4

C++ Programming: Polymorphism

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

Instantiation of Template class

1.00 Lecture 13. Inheritance

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Lecture 5: Inheritance

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Object Oriented Java

Super-Classes and sub-classes

Transcription:

Implements vs. Extends When Defining a Class implements: Keyword followed by the name of an INTERFACE Interfaces only have method PROTOTYPES You CANNOT create on object of an interface type extends: Keyword followed by the name of a BASE CLASS Base class contains method IMPLEMENTATIONS Allows INHERITANCE!! You CAN create objects of that base class type 0

Example: People at University Base class: person Derived classes: student, faculty, administrator Derived from those: undergrad, grad, instructor, professor, Person Student Faculty Administrator Undergrad GradStudent Instructor Professor 1

University Person Example extends Person class: Student instance variables: int admityear double gpa methods: Student( ) [various] int getadmityear( ) double getgpa( ) void setadmityear( int ) void setgpa( double ) String tostring( ) boolean equals( Student ) class: Person instance variables: String name String idnum methods: Person( ) [various] String getname( ) String getidnum( ) void setname( String ) void setidnum( String ) String tostring( ) boolean equals( Person ) extends Person class: Faculty instance variables: int hireyear methods: Faculty( ) [various] int hireyear( ) void sethireyear( int ) String tostring( ) boolean equals( Faculty ) 2

Using super Lets implement the Student class Constructors: Typical (with parameters) No arg Constructor Copy Constructor Overridden methods: tostring equals 3

Overriding vs. Overloading Overriding: a derived class defines a method with same name, parameters as base class Overloading: two or more methods have the same name, but different parameters Example public class Person { Base class setname( ) public void setname( String n ) { name = n; public class Faculty extends Person { public void setname( String n ) { Overriding super.setname( The Evil Professor + n ); public void setname( String first, String last ) { Overloading super.setname( first + + last ); 4

Early vs. Late Binding Consider: Person p = new Student(); System.out.println( p.tostring() ); Which version of tostring Person or Student is called? Early (static) binding p is declared to be of type Person Therefore, the Person version of tostring is used Late (dynamic) binding The object to which p refers was created as Student object Therefore, the Student version of tostring is used Java uses late binding (C++ by default uses early binding) Early binding is more runtime efficient (decisions about method versions can be made at compile time) Late binding respects encapsulation (object defines its operations when it is created) 5

Polymorphism Java s late binding makes it possible for a single reference variable to refer to objects of many different types. Such a variable is said to be polymorphic (meaning having many forms). Example: Create an array of various university people and print. Person[ ] list = new Person[3]; list[0] = new Person( "Col. Mustard", "000-00-0000" ); list[1] = new Student ( "Ms. Scarlet", "111-11-1111", 1998, 3.2 ); list[2] = new Faculty ( "Prof. Plum", "222-22-2222", 1981 ); for ( int i = 0; i < list.length; i++ ) System.out.println( list[i].tostring( ) ) Output: [Col. Mustard] 000-00-0000 [Ms. Scarlet] 111-11-1111 1998 3.2 [Prof. Plum] 222-22-2222 1981 What type is list[i]? It can be a reference to any object that is derived from Person. The appropriate tostring will be called. 6

Public, Protected, Package(default) and Private Select which level of visibility Access Levels Access Level/Group Class Package SubClass World public Y Y Y Y protected(avoid) Y Y Y N package (default) Y Y N N private Y N N N 7

Shadowing Can we override instance variables just like methods? Yes, but be careful! Overriding instance variable is called shadowing Shadowing hides instance variables of base class (can still access them using super.varname in subclass, but not in outside world ) public class Person { String name; public class Administrator extends Person { String name; // name refers to Administrator s name Confusing! Better to pick a new variable name 8

Example of Overloading/Overriding public class Base { public void m (int x) { public class Derived extends Base { public void m (int x) { public int m (int x) { public void m (double d) { Overriding: with increased visibility Error! duplicate method declaration Overloading // The following appears in the same package as above Base b = new Base( ); Base d = new Derived( ); Derived e = new Derived( ); b.m (5); d.m (6); d.m (7.0); e.m (8.0); calls Base:m(int) calls Derived:m(int) Error! Since d is declared Base, the compiler looks for Base:m(double) Doesn t exist! So this does not make it past the compiler, even though Derived:m(double) is defined! calls Derived:m(double) 9

Object Recall: inheritance induces is-a hierarchy on classes Undergrad is-a Student Student is-a Person etc. Person is-a.? Person is-a (n) Object Student is-a (n) Object Object Person Student Faculty Administrator Undergrad GradStudent Instructor Professor 10

More on Object Special class at top of class inheritance hierarchy Defined in java.lang Every class is derived (either directly or indirectly) from Object e.g. public class Foo { is equivalent to public class Foo extends Object { Structure of Object No instance variables A number of methods, including: tostring() equals (Object o) Let s look at the Javadoc 11

Class vs. Type Information In Java Every object is in one class (the one it was created from using new) Objects may have many types (all those that class is based on) Interfaces Superclasses E.g. consider Student bob = new Student(); Person p = bob; Class of object pointed to by bob and p is Student Type of object can be Student, Person, Object, etc. 12

Accessing Type Information instanceof Java boolean operator (not a method) Returns true if given object is-a (n) object of given (class) type E.g. Student carol = new Student ( ); if (carol instanceof Person) // true, because carol is-a Person 13

Object Casting Recall casting in primitive types Casting: conversion of elements from one type to another Widening Conversion (always OK) double x = 3; // 3 (int) widening conversion to double Narrowing Conversion Must use explicit type conversions to perform this casting int x = (int)3.0; // 3.0 explicitly cast to int Similar notions can be found with object types also Upcasting Casting a reference to a superclass (casting up the inheritance tree) Always OK Just ignore the parts that were added by the subclass Downcasting Casting a reference to a derived class Requires explicit casting operator Can cause runtime error 14

Object Casting Person p = new Person(); Student s = new Student(); Person tricky = new Student(); Person x = s; Person x = s; Student y = p; Student y = (Student)p; Student y = tricky; Student y = (Student)tricky; (Faculty)s (Faculty)tricky Upcast works fine Downcast Does not compile Compiles, but throws Exception Does not compile Works fine Does not compile Compiles, but throws Exception 15

Safe Downcasting Illegal downcasting results in a thrown ClassCastException at run-time Q: Can we check for the legality of a cast before trying it? A: Yes, using instanceof Example Given: ArrayList of university people Want: Print the GPAs of the students Solution approach Iterate through list Print GPAs only of Students 16

equals() Reconsidered Recall definition of equals() in Person public boolean equals (Person p) { if (p == null){ return false; return name.equals(p.getname()) && idnum.equals(p.getidnum()); in Student public boolean equals( Student s ) { if (s == null){ return false; return super.equals(s) && admityear == s.admityear && gpa == s.gpa; What does following do? public static void main (String[] args) { Student bob = new Student ("R. Goode", "234-56-7890", 1998, 3.89); Faculty bob2 = new Faculty ("R. Goode", "234-56-7890", 2005); System.out.println (bob.equals (bob2)); true is printed! 17

A Better equals() Take Object as parameter Check for non-null-ness of parameter Check that class type is correct Then do other checks For example in Person: public boolean equals (Object o) { if (! (o instanceof Person)) { return false; Person p = (Person)o; return name.equals(p.getname()) && idnum.equals(p.getidnum()); Similar improvements can be made to Student, Faculty Now bob.equals(bob2) returns false 18

A Better equals() Let s revisit the Person and Student classes 19

Multiple Inheritance? Intuitively useful to be able to inherit from multiple classes (multiple inheritance) Person Student Athlete Faculty StudentAthlete But Java does not allow this 20

Why Does Java Disallow Multiple Inheritance? Semantic difficulties! Consider StudentAthlete Objects would get name field from Student Objects would also get name field from Athlete Duplicate fields: what to do? Some languages (e.g. C++) do allow multiple inheritance Person Student Athlete StudentAthlete 21

Can We Achieve Some of Benefits of Multiple Inheritance in Java? Yes, using interfaces + inheritance Idea: use inheritance for one of inherited classes, interfaces for others Interfaces ensure that relevant methods are implemented Example public class Person { public class Student extends Person { public interface Athlete { public String getsport (); public void setsport (String sport); public class StudentAthlete extends Student implements Athlete { Objects of type StudentAthlete are Students They also can be wherever objects matching Athlete are required 22

Interfaces and Constants Interfaces can also contain public final static variables Sometimes interfaces are used to provide consistent definitions for constants throughout an application Example public interface Months { public final static int JANUARY = 1; public final static int FEBRUARY = 2; public final static int MARCH = 3; public final static int DECEMBER = 12; public class MonthDemo implements Months { public static void main( String[ ] args ) { System.out.println( "March is month number " + MARCH ); Because MonthDemo implements Months, it has access to the constants 23