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

Similar documents
Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and Polymorphism

Inheritance, and Polymorphism.

Chapter 10 Classes Continued. Fundamentals of Java

What is Inheritance?

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

Inheritance Motivation

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

CS-202 Introduction to Object Oriented Programming

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

ITI Introduction to Computing II

Chapter 6 Introduction to Defining Classes

CS 251 Intermediate Programming Inheritance

Polymorphism. Arizona State University 1

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

ITI Introduction to Computing II

core Advanced Object-Oriented Programming in Java

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

CST242 Object-Oriented Programming Page 1

Advanced Object-Oriented. Oriented Programming in Java. Agenda. Overloading (Continued)

CSE 143 Lecture 20. Circle

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

ITI Introduction to Computing II

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

Inheritance and Interfaces

CS 11 java track: lecture 3

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

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Motivations. Chapter 13 Abstract Classes and Interfaces

Inheritance, polymorphism, interfaces

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

Polymorphism. Agenda

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

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

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

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Inheritance (Deitel chapter 9)

Inheritance and Polymorphism

25. Generic Programming

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

Inheritance, Polymorphism, and Interfaces

First IS-A Relationship: Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 9 - Object-Oriented Programming: Polymorphism

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

Chapter 13 Abstract Classes and Interfaces

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

Java Programming Lecture 7

Object-Oriented Concepts

CS200: Advanced OO in Java

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

Fundamentals of Object Oriented Programming

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CMSC 132: Object-Oriented Programming II. Inheritance

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

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

More on Inheritance. Interfaces & Abstract Classes

OVERRIDING. 7/11/2015 Budditha Hettige 82

Programming overview

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Chapter 5 Object-Oriented Programming

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

More Relationships Between Classes

1.00 Lecture 14. Exercise: Plants

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

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

CSC207 Week 3. Larry Zhang

Java Fundamentals (II)

Questions Answer Key Questions Answer Key Questions Answer Key

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Full file at Chapter 2 - Inheritance and Exception Handling

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Arrays Classes & Methods, Inheritance

Week 9. Abstract Classes

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

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

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

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Implements vs. Extends When Defining a Class

Exercise: Singleton 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance (Part 5) Odds and ends

The class Object. Lecture CS1122 Summer 2008

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Object Oriented Relationships

Inheritance. OOP: Inheritance 1

Object-Oriented Design

Transcription:

a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must be able to compute their area. All 2-D classes must respond to area() message. How do we ensure that? Define area method in class Shape Force the subclasses of Shape to respond area() message Java s Solutions Interfaces Idea To define only part of an implementation Can contain instance variables & methods that are fully implemented Leaving the subclasses to provide the details Any class with an abstract method must be declared abstract However you can declare a class abstract that has no abstract method. An abstract method has no implementation (known in C++ as a pure virtual function) If subclass overrides all abstract methods of the superclass, than it becomes a concrete class otherwise we have to declare it as abstract or we can not compile it Any subclass can override a concrete method inherited from the superclass and declare them abstract An abstract class cannot be instantiated However references to an abstract class can be declared Can point to the objects of concrete subclasses 1

Example of abstract class Shape.java /* This is an example of abstract class. Note that this class contains an abstract method with no definition. */ public abstract class Shape { public abstract void calculatearea(); Circle.java /* This class extends from abstract Shape class. Therefore to become concrete class it must provides the definition of calculateareamethod. */ public class Circle extends Shape { private int x, y; private int radius; public Circle() { x = 5; y = 5; radius = 10; // continue Circle.java // providing definition of abstract method public void calculatearea () { double area = 3.14 * (radius * radius); System.out.println( Area: + area); Test.java (Driver class) public class Test { public static void main (String args[ ]){ //can only create references of abstract class Shape s = null; // Shape s1 = new Shape(); //cannot instantiate abstract class //end of class //can point to the concrete subclass s = new Circle(); s.calculatearea(); Compile & Execute Interfaces 2

Interfaces A special java type which Defines a set of method prototypes, but does not provide the implementation for the prototypes Essentially all the methods inside an interface are Abstract Methods or we can say that an interface is like a pure abstract class (Zero Implementation) Can also define static final constants Interfaces Definition Example Syntax (appears like abstract class): All methods are abstract and public by default All constants are static and final by default public interface Speaker { public void speak( ); Implementing (Using) Interfaces Interface - Example Classes Implement interfaces Implementing an interface is like signing a contract. A class that implements an interface will have to provide the definition of all the methods that are present inside an interface <<Interface>> Speaker If the class does not provide definitions of all methods, the class would not compile. We have to declare it as an abstract class in order to get it compiled. Politician Coach Lecturer Responds to relationship Relationship between a class and interface Implementing Interfaces Example class Politician implements Speaker { public void { System.out.println( Talk politics ); class Coach implements Speaker { public void { System.out.println( Sports Talks ); class Lecturer implements Speaker { public void { System.out.println( Web Desing and Development Talks ); Defining Interface public interface Printable { public void print(); Implementing Interface Example Code public class Student implements Printable{ private String name; private String address; public String tostring () { return "name:"+name +" address:"+address; // NOT providing implementation of print method 3

Compile Example Code (cont.) Implementing Interface (Modification) public class Student implements Printable{ private String name; private String address; public String tostring () { return "name:"+name +" address:"+address; public void print() { System.out.println("Name:" +name+" address"+address); Compile More on Interfaces Interface imposes a design structure on any class that uses the interface Leaves the implementation details to the implementing class and hides that implementation from the client. A class can implement more than one interfaces. Java s way of multiple inheritance class Circle implements Drawable, Printable { //additional constants and abstract methods More on Interfaces (cont.) Classes inherit from classes (Single), interfaces inherit from interfaces (Can be multiple) and classes implement interfaces (Can be multiple) public interface Displayable extends Drawable, Printable { //additional constants and abstract methods Objects of interfaces cannot be instantiated. Speaker sp = new Speaker(); // not comaile Interface based Polymorphism However a reference of interface can be created to point to any of its implementation class (Interface based polymorphism). 4

Review again Interface Example <<Interface>> Speaker Example: Interface based Polymorphism /* Speaker interface is implemented by the Politician, Coach and Lecturer class. */ public class Test{ public static void main (String args[ ]) { Speaker sp = null; System.out.println("sp pointing to Politician"); sp = new Politician(); sp.; Politician Coach Lecturer System.out.println("sp pointing to Coach"); sp = new Coach(); sp.; System.out.println("sp pointing to Lecturer"); sp = new Lecturer(); sp.; Interface based Polymorphism Compile & Execute Interfaces vs. Abstract classes Fairly similar uses designed to group behavior, allow upcasting, exploit polymorphism Rules of thumb Choose abstract class if we have shared code and logical is a relationship Choose interface if only want to ensure design structure (method signatures) and/or it is not logical to use is a relationship. 5