26
© 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein is subject to change without notice. C3: Protected C# 3.5 Targeted at: Entry Level Trainees Session 05: Inheritance and Interfaces

Csharp3.5 InheritenceandInterfaces Ver 1.0

Embed Size (px)

DESCRIPTION

Csharp

Citation preview

About the Author

C3: ProtectedC# 3.5

Targeted at: Entry Level TraineesSession 05: Inheritance and Interfaces 2007, Cognizant Technology Solutions. All Rights Reserved.The information contained herein is subject to change without notice.1Enter the course title.Enter the knowledge level that will be addressed through this course.2About the AuthorCreated By:N. Subramania Diwakaran. (186616)Credential Information:Technical Skills: .NET/C#/SQL serverExperience: 11 Years.

Version and Date:C# 3.5/PPT/1.0

Cognizant Certified Official Curriculum

2The Course Metadata screen displays data about the content. Metadata is nothing but data about data. This screen displays various information such as course owner, credential information, copyrights and so on.

First, enter the associate name or the vendor who created the training presentation and the associate id in the Created By row.

Second, briefly state credentials of the creator in the training area; or, why participants should listen to you. For example, if the training is on Java, the credential could be:Sun Certified Java Programmer5 yrs of experience in Java, EJB, Java Beans, JDBC etc.

Finally, enter the current version number and the corresponding date of the training presentation. For example, if this the first release then enter 1.0. and January 1, 2004 for Date.3Icons Used

QuestionsContacts

ReferenceTry it Out

Hands-on ExerciseCoding Standards

Test Your UnderstandingTools

A Welcome Break

C# Session 05: OverviewIntroduction:This session will explore how C# supports inheritance and interfaces .

OOP supports class inheritance and the implementation of interfaces as the ways of allowing new classes to be safely added to existing systems without requiring changes to existing code.444C# Session 05: ObjectivesObjectives:After completing this session, you will be able to:Identify InheritanceDefine Abstract Classes and InterfacesDescribe Polymorphism using Interfaces 555InheritanceInheritance enables you to create new classes that can reuse extend and modify the behaviors that are defined in other classes.Inheritance is a relationship between classes where one class is the parent class of another.Parent class is also called as Base classSuper class and Ancestor.666Inheritance (Contd.)The derived class is also know as subclass or child class.When a subclass inherits from a base class, then it is a is-a-kind-of relationship.For example, suppose you have a superclass named Vehicle with a subclass named Car. Car is a kind of vehicle.The subclass inherits all the super class attributes and extends them or adds others.C# does not support multiple inheritance.77788Inheritance (Contd.)

public class Vehicle {public Vehicle() {} public void start() { // do some logic}public void stop() { // do some logic}}public class Car : Vehicle { public Car() { } public void Run(){//Do something}} public class Program{ public static void Main() {Car ferrari = new Car();ferrari.start();ferrari.Run();ferrari.stop(); } } Child class instance can access the base class methods8Abstract Classes (Contd.) The abstract keyword enables you to create classes and class members solely for the purpose of inheritanceto define features of derived, non-abstract classes. An abstract class cannot be instantiated.The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.Abstract classes may also define abstract methods9public abstract class Vehicle{ public abstract void Run();} public class Aircraft:Vehicle{ public override void Run() { // logic to fly. }} Just definition. No bodyChild class provides the bodyAn abstract method is implicitly a virtual method.Abstract method declarations are only permitted in abstract classes.The implementation is provided by an overriding method, which is a member of a non-abstract class. It is an error to use the static or virtual modifiers in an abstract method declaration.

9Abstract Classes (Contd.)10public abstract class Talk{ public abstract void speak(); public virtual void goodbye() { Console.WriteLine("Talk class says goodbye!");} }public abstract class SayHello:Talk{ public override void speak(){Console.WriteLine(Hello from speak class);} public override void goodbye() { Console.WriteLine("Talk class says goodbye!");} }Use virtual method to provide default method implementationUse override keyword to change the implementation of the virtual methods in the sub class .InterfacesAn interface in C# is a pure abstract classInterfaces looks like a class, but has no implementation.It contain only definition of events, indexers, methods and/or properties.Interfaces inherited by classes and structs, which must provide an implementation for each interface member defined.11Interfaces (Contd.)All interfaces should be declared with the keyword interface.It is possible to implement any number of interfaces in a single derived class, but you should provide signatures to all methods.C# does not support multiple inheritance, but using interfaces it can do that.In interface all the methods are abstract.1213Interfaces (Contd.)Defining an Interface:

Using an Interface:

14Interfaces (Contd.)Multiple inheritance using interfaces:

Output:Method1 CalledMethod2 CalledPolymorphism using InterfacesInterface Polymorphism: Multiple classes may implement the same interface, and a single class may implement one or more interfaces. Interfaces are essentially definitions of how a class needs to respond. An interface describes the methods, properties, and events that a class needs to implement, and the type of parameters each member needs to receive and return. However, this leaves the specific implementation of these members up to the implementing class. 1516Polymorphism using Interfaces (Contd.)

17Abstract Class and InterfacesFeatureInterfaceAbstract classMultiple inheritanceA class may inherit several interfaces.A class may inherit only one abstract class.Default implementationAn interface cannot provide any code, just the signature.An abstract class can provide complete, default code and/or just the details that have to be overridden.ConstantsOnly Static final constants.Both instance and static constants are possible. 18Abstract Class and Interfaces (Contd.)FeatureInterfaceAbstract classCore VS PeripheralInterfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.An abstract class defines the core identity of a class and there it is used for objects of the same type.HomogeneityIf the various implementations only share method signatures, then it is better to use Interface. If the various implementations are of the same kind and use common behaviors or status then abstract class is better to use.19Abstract Class and Interfaces (Contd.)FeatureInterfaceAbstract classSpeedRequires more time to find the actual method in the corresponding classes.FastAdding functionalityIf you add a new method to an Interface, then you have to track down all the implementations of the interface and define implementation for the new method.If you add a new method to an abstract class, then you have the option of providing default implementation and therefore all the existing code might work properly.Abstract Classes (Contd.) An abstract method is implicitly a virtual method.Abstract method declarations are only permitted in abstract classes.The implementation is provided by an overriding method, which is a member of a non-abstract class. It is an error to use the static or virtual modifiers in an abstract method declaration.2021Abstract Classes (Contd.)

Output:Abstract method !.Non-abstract method !.22Allow time for questions from participants

2223Test Your Understanding

What is inheritance?Does C# support multiple inheritance?What is the advantage of using inheritance?What is an abstract class?How many abstract classes can a class inherit?How do you achieve multiple inheritance in C#?State the difference between abstract classes and interfaces.23Inheritance and Interfaces: SourceReference websites:www.msdn.microsoft.com24Disclaimer: Parts of the content of this course is based on the materials available from the Websites and books listed above. The materials that can be accessed from linked sites are not maintained by Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are the marks of the respective owner(s).

24Interface and Inheritance: SummaryHere is a brief recap of this session:Inheritance is a relationship between classes where one class is the parent class of another. C# does not support multiple inheritance.An abstract class can not be instantiated.Abstract methods have no implementation.Interfaces looks like a class, but has no implementation.In interface all the methods are abstract.2525This slide lists the summary of the various point covered in the Module. This slide also presents you the opportunity to clarify learners questions.

Dos:List the main points presented in the module.Restate Objective and Importance of the Module.If any scenario was used in the overview, conclude the scenario in the summary slide.

Donts:Try to keep the text count to 100 words

You have completed Session 05 of C# 3.5. 2007, Cognizant Technology Solutions. All Rights Reserved.The information contained herein is subject to change without notice.26Enter the course title.Enter the knowledge level that will be addressed through this course.