OOPS Concept mainly covers Inheritance, Abstraction, Polymorphism.
Inheritance is the idea that one class, called a subclass, can be based on another class, called a base class. Inheritance provides a mechanism for creating hierarchies of objects.a parent class can inherit its behavior and state to children classes. This concept was developed to manage generalization and specialization in OOP and is represented by a is-a relationship.
The concept of generalization in OOP means that an object encapsulates common state an behavior for a category of objects. The general object in this sample is the geometric shape. Most geometric shapes have area, perimeter, and color. The concept of specialization in OOP means that an object can inherit the common state and behavior of a generic object; however, each object needs to define its own special and particular state an behavior. Thus inheritance provides a mechanism for class level re-usability.
When using Inheritance we use the Access Keywords
base -> Access the members of the base class.
this -> Refer to the current object for which a method is called.
NOTE
# A static member cannot be marked as override, virtual, or abstract. So following is an error:
public static virtual void GetID()
# You can't call static methods of base class from derived class using base keyword.
#Virtual or abstract members cannot be private.
Inheritance makes code elegant and less repetitive
Sealed class
A sealed class is a class that does not allow inheritance. Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.
Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use". An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.
Polymorphism allows objects to be represented in multiple forms. Even though classes are derived or inherited from the same parent class, each derived class will have its own behavior. Polymorphism is a concept linked to inheritance and assures that derived classes have the same functions even though each derived class performs different operations.
Virtual keyword
The virtual keyword allows polymorphism too. A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.
Override keyword
Overriding is the action of modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.
Difference between Interface and Abstract Class
* Interfaces are closely related to abstract classes that have all members abstract.
* For an abstract class, at least one method of the class must be an abstract method that means it may have concrete methods.
* For an interface, all the methods must be abstract
* Class that implements an interface much provide concrete implementation of all the methods definition in an interface or else must be declare an abstract class
* In C#, multiple inheritance is possible only through implementation of multiple interfaces. Abstract class can only be derived once.
* An interface defines a contract and can only contains four entities viz methods, properties, events and indexes. An interface thus cannot contain constants, fields, operators, constructors, destructors, static constructors, or types.
* Also an interface cannot contain static members of any kind. The modifiers abstract, public, protected, internal, private, virtual, override is disallowed, as they make no sense in this context.
* Class members that implement the interface members must be publicly accessible.
No comments:
Post a Comment