1 Interfaces and Abstract Classes Chapter 13. 22 Objectives You will be able to: Write Interface definitions and class definitions that implement them.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Interfaces.
1 Derived Classes and Inheritance Chapter Objectives You will be able to: Create derived classes in C#. Understand polymorphism. Write polymorphic.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
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,
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
One of the most prevalent powerful programming languages.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
Abstract Classes and Interfaces 8. Building Applications Using C#/ Session 8 © Aptech Ltd. Objectives  Define and describe abstract classes  Explain.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Variations on Inheritance Object-Oriented Programming Spring
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
C# for C++ Programmers 1.
Creating Your Own Classes
Static data members Constructors and Destructors
Ch 10- Advanced Object-Oriented Programming Features
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Polymorphism.
Review: Two Programming Paradigms
University of Central Florida COP 3330 Object Oriented Programming
Chapter 5 Hierarchies IS-A associations superclasses subclasses
OOP’S Concepts in C#.Net
Conditional Statements
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 14 Abstract Classes and Interfaces
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 Interfaces and Abstract Classes Chapter 13

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.

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.

44 Interfaces Generally your best tool for separation of interface and implementation. The interface is public. Implementation is private. Users see only the interface.

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.

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 Getting Started Create a new C# Windows Console Application project. Interface_Example

8 Adding an Interface Add interface IShape to the project. Project > Add New Item

9 Interface IShape interface IShape { void Draw(); double Area(); }

10 Class Square Add new class Square Project > Add Class

11 Class Square

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 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 Output

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 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 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 Program in Action End of Section Why can we do this?

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 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 Abstract Classes Add a new class to the project: Default_Shape

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 Class Default_Shape Continued public String Name() { return name; } A method that will be common to all derived classes.

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 Square public void Draw() { Console.WriteLine("Square Draw() called." + " ID = " + id + " Size = " + Size); }

26 Add Class Circle

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 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 Output

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 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 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 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 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 But now we have a problem The Draw method is associated with the Interface, IShape, rather than the Class, Square.

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 Program Works the Same

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 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