ECE 122. Engineering Problem Solving with Java

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

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Interfaces. Quick Review of Last Lecture. November 6, Objects instances of classes. Static Class Members. Static Class Members

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

Inheritance -- Introduction

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

Chapter 6 Reflection

&KDSWHU(QKDQFLQJ&ODVVHV

CS1004: Intro to CS in Java, Spring 2005

Inheritance and Polymorphism

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Enhanced Class Design -- Introduction

Polymorphism. Agenda

INTERFACE WHY INTERFACE

Chapter 4: Writing Classes

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

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

Anatomy of a Class Encapsulation Anatomy of a Method

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

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

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

ECE 122. Engineering Problem Solving with Java

CPSC 427a: Object-Oriented Programming

ECE 122. Engineering Problem Solving with Java

Super-Classes and sub-classes

Inheritance. COMP Week 12

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CS 251 Intermediate Programming Inheritance

CS260 Intro to Java & Android 03.Java Language Basics

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Java Object Oriented Design. CSC207 Fall 2014

Assignment 1 due Monday at 11:59pm

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

INHERITANCE. Spring 2019

CPSC 427: Object-Oriented Programming

Arrays Classes & Methods, Inheritance

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

S <: T Type compa*bility rela*on.

CS-202 Introduction to Object Oriented Programming

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

What is Inheritance?

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

ITI Introduction to Computing II

ECE 122. Engineering Problem Solving with Java

Interfaces, Testing, and Layout Managers

CMSC 132: Object-Oriented Programming II

ITI Introduction to Computing II

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Object-Oriented Concepts

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

Inheritance. Transitivity

Inheritance, Polymorphism, and Interfaces

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Programming in C# Inheritance and Polymorphism

Chapter 10: Inheritance

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CSCE3193: Programming Paradigms

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

Lecture 4: Extending Classes. Concept

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

CS111: PROGRAMMING LANGUAGE II

OVERRIDING. 7/11/2015 Budditha Hettige 82

ITI Introduction to Computing II

CLASS DESIGN. Objectives MODULE 4

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

CS 112 Introduction to Programming

Inheritance (IS A Relationship)

Admin. q Admin and recap q Class inheritance

Java Fundamentals (II)

Inheritance and Substitution (Budd chapter 8, 10)

Subclasses, Superclasses, and Inheritance

Chapter 11: Inheritance

Chapter 6 Introduction to Defining Classes

Objects and Classes -- Introduction

Programming overview

More on Inheritance. Interfaces & Abstract Classes

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Chapter 9 Inheritance

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

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

C++ Important Questions with Answers

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

MODULE 3q - An Extended Java Object

Comparing Objects 3/14/16

COMP 250 Fall inheritance Nov. 17, 2017

Transcription:

ECE 122 Engineering Problem Solving with Java Lecture 21 Interfaces and Abstract Classes

Overview Problem: Can we make inheritance flexible? Abstract methods Define methods that will be filled in by children Interfaces Define common methods which may be implemented by several classes Visibility Clever techniques to access hidden data

Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish a set of methods that a class will implement

Interfaces interface is a reserved word None of the methods in an interface are given public interface Doable a definition (body) { public void dothis(); public int dothat(); public void dothis2 (float value, char ch); public boolean dotheother (int num); } A semicolon immediately follows each method header

Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by: stating so in the class header providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

Interfaces public class CanDo implements Doable { public void dothis () { // whatever } implements is a reserved word public void dothat () { // whatever } Each method listed in Doable is given a definition } // etc.

Interfaces A class that implements an interface can implement other methods as well In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

//************************************************ // Complexity.java Author: Lewis/Loftus // // Represents the interface for an object that can be assigned // an explicit complexity. //************************************************ public interface Complexity { public void setcomplexity (int complexity); public int getcomplexity(); } Note the semicolon after the abstract methods! Note: public interface

Interfaces A class can implement multiple interfaces The interfaces are listed in the implements clause The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains one abstract method called compareto, which is used to compare two objects We discussed the compareto method of the String class in Chapter 5 The String class implements Comparable, giving us the ability to put strings in lexicographic order

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type if (obj1.compareto(obj2) < 0) System.out.println ("obj1 is less than obj2"); The value returned from compareto should be negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer designs a class that implements the Comparable interface, it should follow this intent

The Comparable Interface It's up to the programmer to determine what makes one object less than another Possible to define the compareto method of an Employee class to order employees Ordering by name (alphabetically) or by employee number The implementation of the method can be as straightforward or as complex as needed for the situation

Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy Business RetailBusiness ServiceBusiness KMart Macys Kinkos

Class Hierarchies Two children of the same parent are called siblings Common features should be put as high in the hierarchy as is reasonable An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes There is no single class hierarchy that is appropriate for all situations

The Object Class A class Object is defined in the java.lang package of the Java standard class library All classes are derived from the Object class Sometimes a class is not explicitly defined to be the child of an existing class It is assumed to be the child of the Object class The Object class is the ultimate root of all class hierarchies

The Object Class The Object class contains a few useful methods, which are inherited by all classes For example, the tostring method is defined in the Object class Every time we define the tostring method, we are actually overriding an inherited definition The tostring method is in the Object class Defined to return a string that contains the name of the object s class along with some other information

The Object Class The equals method of the Object class returns true if two references are aliases We can override equals in any class to define equality in some more appropriate way The String class defines the equals method Returns true if two String objects contain the same characters String class overrides the equals method inherited from Object

Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract: public abstract class Product { // contents }

Abstract Classes An abstract class often contains abstract methods with no definitions (like an interface) Unlike an interface, the abstract modifier must be applied to each abstract method Also, an abstract class typically contains nonabstract methods with full definitions A class declared as abstract does not have to contain abstract methods

Abstract Classes The child of an abstract class must override the abstract methods of the parent, Otherwise, it too will be considered abstract An abstract method cannot be defined as final or static The use of abstract classes is an important element of software design It allows us to establish common elements in a hierarchy that are too generic to instantiate

Interface Hierarchies Inheritance can be applied to interfaces as well as classes That is, one interface can be derived from another interface The child interface inherits all abstract methods of the parent A class implementing the child interface must define all methods Includes both the ancestor and child interfaces Note that class hierarchies and interface hierarchies are distinct (they do not overlap)

Visibility Revisited It's important to understand one subtle issue related to inheritance and visibility All variables and methods of a parent class, even private members, are inherited by its children As we've mentioned, private members cannot be referenced by name in the child class However, private members inherited by child classes exist and can be referenced indirectly The parent can refer to the private member The child can reference it indirectly using its parent's methods The super reference can be used to refer to the parent class, even if no object of the parent exists

Summary Inheritance is an important part of object oriented programming Interfaces allow us to define common methods which can be implemented by one or more classes Abstract classes define a placeholder for methods which will be implemented later Most programmers use basic techniques. Too much complexity gets confusing