Abstract Classes and Interfaces

Similar documents
Polymorphism and Interfaces. CGS 3416 Spring 2018

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

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

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

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Chapter 14 Abstract Classes and Interfaces

CS 251 Intermediate Programming Inheritance

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

CS/ENGRD 2110 SPRING 2018

OVERRIDING. 7/11/2015 Budditha Hettige 82

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

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

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

First IS-A Relationship: Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Polymorphism

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

Need to store a list of shapes, each of which could be a circle, rectangle, or triangle

Inheritance. Transitivity

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

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

CS111: PROGRAMMING LANGUAGE II

Inheritance, Polymorphism, and Interfaces

11. Abstract Classes and Interfaces

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Inheritance (Outsource: )

8. Polymorphism and Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

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

INHERITANCE. Spring 2019

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

Inheritance and Polymorphism. CS180 Fall 2007

5.6.1 The Special Variable this

Java Programming Lecture 7

More Relationships Between Classes

Inheritance & Polymorphism

C09: Interface and Abstract Class and Method

In Java, to create a class named "B" as a subclass of a class named "A", you would write

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Object-oriented programming in Java (2)

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

C09: Interface, and Abstract Class and Method

index.pdf January 21,

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CS260 Intro to Java & Android 03.Java Language Basics

ITI Introduction to Computing II

CS 11 java track: lecture 3

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

ITI Introduction to Computing II

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Virtual functions concepts

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Software Development (cs2500)

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

We are on the GUI fast track path

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Inheritance, polymorphism, interfaces

COMP 110/L Lecture 19. Kyle Dewey

CS 162, Lecture 25: Exam II Review. 30 May 2018

Inheritance & Polymorphism

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

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

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

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Chapter 6 Introduction to Defining Classes

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

COMP 110/L Lecture 20. Kyle Dewey

Abstract. 1. What is an ABSTRACT METHOD? 2. Why you would want to declare a method as abstract? 3. A non-abstract CLASS is called a concrete class

Object-Oriented Programming

Object-Oriented Concepts and Design Principles

CSCE3193: Programming Paradigms

Java Object Oriented Design. CSC207 Fall 2014

CMSC 132: Object-Oriented Programming II

Chapter 10 Classes Continued. Fundamentals of Java

Inheritance, and Polymorphism.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CS 61B Discussion 4: Inheritance Fall 2018

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

Object Oriented C# Classes. The OO features of C# are very similar to Java. We ll point the exceptions along the way. 1. A simple C# class.

The init() Method. Browser Calling Applet Methods

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

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

CS100J Prelim I, 29 Sept. 2003

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

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

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

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

CISC 1600 Lecture 3.1 Introduction to Processing

Transcription:

Abstract methods Abstract Classes and Interfaces You can declare an object without defining it: Person p; Similarly, you can declare a method without defining it: public abstract void draw(int size); Notice that the body of the method is missing A method that has been declared but not defined is an abstract method 30-Oct-03 2 Abstract classes I Any class containing an abstract method is an abstract class You must declare the class with the keyword abstract: abstract class MyClass {... An abstract class is incomplete It has missing method bodies You cannot instantiate (create a new instance of) an abstract class Abstract classes II You can extend (subclass) an abstract class If the subclass defines all the inherited abstract methods, it is complete and can be instantiated If the subclass does not define all the inherited abstract methods, it too must be abstract You can declare a class to be abstract even if it does not contain any abstract methods This prevents the class from being instantiated 3 4 1

Why have abstract classes? Suppose you wanted to create a class Shape, with subclasses Oval, Rectangle, Triangle, Hexagon, etc. You don t want to allow creation of a Shape Only particular shapes make sense, not generic ones If Shape is abstract, you can t create a new Shape You can create a new Oval, a new Rectangle, etc. Abstract classes are good for defining a general category containing specific, concrete classes An example abstract class public abstract class Animal { abstract int eat(); abstract void breathe(); This class cannot be instantiated Any non-abstract subclass of Animal must provide the eat() and breathe() methods 5 6 Why have abstract methods? Suppose you have a class Shape that isn t abstract Shape should not have a draw() method Each subclass of Shape should have a draw() method Now suppose you have a variable Shape figure; where figure contains some subclass object (such as a Star) It is a syntax error to say figure.draw(), because the Java compiler can t tell in advance what will be in the figure variable Solution: Give Shape an abstract method draw() Now the class Shape is abstract, so it can t be instantiated The figure variable cannot contain a (generic) Shape, because it is impossible to create one Any object (such as a Star object) that is a (kind of) Shape will have the draw() method The Java compiler can depend on figure.draw() being a legal call and does not give a syntax error Interfaces An interface declares (describes) methods but does not supply bodies for them interface KeyListener { public void keypressed(keyevent e); public void keyreleased(keyevent e); public void keytyped(keyevent e); All the methods are implicitly public and abstract You can add these qualifiers if you like, but why bother? You cannot instantiate an interface An interface is like a very abstract class none of its methods are defined An interface may also contain constants (final variables) 7 8 2

Designing interfaces Most of the time, you will use Sun-supplied Java interfaces Sometimes you will want to design your own You would write an interface if you want classes of various types to all have a certain set of capabilities For example, if you want to be able to create animated displays of objects in a class, you might define an interface as: public interface Animatable { install(panel p); display(); Now you can write code that will display any Animatable class in a Panel of your choice, simply by calling these methods Implementing an interface I You extend a class, but you implement an interface A class can only extend (subclass) one other class, but it can implement as many interfaces as you like class MyListener implements KeyListener, ActionListener { 9 10 Implementing an interface II When you say a class implements an interface, you are promising to define all the methods that were declared in the interface class MyKeyListener implements KeyListener { public void keypressed(keyevent e) {...; public void keyreleased(keyevent e) {...; public void keytyped(keyevent e) {...; Now you can create a new MyKeyListener Partially implementing an Interface It is possible to define some but not all of the methods defined in an interface: abstract class MyKeyListener implements KeyListener { public void keytyped(keyevent e) {...; Since this class does not supply all the methods it has promised, it is an abstract class You must label it as such with the keyword abstract 11 12 3

What are interfaces for? Reason 1: A class can only extend one other class, but it can implement multiple interfaces This lets the class fill multiple roles In writing Applets, it is common to have one class implement several different listeners class MyApplet extends Applet implements ActionListener, KeyListener {... Reason 2: You can write methods that work for more than one kind of class How to use interfaces You can write methods that work with more than one class interface RuleSet { boolean islegal(move m, Board b); void makemove(move m); Every class that implements RuleSet must have test methods class CheckersRules implements RuleSet { // one implementation public boolean islegal(move m, Board b) {... public void makemove(move m) {... class ChessRules implements RuleSet {... // another implementation class LinesOfActionRules implements RuleSet {... // and another RuleSet rulesofthisgame = new ChessRules(); This assignment is legal because a rulesofthisgame object is a RuleSet object if (rulesofthisgame.islegal(m, b)) { void makemove(m); This method is legal because, whatever kind of RuleSet object rulesofthisgame is, it must have islegal and makemove methods 13 14 instanceof Interfaces, again instanceof is a keyword that tells you whether a variable is a member of a class or interface For example, if class Dog extends Animal implements Pet {... Animal fido = new Dog(); then the following are all true: fido instanceof Dog fido instanceof Animal fido instanceof Pet instanceof is seldom used When you find yourself wanting to use instanceof, think about whether the method you are writing should be moved to the individual subclasses When you implement an interface, you promise to define all the functions it declares There can be a lot of methods interface KeyListener { public void keypressed(keyevent e); public void keyreleased(keyevent e); public void keytyped(keyevent e); What if you only care about a couple of these methods? 15 16 4

Adapter classes Solution: use an adapter class An adapter class implements an interface and provides empty method bodies class KeyAdapter implements KeyListener { public void keypressed(keyevent e) { ; public void keyreleased(keyevent e) { ; public void keytyped(keyevent e) { ; You can override only the methods you care about This isn t elegant, but it does work Java provides a number of adapter classes Vocabulary abstract method a method which is declared but not defined (it has no method body) abstract class a class which either (1) contains abstract methods, or (2) has been declared abstract instantiate to create an instance (object) of a class interface similar to a class, but contains only abstract methods (and possibly constants) adapter class a class that implements an interface but has only empty method bodies 17 18 5