1 / 16

Advanced OOP Concepts

Advanced OOP Concepts. 6.00 Apply Advanced Object-Oriented P rogramming C oncepts. Abstract Class & Interfaces. 6.04 Understand abstract classes and interfaces. (3%). What is an Abstract Class?.

dani
Download Presentation

Advanced OOP Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced OOP Concepts 6.00 Apply Advanced Object-Oriented Programming Concepts

  2. Abstract Class & Interfaces 6.04 Understand abstract classes and interfaces. (3%)

  3. What is an Abstract Class? • Use the abstractmodifier in a class declaration to indicate that a class is intended only to be a base class of other classes. • Abstract classes have the following features: • An abstract class cannot be instantiated. • An abstract class may contain abstract methods and accessors. • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited. • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

  4. Abstract Class Example • Create the class with the keyword abstract

  5. An abstract class cannot be instantiated • This means you will NOT construct an object from an abstract class. • Given abstract class A { public abstract void aMethod() {} } • You cannot A objectA = new A(); • Why? Because there is NO implementation. • An abstract class is basically a template shell

  6. An Abstract Class may Contain Abstract Methods and Accessors • Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation. • This means there is no code inside the method’s {}’s. • Abstract methods have the following features • An abstract method is implicitly a virtual method. • Abstract method declarations are only permitted in abstract classes. • Because an abstract method declaration provides no actual implementation, there is no method body; the method declaration simply ends with a semicolon and there are no braces ({ }) following the signature.public abstract void MyMethod();

  7. Non-abstract Class Derived from an Abstract Class • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors. • This means that the methods must be completed for each abstract method from the abstract base class.

  8. Check Your Understanding • Which of the following statements would create a C# interface? • public PlayerClass • interface public PlayerClass • public PlayerClass: interface • public interface PlayerClass

  9. Check Your Understanding • Which statement would tell the compiler that the Warrior class implements the PlayerClass? • public class Warrior : PlayerClass • public class PlayerClass : Warrior • public class Warrior implements PlayerClass • public class PlayerClassimplements Warrior

  10. Check Your Understanding • If the PepperGameclass implements the VeggieGameclass, which of the following uses that relationship in the Update method of the PepperGameclass? • public override void Update() • public void Update (VeggieGame game) • override public void Update (VeggieGame game) • public override void Update (VeggieGame game)

  11. What is an Interface? • Interfaces describe a group of related functionalities that can belong to any class or struct. • You define an interface by using the interface keyword. interface IEquatable<T> {bool Equals(T obj); }

  12. What is an Interface? • Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. • An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. • It cannot contain static members. • Interfaces members are automatically public, and they cannot include any access modifiers.

  13. Interfaces in Derived Classes • When a class or struct implements an interface, the class or struct provides an implementation for all of the members defined by the interface. • The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. • However, if a base class implements an interface, the derived class inherits that implementation. • The derived class is said to implement the interface implicitly.

  14. Interface vs. Base Class • Classes and structs implement interfaces in a manner similar to how classes inherit a base class or struct, with two exceptions: • A class or struct can implement more than one interface. • When a class or struct implements an interface, it receives only the method names and signatures, because the interface itself contains no implementations

  15. Abstract Class or Interface • Abstract Class • An abstract class is a class that cannot be instantiated, but must be inherited from. • An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes. • Interface • An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. • The implementation of an interface is left completely to the developer. http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx

  16. Abstract Class or Interface Abstract Class Interface If the functionality you are creating will be useful across a wide range of disparate objects If you are designing small, concise bits of functionality • If you anticipate creating multiple versions of your component • If you are designing large functional units • If you want to provide common, implemented functionality among all implementations of your component

More Related