articles

Home / DeveloperSection / Articles / OOP’s Concept

OOP’s Concept

Anonymous User 9019 09-Sep-2010
POLYMORPHISM

“Polymorphism means one name multiple forms”. Using this feature we can make more than one method using the same name. Polymorphism can be achieved in two ways method overloading and method overriding.

DIFFERENCE BETWEEN METHOD OVERLOADING AND METHOD OVERRIDDING
METHOD OVERLOADING:

Overloading means same method name but having different types (signatures) of parameters as well as no of parameters (arguments), may or may not the same return type written in same class itself. Methods can have different data types. Overloading is mainly used in operators.

EXAMPLE:

 Int add(int a,int b)       (different argument ,different parameters)       
Int add(float a,float b,float c)
 
2)class cclass
{
String print(int i);              (same method name but different parameters)
String print(int i,char c);
};
METHOD OVERRIDDING

Method overriding means same method name with same arguments and same return types associated in a class and its subclass. Overriding is mainly used in functions.

We can override only those methods which are declared with the keyword virtual in the base class.

EXAMPLE:
Class a
{
Virtual void hi (int i)
{
}
};
 
Class b: a
{
Public override void hi (int i)
{
}
}

 

NOTE: In the above program same method name i.e. hi, with same no of parameters, with same return types. We can override only those methods which are declared with the keyword virtual in the base class. We can use the keyword override

INHERITENCE

Inheritance is a principle of OOPS in which a class inherits the features from some of another class. A class inherits data members and the methods. A derived class can have some or all the features of the base class, apart from it also have some other features

Syntax

Class derivedClass: baseClass
{
}
ABSTARCT CLASSES

Abstract class cannot be instantiated i.e. we cannot create objects of an abstract class, it must be inherited. It is declared using an abstract keyword. It can also have non abstract method. It can have as much abstract method as it is required. It must have ablest one abstract method. It can always public I.e. we can use anywhere in a program.

EXAMPLE:
abstract class WashingMachine
{
   public WashingMachine()
   {
   }
 
   abstract public void Wash();
   abstract public void Load(int loadSize);
   abstract public long Spin(int speed);
}

 

In this example abstract class is declared with one implemented method and other 3 methods are unimplemented method.

EXAMPLE:
//We can create abstract class car:
Public abstract void class car
{
Public abstract void fillcolor();
Public virtual void music();
{
Messagebox.show(“car have music system”);
}
}
 
In this code abstract method named fill color using the keyword abstract which is declared without method body.non abstract method I.e. music is made a virtual method using the keyword virtual but can be override in the derived class by the message “car have music system”.
Public class myoldcar:car
{
Public override void fillcolor()
{
Message box.show(“color of old car is red”);
}
}

 

 In this code myoldcar class is derived class inherited from the base class abstract class car and the fill color method belongs to the base class is being overridding in the derived class.

INTERFACE (MULTIPLE INHERITENCE)

It is declared in the same way as we declared classes. It is declared by using the keyword interface. It contains only method declarations the method do not have their body containing code. Interfaces in C# are the replacement of multiple inheritances because c# does not support multiple inheritances. The most important thing is that the classes that implement the method defined in the interface.

Declaration of Interface

Interface  InterfaceName

{ //method declarations } 

 

Example: (implementation of interface)

Let’s take an example of mammals, as we know mammals have both similar as well as dissimilar characteristics we have taken two subclasses of mammals like human and whale because human have only characteristics of intelligence which is distinguish from other subclass of mammals the human class inherit both the class mammal and an interface and an intelligent that selectively describes it as a separate class of other mammals.

CONSTRUCTOR

A Constructor is a special member function whose task is to initialize the object of its class. It is special because its name is same as class name. Constructor cannot return anything not even void. We can overload the constructor. It should be declared in the public section of the class. A class can have multiple constructors.

EXAMPLE:
Class X
{
Public:
X ();             //constructor for class X with no parameters
X (int a)    //constructor for class X with 1 parameters
};
STATIC MEMBERS

Static members are of two types’ static variables and static methods

They are accessed using the class name .static members are used to represent data or calculations that do not change in response to object state

DECLARATIONS OF STATIC MEMBERS ARE:

Static data types variable name;                                //static variable declarations

Static access-modifier return type method name (); //static method declarations

Example:
Class circle
{
Public static int area;
Const float PI=3.14;
Static public void area()
{
Float res=PI*radius*radius;
Console.writeline(“area of circle”);
}
}
Class irfan
{
Static void main(string[] args)
{
circle. radius=5;
circle. area();
Console. read ();
}
}
}

Updated 30-Nov-2017
I am a content writter !

Leave Comment

Comments

Liked By