Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes and Interfaces

Similar presentations


Presentation on theme: "Abstract Classes and Interfaces"— Presentation transcript:

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

2 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

3 ABSTRACT vs CONCRETE Objects
4/26/2018

4 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

5 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

6 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

7 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

8 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

9 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

10 Abstract Classes and Methods
4/26/2018

11 Abstract Classes and Methods
4/26/2018

12 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

13 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

14 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

15 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

16 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

17 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

18 An Interface is 100% Abstract
4/26/2018

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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

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

35 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

36 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

37 IPayable 4/26/2018

38 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

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

40 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

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


Download ppt "Abstract Classes and Interfaces"

Similar presentations


Ads by Google