affiliate_link
Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. Show all posts

Sunday, June 10, 2012

OOP Q&A

1. What is the difference between Abstract class and Interface?
Abstract Class
An abstract class can have one or more abstract functions but not necessarily all functions have to be abstract.
Abstract function needs to be implemented in its extending class
Abstract classes can have function definitions as well as implementations

Interface
Interfaces can only have abstract functions which must be implemented in extending class

2. What is the difference between encapsulation and abstraction?

Encapsulation
Encapsulation means information hiding i.e hiding all the complexity in one class.
Encapsulation can be achieved by grouping data and behavior in one class that is capsule and making use of access modifiers like public, protected, private along with inheritance, composition or aggregation.

Like Vehicle class has start and stop behaviors which can be inherited by Car class without knowing all the complexities involving in the start & stop functions

Abstraction
Abstraction is the characteristics of an object which differs it from other objects
Abstraction can be achieved by defining a class having one or more abstract methods which is nothing but characteristics which should be implemented by the extending class

Thursday, October 2, 2008

Constructor and Destructor

Contructor
A constructor is a special kind of a method that is called whenever a class is initialized

VB.Net code snippet
    Public Sub New()
' Perform initialization
Debug.WriteLine("I am alive")
End Sub
Data can be passed to a constructor for more flexibility and power. Constructors with parameters are called parameterized constructors.

Destructor
Desctructor is a method that is called to cleanup resources.

Visual Basic .NET provides a Finalize destructor. This destructor is called when the .NET garbage collector determines that the object is no longer needed. There may be a delay between the time an object is terminated and the time the garbage collector actually destroys the object.

Tip
You should not normally use a Finalize destructor because of this delay and the additional processing required by the system to manage objects with a Finalize destructor. Use the Dispose destructor instead.

In order to better manage the resources used by your class, implement the IDisposable interface and the Dispose destructor:

VB.Net code snippet

    Implements IDisposable
Public Sub Dispose() Implements System.IDisposable.Dispose
' Perform termination
End Sub
This destructor is not called automatically, so it must be explicitly called as shown in the next section.

Tip The Dispose destructor is not required, but it is recommended. By implementing the Dispose destructor in every class, even if it does not do anything, developers can routinely call the Dispose method on any object.
   m_oCustomer.Dispose()
m_oCustomer = Nothing

What is an Object?

Object is an in-memory representation of a class

Declaring an object

C# code snippet

Customer oCust = new Customer();

What is a class?

A class is a logical grouping of attributes, properties or fields and methods.

Declaring a class

C# code snippet

public class Customer
{
public int ID;
public string name;
public string phone;

public Order GetLastOrder()
{
return new Order();
}
}