Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

Similar documents
Chapter 14 Abstract Classes and Interfaces

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

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

25. Generic Programming

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

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

Goals of the Lecture OO Programming Principles

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

Inheritance (continued) Inheritance

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Inheritance and Polymorphism

Chapter 10 Classes Continued. Fundamentals of Java

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

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

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

What is Inheritance?

CST242 Object-Oriented Programming Page 1

C++ Inheritance and Encapsulation

Inheritance and Polymorphism

Chapter 6 Introduction to Defining Classes

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

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

Chapter 5 Object-Oriented Programming

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

ITI Introduction to Computing II

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

OOP: Key Concepts 09/28/2001 1

ECE 3574: Dynamic Polymorphism using Inheritance

Programming II (CS300)

ITI Introduction to Computing II

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

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

Programming overview

CMSC 132: Object-Oriented Programming II

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

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

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

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

Inheritance. OOP: Inheritance 1

OBJECT ORIENTED PROGRAMMING

Inheritance and Interfaces

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

CMSC 132: Object-Oriented Programming II

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

OBJECT ORİENTATİON ENCAPSULATİON

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CH. 2 OBJECT-ORIENTED PROGRAMMING

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

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

Use the scantron sheet to enter the answer to questions (pages 1-6)

Inheritance and Polymorphism

Inheritance and Polymorphism

Inheritance, and Polymorphism.

ITI Introduction to Computing II

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

EXERCISES SOFTWARE DEVELOPMENT I. 09 Objects: Inheritance, Polymorphism and Dynamic Binding 2018W

Inheritance, Polymorphism, and Interfaces

Practice for Chapter 11

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

2. The object-oriented paradigm

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

CSE 143 Lecture 20. Circle

Inheritance, polymorphism, interfaces

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

1 Shyam sir JAVA Notes

CS111: PROGRAMMING LANGUAGE II

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Comments are almost like C++

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Programming II (CS300)

Object-Oriented Programming Concepts

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance Motivation

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

» Access elements of a container sequentially without exposing the underlying representation

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

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

C++ Important Questions with Answers

CS-202 Introduction to Object Oriented Programming

index.pdf January 21,

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

Java 8 Programming for OO Experienced Developers

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

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

Structured Programming

ITI Introduction to Computing II

Computer Science 210: Data Structures

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

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Java How to Program, 8/e

Polymorphism. Arizona State University 1

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Object Oriented Programming in C#

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

ITI Introduction to Computing II

Transcription:

Abstract Classes, and Enhancement of Polymorphism Abstract Classes Designing a hierarchy, it s current (and proper) practice placing in the super-classes all the common methods and data structures required by subclasses. Sometimes a super-class is aimed only at acting as a common model for the sub-classes, and no reason exists to actually instantiate it. In this case, it may be declared as abstract. public abstract class Shape {... Abstract classes CANNOT be instantiated

Abstract Classes You create an abstract class when you want to manipulate a set of classes through this common interface Abstract Classes Abstract classes can contain whatever an ordinary class can: instance and class variables, instance and class methods, with whatever modifiers Moreover, abstract classes can contain abstract methods An abstract method is given the signature only. An abstract method is not equipped with a body, i.e. no implementation is given for it. The implementation of the body of an abstract method is provided in sub-classes of the abstract class.

Abstract classes A method can be declared abstract iff it is contained in an abstract class. Abstract methods roughly describe within a super-class behaviors that are exhibited by sub-classes. Each subclass is in charge of providing the correct specific implementation for such behaviors public abstract class Shape { protected double x,y; public void whatplace() { System.out.println( My position: +x+, +y); public abstract double area(); class Circle extends Shape { protected double radius; public Circle(double r){ radius=r; public double area() { return(radius*radius*3.1415); Sub-classes of Shape inherit the concrete method whatplace() and variables x and y They MUST implement the method area() as well Abstract Classes class Square extends Shape{ protected double edge; public Square(double e){ edge=l; public double area() { return(edge*egde); Classes Circle and Square are sub-classes of Shape; Thus, they inherit variables x and y, and method whatplace() as well. Moreover, they implement method area() declared as abstract in class Shape

Abstract Classes... Shape[] shp=new Shape[2]; shp[0]=new Circle(1.0); shp[1]=new Square(2.0); for(int i=0; i< shp.length; i++) { System.out.println( Area: + shp[i].area()); shp[i].whatplace();... Class1Lev1 ClassLev0 ClassLev2 Here, a two-element array is created, and it is assigned two objects: one of type Circle and the other of type Square The methods whatplace() and area() are invoked on the array elements: the former is inherited from class Shape, where it has been implemented; the latter has been declared abstract The Diamond Problem Class2Lev1

In Java (differently by other OO languages such as C++) multiple inheritance among classes is not allowed. Whenever multiple inheritance is present, a single class may extend multiple super-classes. In Java a single class can extend just one single super-class. This feature makes the language easier to learn and to implement. One of the drawbacks of this approach is the following: it s not possible to specify that classes on separate sub-trees share some behaviors. E.g., the two classes Car and Factory may share the behavior of PollutingObject characterized by the methods emissionofpollutinggases (), etc. Java provides a solution to this issue by means of the introduction of interfaces An interface is a collection of method definitions with no implementation; no instance variable is present within an interface An interface can be associated to whatever class, in order to provide it with a behavior which is possibly not inherited by a super-class are not integral part of the ordinary class hierarchy.

An interface definition is carried out according to the same rules used for classes: it must be placed in a file named as the interface itself, and with the extension.java; once it gets compiled, it is contained in a.class file. The keyword interface is used to create a new interface package geometry; public interface Measurable { public static final double PI=3.1415; public abstract double area(); double perimeter(); package geometry; public interface Measurable { public static final double PI=3.1415; public abstract double area(); double perimeter(); may be placed in a package As for classes, interfaces must have a public or package protection level. Methods can be declared as public and abstract They cannot be neither protected nor private If no modifier is provided (as in perimeter()), a member assumes the same visibility as the class (in this case, public) The defined variable must be public, static, final In case mo modifier is specified, the same rules as for methods apply.

An interface can be defined to be an extension of another by using the keyword extends: public interface InterfaceB extends InterfaceB { Interface hierarchy, differently from the class hierarchy, has no root (neither explicit nor implicit; no counterpart of the java.lang.object exists). Multiple inheritance is used in the interface hierarchy public interface InterfaceX extends InterfaceA, InterfaceB { InterfaceX contains all the method/variable definitions and constants that are present both in InterfaceA and in InterfaceB : the keyword implements Because of the use of the keyword implements, a class is in charge of implementing all the methods defined in the interface. package geometry; public interface Measurable { public static final double PI=3.1415; public abstract double area(); double perimeter(); Sub-classes of a class that implements a given interface, inherit the methods in the implemented interface. class Square implements Measurable{ protected double edge; public Quadrato(double e){ edge=e; public double area() { return(edge*edge); public double perimeter() { return(edge*4);

A class can implement more than one interface. class Apple extends Fruit implements Peelable, Eatable, Sellable, If two implemented interfaces have the same method with the same signature, a single implementation for both has to be specified. If the two methods have the same name and different signatures, both of them have to be implemented. If the two methods have the same name, the same signature but different types are returned, an error occurs. It s possible to declare a variable whose type is an interface. Such a reference variable can be assigned object of classes that implement such an interface. Measurable mea=new Square(2.0) As mea is an object of type Measurable, it is possible to invoke over it the two methods perimeter() and area() can be used to group a number of constants to be imported into several different classes public interface Constants { double PI=3.1415, sqrttwo=1.4142,

: More Polymorphism is Added The fact that it s possible to declare a variable whose type is an interface, gives us much more flexibility in using polymorphic behaviors. A certain method a() in an interface X can be invoked over all the objects whose classes implements X, REGARDLESS of the placement of the classes in the class hierarchy. In this context, it is common to deal with arrays (or more complex data structures of this kind) with elements of type interface : What about Cast? What about cast among interfaces? What about cast among interfaces and classes? What about cast among interfaces and abstract classes Let s make some experiments!

Exercise Create the class Rodent and the two classes Mouse and Beaver, making possibly used of an abstract class Create class Cage, containing three object of type Rodent Cage must implement the interface java.util.enumeration (present in the core APIs) which is made of two methods: Object nextelement() Returns an element boolean hasmoreelements() Returns true if there are more elements to be enumerated Each element can be returned just once. Print the attributes of the three Rodent objects within an instance of Cage