Tuesday, April 29, 2008

Abstract Class & Interface

Q 1. What are abstract classes ?

Ans - Following are the features of a abstract class :-

  • You can not create a object of abstract class.
  • Abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built.
  • Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
  • Abstract classes can have implementation or pure abstract methods which should be implemented in the child class.
  • In VB.NET abstract classes are created using "MustInherit" keyword.In C# we have "Abstract" keyword.

An abstract class only allows other classes to inherit from it and cannot be instantiated. When we create an abstract class, it should have one or more completed methods but at least one or more uncompleted methods and these must be preceded by the key word abstract. If all the methods of an abstract class are uncompleted then it is the same as an interface, but there is a restriction that it cannot make a class inherit from it, which means it can not work as a base class.


Q 2. What is a Interface ?

Ans - An interface is defined by the key word interface. An interface has no implementation; it only has the definition of the methods without the body. When we create an interface, we are basically creating a set of methods without any implementation. A class implementing an interface must provide the implementation of the interface members. All the methods and properties defined in an interface are by default public and abstract.

Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing Idisposable interface that means it has a functionality to release unmanaged resources. Now external objects using this class know that it has contract by which it can dispose unused unmanaged objects.

  • Single Class can implement multiple interfaces.
  • If a class implements a interface then it has to provide implementation to all its methods.


Q 3. What is the difference between abstract classes and interfaces?

Ans - Following are the differences between abstract and interfaces :-

  • An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.
  • An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.
  • An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.
  • A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
  • Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
  • Abstract classes are faster than interfaces.

Sunday, April 27, 2008

OOPS Concepts

OOPS Concepts are described below: -
  • Class - A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It’s a comprehensive data type which represents a blue print of objects. It’s a template of object.
  • Object - An object is the basic building block of object-oriented programming
  • Instance - One can have an instance of a class or a particular object. The instance is the actual object created at runtime. The set of values of the attributes of a particular object is called its state. The object consists of state and the behaviour that's defined in the object's class.
  • Method - In a simple language it is An object's abilities.
  • Message Passing - The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing.
  • Inheritance - It means reusability. Example we have VEHICLE class and we can inherit this class to make more specialized class like CAR, which will add new attributes and use some existing qualities of the parent class. It shows more of a parent-child relationship. This kind of hierarchy is called inheritance.
  • Encapsulation - Encapsulation conceals the functional details of a class from objects that send messages to it in other words It is a process of hiding all the internal details of an object from the outside world. It means that all of the object's data is contained and hidden in the object and access to it restricted to members of that class. Programming languages aren't quite so strict and allow differing levels of access to the object's data. The first three levels of access shown below are in both C++ and C#, with the last two in C#.
    1 . Public : All Objects can access it.
    2 . Protected : Access is limited to members of the same class or descendants.
    3 . Private : Access is limited to members of the same class.
    4 . Internal : Access is limited to the current assembly.
    5 . Protected Internal : Access is limited to the current assembly or types derived from the containing class.
  • Abstraction - It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB. By just making the combination of these three colors we can achieve any color in world.It’s a model of real world or concept.
  • Polymorphism - Polymorphism allows you to treat derived class members just like their parent class' members.
    When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class(Generalized class). The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.

Saturday, April 26, 2008

OOPS Interview Question part - I


(Q1 ) What is Object Oriented Programming ?

Ans . Object-oriented programming (OOP) is a programming paradigm that uses "objects" and their interactions to design applications and computer programs. Programming techniques may include features such as, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming language now support OOP.

It is a problem solving technique to develop software systems. It is a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects.

(Q2) What’s a Class ?

Ans . A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It’s a comprehensive data type which represents a blue print of objects. It’s a template of object.

(Q3) What’s an Object ?

Ans. It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class. Attributes and behavior of an object are defined by the class definition.

(Q4) What is the relation between Classes and Objects ?

Ans. They look very much same but are not same. Class is a definition, while object is a instance of the class created. Class is a blue print while objects are actual objects existing in real world. Example we have class CAR which has attributes and methods like Speed, Brakes, Type of Car etc. Class CAR is just a prototype, now we can create real time objects which can be used to provide functionality. Example we can create a Maruti car object with 100 km speed and urgent brakes.