Tuesday, 14 February 2012

Differences between Abstract Class and Interfaces


Abstract class

Interface

A class can extend one abstract class

Abstract class contains abstract as well as non abstract methods

In this, multiple inheritance is not supported

Abstract class defines few or none of the methods.

Abstract classes should have sub classes

Any class can extend an abstract class

A interface can extend several interfaces

Interface contains only abstract methods

In this, multiple inheritance is supported.

Interface declares all the methods

Interface must implementation by classes.

Only an interface can extend another interface.

Interfaces


Interfaces contain only the signature of the methods.

Interfaces describe common set of methods. An interface has no implementation on its own, because it contains only the definition of methods without their bodies.

Definition of the method contains method name, parameter’s name, and their data type.

Interface is declared by interface keyword.

Interfaces are used to apply multiple inheritance in code. By This feature interface is differ from abstract classes.

Interfaces can extend multiple interfaces so multiple inheritance is not the problem

Example:

Interface A

{

Void print ()

}

Interface B

{

Void print ()

}

Class C : A,B

{

Void A. print ()

{

Console.writeLine (“this is interface A’s method”);

}

Void B.print ()

{

Console.writeLine (“This is interface B’s method”);

}

Public static void main ()

{

C x= new C ();

A a=(A)x;

B b=(B)x;

a.print();

b.print();

}

Abstract class


An abstract class is a class which provides a common definition of base class that multiple derived classes can share.

Some distinct characteristics of an abstract class:

Ø We cannot create an object of the abstract class, it must be inherited.

Ø It can have abstract as well as non-abstract members in an abstract class.

Ø We must declare at least one abstract method in the abstract class.

Ø An abstract class is always public.

We need abstract classes whenever we define a template that needs to be followed by all the derived classes.

Abstract classes are specified by keyword Abstract

Example:

Abstract class A

{

Public abstract void display ();

}

Class B : A

{

Public override void display ()

{

Console.writeLine (“this is class B’s method”);

}

Class C : A

{

Public override void display ()

{

Console.writeLine (“this is class C’s method”);

}

Class program

{

Public static void main (string [] args)

{

B b=new B ();

b.display ();//calls class B’s method

C c=new C ();

c.display ();// calls class C’s method

}

}

Encapsulation:


Encapsulation is a process of hiding irrelevant data and showing relevant data to user.

In this, we can show only essential features of a class to user and background details of a class are kept hidden from user.

The user can perform limited operations on hidden data of the class by methods.

Access modifiers are used to achieve the encapsulation in object oriented programming language.

Access modifiers:

In c#.net, we have four access modifiers

· Public

· Private

· Protected

· Internal

By using this we have five accessibility levels they are

Ø Public

Ø Private

Ø Protected

Ø Internal

Ø Protected internal

Public:

it gives unrestricted access to members inside and outside a class.

Example:

Class A

{

Public int i;

}

Private:

It gives access to the members of inside the class only. If we declare private to any member, it will not access outside of the class. By default, class members are private.

Example:

Class A

{

Private int i;

Float x;//by default, it is private.

}

Protected:

It gives access to members of the class and its derived class members.

Example:

Class A

{

Protected int i;

}

Class B: A

{

Int j;

j=i;

}

Internal:

It gives access to the members that are present in the current assembly. If a member with internal access modifier is accessed from outside the assembly in which it has been defined, an error is generated.

Example:

Public class A

{

Internal int x=0;//accessible within the same assembly

}

Protected internal:

It gives access to the members that are visible either to the current assembly or to the types derived from the class in which they are declared.

polymorphism


Polymorphism is the process of defining more functionality with same name with in the class. Polymorphism is a process of one form available in different forms. We have two different types of polymorphism,

1) Compile time polymorphism/static polymorphism (overloading)

2) Runtime polymorphism/dynamic polymorphism (overriding)

Compile time polymorphism (overloading):

Compile time polymorphism is defining more functionality with the same name in the class but difference in the signature of the method. Signature of the method means the order and the type of method arguments.

In this, multiple methods declared with same name but signature will be different. The compiler decides which method will execute in compile time, so it is known as compile time polymorphism.

The main advantage of this type is execution is fast.

Overloading a method simply involves another method with the same name with in the class

Example:

Class A

{

Public void display ()

{

Console.writeLine (“this is first method”);

}

Public void display (int a, int b)//method overloading

{

Console.writeLine (“this is second method”);

}

Public void display (int a, float b)

{

Console.writeLine (“this is third method”);

}

}

Class program

{

Public static void main (string [] args)

{

A x=new A ();

x.display ();

x.display (5, 8.4);

x.display (2, 4);

}

}

Runtime polymorphism (overriding):

Runtime polymorphism is defining more functionality with the same name and same signature in the derived class.

In this, base class method is override in derived class with the keyword ‘override’. The compiler decides which method will execute in runtime, so it is known as runtime polymorphism.

Overriding occurs when a derived class has a method with same signature as a base class’s method.

The ‘base’ keyword is used to access the base method that has been overridden.

Example:

Class A

{

Public virtual void display ();

}

Class B : A

{

Public override void display ()

{

Console.writeLine (“this is class B’s method”);

}

Class C : A

{

Public override void display ()

{

Console.writeLine (“this is class C’s method”);

}

Class program

{

Public static void main (string [] args)

{

B b=new B ();

b.display ();//calls class B’s method

C c=new C ();

c.display ();// calls class C’s method

}

}

Inheritance

Inheritance:

Getting the features of one class to another class is known as inheritance.

The class which is giving features to another class is known as base class or parent class.

The class which getting the features from another class is known as derived class or child class.

Data members and methods are called as features of class.

The main advantage of inheritance is ‘code reusability’.

Types of inheritance are

1) Single inheritance

2) Multilevel inheritance

3) Hierarchical inheritance

4) Multiple inheritance

5) Hybrid inheritance

Single inheritance:

In this inheritance, there is only one base class and one derived class.

Multilevel inheritance:

In this we have many base classes and many derived classes. The classes which are acts as a base class as well as derived class known as intermediate classes.

Hierarchical inheritance:

In this, there is no. of derived classes derived from one base class.

Multiple inheritance:

In this, one class derived from multiple base classes. Multiple inheritance is not supported in c# in classes. We can achieve this by interfaces.

Hybrid inheritance:

The combination of any two inheritances is known as hybrid inheritance.


Example:

public class A

{

public void Add()

{

console.writeline("this is class A's method");

}

}

public class B : A

{

public void print ()

{

console.writeline ("this is class B's method");

}

class program

{

static void main(string[] args)

{

A x = new A ();

x.print();

B x= new B ();

x.print ();

}

}

Classes and Objects

Class:

Class is the name of type that we use to represent the relation between the data and functions or methods. In the other way, Class is a block of code which combines methods and properties for a group of objects.

Class describes the properties of objects which are present in the class. In the class, objects are categorized according to their state and behavior. By default, classes are private.

Syntax:

Class class name

{

Statements;

}

Example:

Class employee

{

String name;

int id;

Public employee (string name, int id) // constructor

{

This.name=name;

This.id=id;

}

Public void print () //method

{

Console.writeLine (“employee name is”, name);

Console.writeLine (“employee id is”, id);

}

}

Class program

{

Public static void main ()

{

employee s1=new employee ();

s1.print ();

employee s2=new employee ();

s2.print ();

}

}

Object:

Object is an instance or an entity that will represent the components of a class. Objects interact with each other through the functionality of methods in the class.

The ‘new’ operator is used to create object of a class. When we create an object, then system creates memory for data member and methods that are present in the class.

For example, we take ‘animal’ as a class then dog, cat, tiger and other animals are the objects.