Abstract Classes Interfaces CSCI 201 Principles of Software Development

Similar documents
Abstract Classes Interfaces CSCI 201 Principles of Software Development

Polymorphism CSCI 201 Principles of Software Development

Monitors CSCI 201 Principles of Software Development

Inheritance, and Polymorphism.

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Concurrency CSCI 201 Principles of Software Development

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Sleeping Barber CSCI 201 Principles of Software Development

Concurrent Computing CSCI 201 Principles of Software Development

Parallel Computing CSCI 201 Principles of Software Development

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Exception Handling CSCI 201 Principles of Software Development

Composite Pattern - Shapes Example - Java Sourcecode

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

Chapter 9 Inheritance

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

Trees CSCI 201 Principles of Software Development

Arrays Classes & Methods, Inheritance

Java Object Oriented Design. CSC207 Fall 2014

CSC207 Week 3. Larry Zhang

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Chapter 5. Inheritance

Networking Code CSCI 201 Principles of Software Development

C18a: Abstract Class and Method

Friend Functions, Inheritance

Method Overriding in Java

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

SQL CSCI 201 Principles of Software Development

Inheritance, Polymorphism, and Interfaces

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

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

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

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

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Inheritance and Encapsulation. Amit Gupta

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

Instance Members and Static Members

Inheritance and Interfaces

COMP 110/L Lecture 19. Kyle Dewey

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

ECE 122. Engineering Problem Solving with Java

CMSC 132: Object-Oriented Programming II. Inheritance

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

Chapter 14 Abstract Classes and Interfaces

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Character Stream : It provides a convenient means for handling input and output of characters.

INHERITANCE. Spring 2019

OVERRIDING. 7/11/2015 Budditha Hettige 82

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

ITI Introduction to Computing II

C++ Important Questions with Answers

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Reusing Classes. Hendrik Speleers

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

INHERITANCE AND EXTENDING CLASSES

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

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

CSCI 201L Midterm Written SOLUTION Fall % of course grade

Producer/Consumer CSCI 201 Principles of Software Development

ITI Introduction to Computing II

Databases CSCI 201 Principles of Software Development

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

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS-140 Fall 2018 Test 2 Version A Nov. 12, Name:

Notes on Java Programming IST JAV 2012 S. Frénot

Inheritance and Polymorphism

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

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

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

CS1150 Principles of Computer Science Objects and Classes

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Design Patterns: State, Bridge, Visitor

Polymorphism. Agenda

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Abstract Class (2) Abstract Classes and Interfaces. Abstract Class (1) Abstract Class (3) EECS2030: Advanced Object Oriented Programming Fall 2017

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Event Handling Java 7

Computer Science 210: Data Structures

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

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

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

JAVA Programming Language Homework I - OO concept

Abstract Classes and Interfaces

MODULE 3q - An Extended Java Object

COMP 110/L Lecture 6. Kyle Dewey

Enumerations. Fundamentals of Computer Science

Programming in the Large II: Objects and Classes (Part 2)

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

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CS-202 Introduction to Object Oriented Programming

Transcription:

Abstract Classes Interfaces CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu

Abstract Classes Outline USC CSCI 201L

Abstract Classes An abstract class is a way for parent classes to guarantee that child classes provide an implementation for a specific method Consider the Shape example. Even though a Shape does not know how to find the area of a Triangle or Rectangle, it could require that both of those classes implement a getarea() method Abstract methods only contain declarations but no implementations Any class that contains an abstract method must be declared abstract Abstract classes cannot be instantiated since not all of the methods have implementations Any class that inherits from an abstract class must implement all of the abstract methods or declare itself abstract When a class implements an abstract method, it is said to override that method Abstract Classes USC CSCI 201L 3/11

Abstract Class Example 1 abstract class Parent { 2 public abstract int meth1(); 3 public int meth() { 4 return 10; 5 } 6 } 7 8 class Child extends Parent { 9 public int meth1() { 10 return 20; 11 } 12 } 13 public class Test { 14 public static void main(string [] args) { 15 Child c = new Child(); 16 System.out.println(c.meth()); 17 System.out.println(c.meth1()); 18 Parent p = new Parent(); 19 System.out.println(p.meth()); 20 System.out.println(p.meth1()); 21 } 22 } Abstract Classes USC CSCI 201L 4/11

Abstract Class Example 1 abstract class Parent { 2 public abstract int meth1(); 3 public int meth() { 4 return 10; 5 } 6 } 7 8 class Child extends Parent { 9 public int meth1() { 10 return 20; 11 } 12 } 13 public class Test { 14 public static void main(string [] args) { 15 Child c = new Child(); 16 System.out.println(c.meth()); 17 System.out.println(c.meth1()); 18 Parent p = new Child(); 19 System.out.println(p.meth()); 20 System.out.println(p.meth1()); 21 } 22 } Abstract Classes USC CSCI 201L 5/11

Abstract Classes Outline USC CSCI 201L

Interfaces An interface is similar to a class, but there are no method implementations in it (not even inherited) When a class implements an interface, it must implement all of the methods in the interface If it doesn t implement all of the methods, it has then inherited an abstract method, so the class must be declared abstract A class can implement as many interfaces as it wants This is how Java deals with supporting something similar to multiple inheritance This is different than multiple inheritance though. How? If the same method is inherited from more than one interface, there is no implementation, so there is no confusion If interfaces inherit from other interfaces, they will extend them USC CSCI 201L 7/11

Multiple Inheritance and Interfaces In C++, we had a problem with multiple inheritance when the same function was implemented in two different branches of the hierarchy Telephone make_call() send_email() Email_Reader read_email() send_email() Which send_email() function gets called when you call it on an instance of IPhone? IPhone buy_app() IPhone ip; ip.send_email(); It doesn t matter which one gets called if the Telephone and Email_Reader classes only contained function definitions and the only implementation was in IPhone USC CSCI 201L 8/11

Interface Example 1 interface Parent { 2 public abstract int meth1(); 3 public int meth(); 4 } 5 6 abstract class Child implements Parent { 7 public int meth1() { 8 return 20; 18 public class Test { 9 } 19 public static void main(string [] args) { 10 } 20 GrandChild gc = new GrandChild(); 11 21 System.out.println(gc.meth()); 12 class GrandChild extends Child { 22 System.out.println(gc.meth1()); 13 public int meth() { 23 Child c = new Child(); 24 System.out.println(c.meth()); 14 return 30; 25 System.out.println(c.meth1()); 15 } 26 } 16 } 27 } 17 USC CSCI 201L 9/11

Interface Example 1 interface Parent { 2 public abstract int meth1(); 3 public int meth(); 4 } 5 6 abstract class Child implements Parent { 7 public int meth1() { 8 return 20; 18 public class Test { 9 } 19 public static void main(string [] args) { 10 } 20 GrandChild gc = new GrandChild(); 11 21 System.out.println(gc.meth()); 12 class GrandChild extends Child { 22 System.out.println(gc.meth1()); 13 public int meth() { 23 Child c = new GrandChild(); 24 System.out.println(c.meth()); 14 return 30; 25 System.out.println(c.meth1()); 15 } 26 } 16 } 27 } 17 USC CSCI 201L 10/11

Interface Example 1 interface Parent { 2 public abstract int meth1(); 3 public int meth(); 4 } 5 6 abstract class Child implements Parent { 7 public int meth1() { 8 return 20; 18 public class Test { 9 } 19 public static void main(string [] args) { 10 } 20 GrandChild gc = new GrandChild(); 11 21 System.out.println(gc.meth()); 12 class GrandChild extends Child { 22 System.out.println(gc.meth1()); 13 public int meth() { 23 Parent p = new GrandChild(); 24 System.out.println(p.meth()); 14 return 30; 25 System.out.println(p.meth1()); 15 } 26 } 16 } 27 } 17 USC CSCI 201L 11/11