Download presentation
Presentation is loading. Please wait.
Published byBennett Eaton Modified over 9 years ago
1
1 Interfaces and Abstract Classes Chapter 13
2
22 Objectives You will be able to: Write Interface definitions and class definitions that implement them. Write abstract class definitions and derived classes that inherit from them. Know what it means to designate classes and methods as “sealed”. Use explicit interface specification.
3
33 Interfaces An interface just declares methods, which must be provided by any class that implements the interface. Says nothing about implementation. A virtual method must have a body. An method declared in an interface does not.
4
44 Interfaces Generally your best tool for separation of interface and implementation. The interface is public. Implementation is private. Users see only the interface.
5
55 interface IShape { void Draw(); double Area(); } How to Define an Interface Methods declared by the interface No public, protected, or private specification. Microsoft recommends this naming convention.
6
66 Restrictions An interface never includes implementation. Consequently: You cannot instantiate an instance. No fields No constructors (or destructors) No access modifiers (public, private) methods automatically public No type definitions inside interface definition. enums, structs, classes, etc. Cannot inherit from a class or a struct. Classes and structs must have implementations.
7
7 Getting Started Create a new C# Windows Console Application project. Interface_Example
8
8 Adding an Interface Add interface IShape to the project. Project > Add New Item
9
9 Interface IShape interface IShape { void Draw(); double Area(); }
10
10 Class Square Add new class Square Project > Add Class
11
11 Class Square
12
12 Inherit an interface just like a base class class Square : IShape { private double Size; // Length of each side public Square (double size) { Size = size; } public double Area() { return Size * Size; } public void Draw() { Console.WriteLine ("Square Draw method called"); } Implementing an Interface Provide constructor Provide an implemenation of each method defined by the interface. Provide fields as needed.
13
13 Example: Using a Class that Implements an Interface class Program { static void Main(string[] args) { Square sq = new Square(10); double area = sq.Area(); Console.WriteLine ("Area = " + area); sq.Draw(); Console.ReadLine(); } }
14
14 Output
15
15 Declaring an Interface You can declare a varible as an interface. This compiles: class Class1 { static void Main(string[] args) { IShape sq;... } What does this mean?
16
16 Interfaces But an interface cannot be instantiated class Class1 { static void Main(string[] args) { IShape sq; sq = new IShape(); } } This gets a compile error.
17
17 Interfaces This compiles and runs: class Program { static void Main(string[] args) { IShape sq = new Square(10); double area = sq.Area(); Console.WriteLine("Area = " + area); sq.Draw(); Console.ReadLine(); }
18
18 Program in Action End of Section Why can we do this?
19
19 Abstract Classes Often you will have several different classes that implement a given interface: e.g. Circle, Square, Triangle,... The classes may have some methods that are identical Examples: String Name(); int ID(); Replicating functionality in multiple classes is a bad idea.
20
20 Abstract Classes Solution is to define an abstract class. Provides implementation for methods that are common to derived classes. Like an interface, cannot be instantiated. Exists only to be inherited. Derived classes inherit from the abstract class. Implement methods that are different for each kind of derived class.
21
21 Abstract Classes Add a new class to the project: Default_Shape
22
22 An Abstract Class abstract class Default_Shape { protected String name; static int next_id = 1; protected int id; protected Default_Shape(String name_) { name = name_; id = next_id++; } public int ID() { return id; } Fields that will be common to all derived classes. Constructor for the abstract class. A method that will be common to all derived classes.
23
23 Class Default_Shape Continued public String Name() { return name; } A method that will be common to all derived classes.
24
24 Some Derived Classes class Square : Default_Shape, IShape { private double Size; // Length of each side public Square(double size, String name_) : base(name_) { Size = size; } public double Area() { return Size * Size; } Inherits from the abstract class Default_Shape AND implements the IShape interface Methods with implementations unique to this derived class
25
25 Square public void Draw() { Console.WriteLine("Square Draw() called." + " ID = " + id + " Size = " + Size); }
26
26 Add Class Circle
27
27 Class Circle class Circle : Default_Shape, IShape // Same bases { private double radius; // Unique field public Circle (double radius_, String name_) : base (name_) { radius = radius_; } public double Area() { return Math.PI*radius*radius; } public void Draw() { Console.WriteLine ("Circle Draw() called." + " ID = " + id.ToString() + " radius = " + radius.ToString() ); }
28
28 Using the Derived Classes Square sq1;// Declarations Circle c1; IShape ishape1; IShape ishape2; sq1 = new Square(1, "sq1");// Instantiation c1 = new Circle (1, "c1"); ishape1 = new Square(10, "ishape1"); ishape2 = new Circle(10, "ishape2"); sq1.Draw();// Method calls c1.Draw(); ishape1.Draw(); ishape2.Draw();
29
29 Output
30
30 Sealed Classes What if you don’t want anyone to derive classes from your class? Mark class as sealed. sealed class Square : Default_Shape, IShape {... } A program that atttempts to define a derived class based on Square will get a compile error. All structs are implicitly sealed.
31
31 Sealed Methods You can also mark a method as sealed. This prevents any derived class from overriding that method. Normally a method that overrides a virtual method in a base class is itself virtual. The method can be re-implemented again and again in successive derived functions. “sealed” puts an end to this.
32
32 Working with Multiple Interfaces Mulitple Inheritance Some languages, including C++, permit a derived class to inherit from multiple base classes. In C#, a class can have at most one base class BUT can implement any number of interfaces
33
33 Working with Multiple Interfaces Syntax: class Square : Default_Shape, IShape, IDisposable, IComparable {... } A class must implement all methods in all interfaces that it inherits. Base class Interfaces Base class, if present, must preceed any interfaces.
34
34 Explicit Interface Implementation Another way for a class to implement an interface method: Name the interface explicitly in the method declaration. void IShape.Draw() { Console.WriteLine("Square Draw() called." + " ID = " + id + " Size = " + Size); }
35
35 But now we have a problem The Draw method is associated with the Interface, IShape, rather than the Class, Square.
36
36 Solution static void Main(string[] args) { Square sq1;// Declarations Circle c1; IShape ishape1; IShape ishape2; sq1 = new Square(1, "sq1");// Instantiation c1 = new Circle(1, "c1"); ishape1 = new Square(10, "ishape1"); ishape2 = new Circle(10, "ishape2"); ((IShape) sq1).Draw();// Method calls c1.Draw(); ishape1.Draw(); ishape2.Draw(); Console.ReadLine(); }
37
37 Program Works the Same
38
38 Explicit Interface Implementation EIS solves the problem of the same method name being used by each of two Interfaces implemented by a class. The class can implement both methods, even though they have the same name. Typecast on method calls tells the compiler which one the caller wants.
39
39 Summary Inheritance is one of the key concepts of object oriented programming. A class can inherit A base class Any number of interfaces Functionality common to multiple classes can be factored out to an abstract base class that all inherit. Avoid replication. End of Presentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.