Abstract Classes and Interfaces

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Object Oriented Programming using Java - Polymorphism
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Java™ How to Program, 9/e Presented by: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
InterfacestMyn1 Interfaces Sometimes it is helpful to define what a class must do but not how it will do it. We have already seen an example of this: the.
Modern Programming Tools And Techniques-I
More About Java and Java How to Program By Deitel & Deitel.
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Object-Oriented Programming: Polymorphism
Polymorphism, Interfaces & Operator Overloading
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Interfaces.
Inheritance and Polymorphism
Object-Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Chapter 13 Abstract Classes and Interfaces
Lecture 23 Polymorphism Richard Gesick.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
Interface.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Abstract Classes AKEEL AHMED.
Interfaces.
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Advanced Java Programming
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Java Inheritance.
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming in java
CIS 199 Final Review.
Object-Oriented Programming: Polymorphism
Final and Abstract Classes
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

Abstract Classes and Interfaces Lecture 7 Abstract Classes and Interfaces CSE 1322 4/26/2018

Meet the limitations of Inheritance Basic concepts of Abstraction MOTIVATION Meet the limitations of Inheritance Basic concepts of Abstraction Learn how the abstraction is implemented in OOP. Abstract Class vs Interface Advantages and Disadvantages of each other 4/26/2018

ABSTRACT vs CONCRETE Objects 4/26/2018

To distinguish between abstract and concrete classes. OBJECTIVES To distinguish between abstract and concrete classes. To declare abstract methods to create abstract classes. To define interfaces To implement interfaces 4/26/2018

Abstract Classes and Methods Abstract classes, or abstract base classes cannot be used to instantiate objects. Abstract base classes are too general to create real objects—they specify only what is common among derived classes. Classes that can be used to instantiate objects are called concrete classes. Concrete classes provide the specifics that make it reasonable to instantiate objects. 4/26/2018

Abstract Classes and Methods An abstract class normally contains one or more abstract methods, which have the keyword abstract in their declaration. A class that contains abstract methods must be declared as an abstract class even if it contains concrete (non- abstract) methods. Abstract methods do not provide implementations. Constructors and static methods cannot be declared abstract. 4/26/2018

Abstract Class C# public abstract class Pet { private string myName; public Pet(string name) myName = name; } public string getName() return myName; public abstract string speak(); public class Cat : Pet { public Cat(string name): base(name) { } public override string speak() return "meow"; } 4/26/2018

Abstract Class Java public abstract class Pet { private String myName; public Pet(String name) myName = name; } public string getName() return myName; public abstract String speak(); public class Cat extends Pet { public Cat(String name): super(name) { } public String speak() return "meow"; } 4/26/2018

C# Abstract Properties abstract property declarations have the form: public abstract ReturnType MyProperty { get; set; } An abstract property may omit implementations for the get accessor, the set accessor or both. Concrete derived classes must provide implementations for every accessor declared in the abstract property. 4/26/2018

Abstract Classes and Methods 4/26/2018

Abstract Classes and Methods 4/26/2018

Abstract Classes and Methods Attempting to instantiate an object of an abstract class is a compilation error. Failure to implement a base class’s abstract methods and properties in a derived class is a compilation error unless the derived class is also declared abstract 4/26/2018

Abstract Classes and Methods We can use abstract base classes to declare variables that can hold references to objects of any concrete classes derived from those abstract classes. You can use such variables to manipulate derived-class objects polymorphically and to invoke static methods declared in those abstract base classes. 4/26/2018

Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables Assigning a base-class reference to a base-class variable is straightforward. Assigning a derived-class reference to a derived-class variable is straightforward. Assigning a derived-class reference to a base-class variable is safe, because the derived-class object is an object of its base class. However, this reference can be used to refer only to base-class members. Attempting to assign a base-class reference to a derived-class variable is a compilation error. To avoid this error, the base-class reference must be cast to a derived-class type explicitly. 4/26/2018

C# sealed Methods and Classes A method declared sealed in a base class cannot be overridden in a derived class. Methods that are declared private are implicitly sealed. Methods that are declared static also are implicitly sealed, because static methods cannot be overridden either. A derived-class method declared both override and sealed can override a base-class method, but cannot be overridden in classes further down the inheritance hierarchy. Calls to sealed methods are resolved at compile time—this is known as static binding. 4/26/2018

Java final Methods and Classes A method declared final in a base class cannot be overridden in a derived class. Methods that are declared private are implicitly final. Methods that are declared static also are implicitly final, because static methods cannot be overridden either. A derived-class method can override a base class method and be declared final, but cannot be overridden in classes further down the inheritance hierarchy. Calls to final methods are resolved at compile time—this is known as static binding. 4/26/2018

Java final Methods and Classes A class that is declared final cannot be a base class (i.e., a class cannot extend a final class). All methods in a final class are implicitly final. Class String is a final class. This class cannot be extended, so applications that use Strings can rely on the functionality of String objects as specified in the java API . 4/26/2018

An Interface is 100% Abstract 4/26/2018

Using an Abstract Class and Interface(s) Together 4/26/2018

Creating and Using Interfaces Interfaces define and standardize the ways in which people and systems can interact with one another. An interface describes a set of methods that can be called on an object— to tell it, for example, to perform some task or return some piece of information. An interface declaration begins with the keyword interface. All interface members are implicitly declared both public and abstract. An interface typically specifies behavior that a class will implement. Interface members can be any of the following:  classes, constants(java only), abstract methods,  other interfaces An interface can extend one or more other interfaces to create a more elaborate interface that other classes can implement. 4/26/2018

Creating and Using Interfaces An interface is typically used when unrelated classes need to share common methods so that they can be processed polymorphically You can create an interface that describes the desired functionality, then implement this interface in any classes requiring that functionality. An interface often is used in place of an abstract class when there is no default implementation to inherit—that is, no fields and no default method implementations. Like abstract classes, interfaces are typically public types, so they are normally declared in files by themselves with the same name as the interface 4/26/2018

Finer Points of Interfaces Java An interface's fields are public, static, and final . These keywords can be specified or omitted. When you define a field in an interface, you must assign a value to the field.  All methods within an interface must be abstract, so the method definition must consist of only a method header and a semicolon. The abstract keyword also can be omitted from the method definition. 4/26/2018

Finer Points of Interfaces C# A C# interface can not have fields. All methods within an interface are implicitly public and abstract, so the method definition must consist of only a method header and a semicolon. The public and abstract keywords must be omitted from the method definition. 4/26/2018

Inheriting from an Interface To inherit from an interface, a class declares that it implements the interface in the class definition, A class can implement 0, 1, or more interfaces. When a class implements an interface, the class must provide an implementation for each method in the interface. 4/26/2018

Multiple Interfaces Java and C# do not allow derived classes to inherit from more than one base class, but do allow a class to implement any number of interfaces. To implement more than one interface, use a comma-separated list of interface names after the colon (:) or implements in the class declaration. When a class inherits from a base class and implements one or more interfaces, the class declaration must list the base-class name before any interface names. 4/26/2018

Inheriting from an interface--java using the following syntax:   accessModifier class ClassName extends SuperclassName implements Interface1, Interface2, … The extends clause is optional. 4/26/2018

Inheriting from an interface—C# using the following syntax:   accessModifier class ClassName :baseClassName,Interface1, Interface2, … The inheritance from the base class (:) is optional. 4/26/2018

Common Errors In C#, it is a compilation error to declare an interface member public or abstract explicitly, because they are redundant in interface-member declarations. It is also a compilation error to specify any implementation details, such as concrete method declarations, in an interface. 4/26/2018

C# Creating and Using Interfaces To use an interface, in C#, a class must specify that it implements the interface by listing the interface after the colon (:) in the class declaration. public interface IMoveable { . . .} public class Player: IMoveable A concrete class implementing an interface must declare each member of the interface with the signature specified in the interface declaration. A class that implements an interface but does not implement all its members is an abstract class—it must be declared abstract and must contain an abstract declaration for each unimplemented member of the interface. 4/26/2018

Java Creating and Using Interfaces To use an interface, in java, a class must specify that it implements the interface by listing the interface the word implements in the class declaration. public interface Moveable { . . .} public class Player implements Moveable A concrete class implementing an interface must declare each member of the interface with the signature specified in the interface declaration. A class that implements an interface but does not implement all its members is an abstract class—it must be declared abstract and must contain an abstract declaration for each unimplemented member of the interface. 4/26/2018

Implementing an interface C# public interface IMoveable { void move (); void reverse (); } public class Player: IMoveable { . . . public void move () posX += 1; posY += 1; } public void reverse () posX -= 1; posY -= 1; 4/26/2018

Implementing an interface Java public interface Moveable { public abstract avoid move (); //public and abstract allowed //but not required void reverse (); } public class Player implements Moveable { . . . public void move () posX += 1; posY += 1; } public void reverse () posX -= 1; posY -= 1; 4/26/2018

Developing an IPayable Hierarchy To build an application that can determine payments for employees and invoices alike, we first create an interface named IPayable. Interface IPayable contains method GetPaymentAmount that returns a double amount to be paid for an object of any class that implements the interface. 4/26/2018

UML with Interfaces The UML class diagram shows the interface and class hierarchy used in an accounts-payable application. 4/26/2018

Creating and Using Interfaces The UML distinguishes an interface from a class by placing the word “interface” in guillemets (« and ») above the interface name. The UML expresses the relationship between a class and an interface through a realization. 4/26/2018

Good Programming Practices In C#, by convention, the name of an interface begins with "I". This helps distinguish interfaces from classes, improving code readability. When declaring a method in an interface, choose a name that describes the method’s purpose in a general manner, because the method may be implemented by a broad range of unrelated classes. 4/26/2018

IPayable 4/26/2018

Interfaces and Inheritance Inheritance and interfaces are similar in their implementation of the is-a relationship. An object of a class that implements an interface may be thought of as an object of that interface type. The is-a relationship that exists between base classes and derived classes, and between interfaces and the classes that implement them, holds when passing an object to a method. 4/26/2018

Common Interfaces of the .NET Framework Class Library 4/26/2018

Common Interfaces in java.util Description Collection<E> The root interface in the collection hierarchy. Comparator<T> A comparison function, which imposes a total ordering on some collection of objects. Enumeration<E> An object that implements the Enumeration interface generates a series of elements, one at a time. EventListener A tagging interface that all event listener interfaces must extend. Iterator<E> An iterator over a collection. List<E> An ordered collection (also known as a sequence). ListIterator<E> An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. 4/26/2018

Practice a Program with an Abstract Base Class and Some Interfaces Convert a Base Class into an Interface 4/26/2018