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

Similar documents
Chapter 14 Abstract Classes and Interfaces

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

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

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

More on Inheritance. Interfaces & Abstract Classes

What is Inheritance?

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

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

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

Java Object Oriented Design. CSC207 Fall 2014

We are on the GUI fast track path

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

Chapter 13 Abstract Classes and Interfaces

Motivations. Chapter 13 Abstract Classes and Interfaces

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Inheritance -- Introduction

Inheritance and Polymorphism

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

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

Inheritance and Interfaces

Polymorphism. Arizona State University 1

CS111: PROGRAMMING LANGUAGE II

CS Programming I: Inheritance

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

C++ Important Questions with Answers

Inheritance, polymorphism, interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Chapter 13 Abstract Classes and Interfaces

Interfaces CSC 123 Fall 2018 Howard Rosenthal

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

ECE 122. Engineering Problem Solving with Java

Chapter 9 Abstract Classes and Interfaces

CS-202 Introduction to Object Oriented Programming

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

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

More Relationships Between Classes

C09: Interface and Abstract Class and Method

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

First IS-A Relationship: Inheritance

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

CS 251 Intermediate Programming Inheritance

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Inheritance and Polymorphism

Chapter 13 Abstract Classes and Interfaces

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

Inheritance (Outsource: )

Chapter 5 Object-Oriented Programming

25. Generic Programming

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

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

VIRTUAL FUNCTIONS Chapter 10

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

type conversion polymorphism (intro only) Class class

Inheritance (IS A Relationship)

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Transitivity

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

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

Polymorphism. return a.doublevalue() + b.doublevalue();

Chapter 5. Inheritance

Declarations and Access Control SCJP tips

Chapter 6 Introduction to Defining Classes

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

CS111: PROGRAMMING LANGUAGE II

Lecture 2: Java & Javadoc

Distributed Systems Recitation 1. Tamim Jabban

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

Object-Oriented Programming More Inheritance

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

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Polymorphism. Agenda

CS1150 Principles of Computer Science Objects and Classes

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

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

Inheritance & Polymorphism

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

Introduction to Inheritance

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

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

Object Orientated Programming Details COMP360

Programming Exercise 14: Inheritance and Polymorphism

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

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

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Arrays Classes & Methods, Inheritance

Introduction to Programming

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java Fundamentals (II)

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

Transcription:

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

Interface & Implements An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface cannot contain instance variable and solid methods. An interface specifies what methods will be implemented by classes that implements an interface. This is another way to design polymorphic methods. 2

Interface All classes share a single root, the Object class, but there is no single root for interfaces. Like a class, an interface also defines a type. A variable of an interface type can reference any instance of the class that implements the interface. If a class extends an interface, this interface plays the same role as a superclass. You can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa. 3

Interface Interface1_2 Interface2_2 Interface1_1 Interface1 Interface2_1 Object Class1 Class2 Suppose that c is an instance of Class2. c is also an instance of Object, Class1, Interface1, Interface1_1, Interface1_2, Interface2_1, and Interface2_2. 4

Interface declaration Declaration: public interface InterfaceName { /* constant declaration */ /* method declarations (without implementations.) */ Constant declaration public, static and final Method abstract, public 5

Interface declaration In an interface, by default all data fields are constant variables and defined as public final static and all methods are public abstract. For this reason, these modifiers can be omitted, as shown below: public interface T1 { public static final int K = 1; Equivalent public interface T1 { int K = 1; public abstract void p(); void p(); A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K). 6

Ex: Interface declaration (constant variables) public interface Conversionfactors { double intomm = 25.3; double ountogram = 28.34952; double poundtogram = 453.5924 ; Save as ConversionFactors.java 7

Ex: Interface declaration (Abstract Methods) public interface Conversions { public double InToMm(); public double OunceToGram(); public double PoundToGram(); Save as Conversions.java 8

Ex: Interface declaration (variables and Methods) public interface MyInterface { public final int aconstant = 32; public final double pi = 3.14159; public void methoda(int x); double methodb(); 9

Implementing an Interface A class may extends one parent (superclass), but it can implement none or more interfaces A class that implements an interface: Can access directly all the variables declared in the interface Have to redefined all the methods declared in the interface Declaration: class SomeClass extends Parent implements SomeInterface { ordinary class definition body 10

The body of the class definition is the same as always. If any of the abstract methods (in the interface) are not defined in the class that implements the interface, the class will become an abstract class 11

Example 1 public interface Speakable { public String speak(); public class Animal { protected String kind; public Animal() { ; public String tostring(){ return "I am a " + kind + " and I go " + ((Speakable)this).speak(); 12

Example 1 public class Cat extends Animal implements Speakable { public Cat() { kind ="cat"; public String speak() { return "meow"; public class Cow extends Animal implements Speakable { public Cow() { kind ="cow"; public String speak(){ return "moo"; 13

Interface vs Abstract In many ways, an interface is similar to an abstract class, but an abstract class can contain variables and concrete methods as well as constants and abstract methods. In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods. 14

Interface vs Abstract Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods 15