Opps

  • 1,423 views
Uploaded on

I am not the author of this file but wanted to collect it for study purpose.

I am not the author of this file but wanted to collect it for study purpose.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,423
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Polymorphism Compile time polymorphism Run time polymorphism Method Hiding Virtual and Overridden Methods Operators overloading Functions/Methods overloading Inheritance Class/ Abstract Class Interface
  • 2. What is inheritance? Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say "A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. If class A inherits from class B, then B is called Base Class of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding Base Class are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the Base Class. However, subclasses are not limited to the state and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. In the literature you may also find other terms for "superclass" and "subclass". Superclasses are also called parent classes or base classes. Subclasses may also be called child classes or just derived classes.
  • 3. Inheritance Example Like a car, truck or motorcycles have certain common characteristics- they all have wheels, engines and brakes. Hence they all could be represented by a common class Vehicle which encompasses all those attributes and methods that are common to all types of vehicles. However they each have their own unique attributes; car has 4 wheels and is smaller is size to a truck; whereas a motorcycle has 2 wheels. Thus we see a parent-child type of relationship here where the Car, Truck or Motorcycle can inherit certain Characteristics from the parent Vehicle; at the same time having their own unique attributes. This forms the basis of inheritance; Vehicle is the Parent, Super or the Base class. Car, Truck and Motorcycle become the Child, Sub or the Derived class.
  • 4. Abstract Classes Classes can be declared as abstract. This is accomplished by putting the keyword abstract before the keyword class in the class definition. The abstract keyword enables you to create classes and class members solely for the purpose of inheritance public abstract class A { // Class members here. } 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. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example: public abstract class A { public abstract void DoWork(int i); }
  • 5.
    • 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.
    • Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain implementation.
    • 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 curly braces ({ }) following the signature. For example:
    • public abstract void MyMethod();
    • The implementation is provided by an overriding method override, 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.
    • Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
    • It is an error to use the abstract modifier on a static property.
    • An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
    • An abstract class must provide implementation for all interface members.
  • 6.
    • What is an Interface ?
    • An Interface is a reference type and it contains only abstract members. Interface's members can be Events , Methods , Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants , data fields , constructors, destructors and static members . All the member declarations inside interface are implicitly public.
    • Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface , all methods defined by that interface must appear in its source code before the class will successfully compile. some features of interface in C#.
    • An interface cannot inherit from a class.
    • An interface can inherit from multiple interfaces.
    • A class can inherit from multiple interfaces, but only one class.
    • Interface members must be methods, properties, events, or indexers
    • All interface members must have public access (the default).
    • By convention, an interface name should begin with an uppercase I.
  • 7.
    • Limitations and Restrictions of Interface
    • The essential idea to remember is that an interface never contains any implementation. The following restrictions and imitations are natural consequences of this
    • You're not allowed any fields in an interface, not even static ones. A field is an implementation of an object attribute.
    • You're not allowed any constructors in an interface. A constructor contains the statements used to initialize the fields in an object, and an interface does not contain any fields!
    • You're not allowed a destructor in an interface. A destructor contains the statements used to destroy an object instance.
    • You cannot supply an access modifier. All methods in an interface are implicitly public.
    • You cannot nest any types (enums, structs, classes, interfaces, or delegates) inside an interface.
    • You're not allowed to inherit an interface from a struct or a class. Structs and classes contain implementation; if an interface were allowed to inherit from either, it would be inheriting some implementation
  • 8. What is Polymorphism in C# and Object Oriented Programming (OOPS) Languages? Object Oriented Programming (OOPS) Languages In object-oriented programming, polymorphism is a generic term that means 'many shapes'. (from the Greek meaning "having multiple forms"). Polymorphism is briefly described as "one interface, many implementations." polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form. Polymorphism in C# There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism . Compile time polymorphism is functions and operators overloading . Runtime time polymorphism is done using inheritance (Override) and virtual functions . Here are some ways how we implement polymorphism in Object Oriented programming languages
  • 9. Polymorphism Compile time polymorphism Run time polymorphism Method Hiding Virtual and Overridden Methods Operators overloading Functions/Methods overloading
  • 10. Function Overloading Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The functions which differ only in their return types cannot be overloaded . The compiler will select the right function depending on the type of parameters passed. Operator Overloading In polymorphism operators can also be overloaded ( Compile time polymorphism ). Operators can be overloaded in order to perform special functions with respect to the class. With the help of operator overloading standard operations such as + , - , * , etc can be applied on the objects of the class.
  • 11. Inherited Methods A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses     using System;     namespace Polymorphism     {         class A         {             public void Foo() { Console.WriteLine("A::Foo () "); }         }         class B : A {}         class Test         {             static void Main(string[] args)             {                 A a = new A();                 a.Foo();  // output --> "A::Foo()"                 B b = new B();                 b .Foo();  // output --> "A::Foo()"             }         }   }
  • 12. Casting     using System;     namespace Polymorphism     {         class A         {               public void Foo() { Console.WriteLine("A::Foo () "); }         }         class B : A         {               public void Foo() { Console.WriteLine("B::Foo () "); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> "A::Foo()"                 b.Foo();  // output --> "B::Foo()"                 a = new B();                 a.Foo();  // output --> "A::Foo()"             }} }
  • 13. Virtual and Overridden Methods Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.     using System;     namespace Polymorphism     {         class A         {             public virtual void Foo() { Console.WriteLine("A::Foo () "); }         }         class B : A         {             public override void Foo() { Console.WriteLine("B::Foo () "); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> "A::Foo()"                 b.Foo();  // output --> "B::Foo()"                 a = new B();                 a.Foo();  // output --> "B::Foo()"             }         }      }
  • 14. Method Hiding Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding . Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:     using System;     namespace Polymorphism     {         class A         {             public void Foo() { Console.WriteLine("A::Foo () "); }         }         class B : A         {             public new void Foo() { Console.WriteLine("B::Foo () "); }         }         class Test         {             static void Main(string[] args)             {                 A a;                 B b;                 a = new A();                 b = new B();                 a.Foo();  // output --> "A::Foo()"                 b.Foo();  // output --> "B::Foo()"                 a = new B();                 a.Foo();  // output --> "A::Foo()"             }         }     }
  • 15. The difference between override and new in C# Case: override This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance: public class Base { public virtual void SomeMethod() { } } public class Derived : Base { public override void SomeMethod() { } } ... Base b = new Derived(); b.SomeMethod(); will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.
  • 16. Case:New KeyWord if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this: public class Base { public virtual void SomeOtherMethod() { } } public class Derived : Base { public new void SomeOtherMethod() { } } ... Base b = new Derived(); Derived d = new Derived(); b.SomeOtherMethod(); d.SomeOtherMethod(); Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method. If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).