410 likes | 426 Views
Learn the limitations of inheritance and how abstraction is implemented in OOP through abstract classes and interfaces. Understand the advantages and disadvantages of abstract classes and interfaces, and distinguish between abstract and concrete classes.
E N D
Lecture 7 Abstract Classes and Interfaces CSE 1322
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
OBJECTIVES To distinguish between abstract and concrete classes. To declare abstract methods to create abstract classes. To define interfaces To implement interfaces
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.
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.
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"; } }
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"; } }
C# Abstract Properties abstract property declarations have the form: public abstract ReturnTypeMyProperty { 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.
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
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.
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. Summary of the Allowed Assignments BetweenBase-Class and Derived-Class Variables
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.
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.
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 .
Interface An Interface is 100% Abstract
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.
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
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.
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.
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.
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.
Inheriting from an interface--java using the following syntax: accessModifier class ClassName extends SuperclassName implements Interface1, Interface2, … • The extends clause is optional.
Inheriting from an interface—C# using the following syntax: accessModifier class ClassName :baseClassName,Interface1, Interface2, … • The inheritance from the base class (:) is optional.
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.
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.
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.
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; }
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; }
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.
UML with Interfaces The UML class diagram shows the interface and class hierarchy used in an accounts-payable application.
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.
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. Good Programming Practices
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. Interfaces and Inheritance
Practice a Program with an Abstract Base Class and Some Interfaces • Practice a Program with an Abstract Base Class and Some Interfaces • Convert a Base Class into an Interface