7. Oops Concepts
Abstract class can simply defined as incomplete class. It contains one or more incomplete methods called abstract methods. It leaves the implementation of these methods to derived or sub classes. Since abstract classes are incomplete, they can not be instantiated.
They must be sub-classed in order to use their functionality. So the abstract class can not be sealed. We can declare a reference of the type of abstract class and it can point to the object of the class that has inherited the abstract class.
We can add more functionality in abstract class without destroying child classes that were using old class.
Abstract class class_name
// Constructor, constructor overloading and static constructor definition
// Abstract method declaration;
// Variable declaration;
// Ordinary method declaration with their definition;
// Static and virtual method declaration with their definition;
Few things about abstract class
Abstract class having declaration for abstract method.
You should define the abstract method using override keyword.
Abstract class support ordinary constructor, static constructor and constructor overloading.
Except private, all the access modifiers are allowed.
Ordinary methods will be having the definition part.
To declare the variables.
Static and virtual methods also to allow.
You can declare the interface inside the abstract class.
You should inherit the abstract class in derived class.
You can’t inherit more than one abstract class in derived class.
You can’t create the object for the abstract class, but you can create the reference for the abstract class.
Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited.
The main features of a static class are:
· They only contain static members.
· They cannot be instantiated.
· They are sealed.
· They cannot contain Instance Constructors
Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state
It is possible to write a static no-parameter constructor for a class. One reason for writing a static constructor would be if your class has some static fields or properties that need to be initialized from an external source before the class is first used. It will be called automatically before the first instance is created.
Use of Static constructors:
Static constructor is used to initialize static data.
To perform particular action that needs performed once only
Another use of static constructor is, when the class is using a log file and the constructor is used to write entries to this file.
A class can have private constructor also. It prevents the class from being instantiated by callers.
There are some cases where the private constructors can be useful:
Class containing only static utility methods
To implement singleton class.
It means “Ability to take more than one form”. An operation may exhibit different behavior and different instances. Behavior is depends upon the operation.
Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name.
1. Compile Time Polymorphism
2. Runtime Polymorphism
Compile Time Polymorphism:
Compile time polymorphism is functions and operators overloading.
Runtime time polymorphism is done using inheritance and virtual functions.
Two or more methods within the same class can share the same name, as long as their parameter declarations are different.
number of parameters
type of parameters
Mainly the overriding is to achieve Runtime Polymorphism. It allows a subclass to re-define a method it inherits from its super class
·If the super class method is public, the overriding method must be public
·If the super class method is protected, the overriding method may be protected or public
·If the super class method is package, the overriding method may be package, protected, or public
·If the super class methods is private, it is not inherited and overriding is not an issue
Overloading Vs. Overriding:
Overloading is nothing but static binding.
Overriding is dynamic binding which will be resolved at run-time.
Overloading deals with multiple methods in the same class with the same name but different signatures.
Overriding deals with two methods, one in a parent class and one in a child class, which have the same signature.
Overloading lets you define a similar operation in different ways for different data.
Overriding lets you define a similar operation in different ways for different object types.
Dynamic Binding/Late Binding:
It means that the code associated with a given procedure Call at Runtime.
It means that the code associated with a given procedure call during compile time itself.
A virtual function is a member function of the base class and which is redefined by the derived class. When a derived class inherits the class containing the virtual function, it has ability to redefine the virtual functions. A virtual function has a different functionality in the derived class according to the requirement. The virtual function within the base class provides the form of the interface to the function. Virtual function implements the philosophy of one interface and multiple methods (polymorphism).
The virtual functions are resolved at the run time. This is called dynamic binding. The functions which are not virtual are resolved at compile time which is called static binding. A virtual function is created using the keyword virtual which precedes the name of the function.
Comparison of interface with class (interface vs. class):
- 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.