Explain polymorphism and inheritance with example

polymorphism and inheritance in java ppt and polymorphism inheritance encapsulation abstraction c#
Dr.MasonHanks Profile Pic
Published Date:23-07-2017
Your Website URL(Optional)
Object Oriented Software Development 5. Interfaces, polymorphism and inheritance www.ThesisScientist.comTypes of interface  The word “interface” has more than one meaning in programming  User interface  The way in which the user interacts with the program  Programming interface  The way in which software components in an application interact with each other  We are looking at the latter here, and will look at the former later on www.ThesisScientist.comInterfaces example  InterfaceDemo project  Employee.cs  ITimeSheet.cs  DatabaseTimeSheet.cs  FileTimeSheet.cs www.ThesisScientist.comThe interface provided by a component  In previous examples we saw an Employee class which uses a TimeSheet object  Method parameter of type TimeSheet  Within its RecordOvertime method an Employee calls the AddEntry method of the TimeSheet object  Employee only needs to know that the TimeSheet class provides a method of that name, and the signature of the method www.ThesisScientist.comThe interface provided by a component  Employee does not need to know anything about how the AddEntry method works  Details are hidden behind the class interface  Details of how AddEntry works could be changed without affecting Employee  If a programmer wants to use the TimeSheet class in another class, only needs to know the class interface www.ThesisScientist.comDocumentation of interface  Good documentation makes it easy for programmer to understand the interface of a class and how to use it  XML comments help to make documentation useful www.ThesisScientist.comProgramming to an interface  Employee doesn’t care about the details of a TimeSheet, just the interface  Make this explicit by defining the interface as an item in its own right  Convention (in .NET anyway) is to name the interface with an I - ITimeSheet  Refer to the interface name, not the actual object type www.ThesisScientist.comDefining an interface  Interface definition method is empty – no code block  Use ITimeSheet as reference type www.ThesisScientist.comDefining properties in an interface  A property is defined in an interface by specifying the property type, and empty get and/or set declarations www.ThesisScientist.comRules for interfaces  Interfaces can contain:  Methods – declaration only  Properties – declaration only  Events  Interfaces can’t contain  Instance variables  Constants  Constructors  Static members  Access modifiers www.ThesisScientist.comImplementing an interface  Interface doesn’t actually do anything – need to create class which implements the interface  Must provide implementation for all members (methods/properties) defined in the interface  Class which implements ITimeSheet must (at least) provide a method called AddEntry with the signature defined in ITimeSheet  Must provide code block for method www.ThesisScientist.comImplementing an interface “implements ITimeSheet” class can have fields, constructors, etc method has code block www.ThesisScientist.comImplementing multiple interfaces  A class can implement more than one interface  Class must provide implementations of all the methods declared in all the interfaces it implements www.ThesisScientist.comInterface as a contract  A contract is an agreement  By implementing an interface, a class agrees that it will provide the defined members  This class can then be used by other classes with confidence because they know that it has agreed to provide the required members  Employee can use any class which implements ITimeSheet in the knowledge that it will have AddEntry method www.ThesisScientist.comReference and runtime types  Method definition in Employee reference type is ITimeSheet create DatabaseTimeSheet  Method call in Program object at runtime pass DatabaseTimeSheet object as can call AddEntry because we parameter at runtime – implements know runtime type must have ITimeSheet agreed to provide this method www.ThesisScientist.comInterface polymorphism  Polymorphism means “many forms”  Reference to an interface type can point, as the program runs, to any object of a type which implements that interface  Can pass any object which implements ITimeSheet as a parameter to RecordOvertime method in Employee  Can declare a variable of type ITimeSheet and set it to an object of any type which implements ITimeSheet www.ThesisScientist.comInterface polymorphism interface as reference type alternative implementations of a time sheet can pass any of these objects at runtime when parameter type is ITimeSheet interface can be implemented by class or struct www.ThesisScientist.comAdvantages of using interfaces  Application code written based on the functionality guaranteed by interface rather than functionality of a particular class  Reduces code dependencies  Code standardisation  Easier to write reusable code  Interfaces are used extensively in the .NET Framework library www.ThesisScientist.comInheritance  Often have classes which have some common features  e.g. different kinds of employee – SalariedEmployee, HourlyPaidEmployee  These are more specific versions of Employee  “is-a” relationship – SalariedEmployee is a type of Employee  They will share some common features  Each will require some specific features www.ThesisScientist.comInheritance example  InheritanceDemo project  Employee.cs  SalariedEmployee.cs  HourlyPaidEmployee.cs www.ThesisScientist.com

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.