Monday, December 29, 2008

OOPS-(C# OOPS,Concepts,Scenarios,Tips and Tricks)

Hi Friends ,We all know that OOP's is the buzz word in the programming environment and we use these concepts in our everyday developments to get flexibility,robustness,scalability.Now a days when we go for any interview that may be C++,.Net,C#,java or SAP,the first questions they ask is OOP's because of its frequent use and importance in real time.So I am bringing you this article that demonstrates the conceptual knowledge and use case scenarios on oops.Whatever the concepts i am covering here is beyond the basic level by thinking that you are familiar with basics .Here I am explaining all the concepts based on C#,same thing applies to other languages with little bit syntax difference .Fun concepts starts from here.........................

What is OOP's:

  • OOP is the common abbreviation for Object-Oriented Programming.
  • It is the new concept of programming ,parallel to Procedure oriented programming.It were intorduced in late 80's.It consider the programming simulated to real world objects.It help in programming approach in order to built robust,user friendly and efficient softwares and provide the efficient way to maintain real world softwares

Why OOP's:

  • OOP become a popular since it provide a better programming style, you don't need to write code which you really need to run anytime you need (such as in structured programming and assembler), you just make a class of object and you may call/instantiate the class and use it from any part of your application, it is reusable.
  • compiler provide a library of those class, and you just use it, no need to write the codes. Your application mostly concern on managing the interaction among object, not write a command for any specific jobs.
  • OOP provide Inheritances model, so your application can inherit another class which is having similarity on behavior such as the variables and its methods, but you just write the distinct behaviour for your own implementation
  • OOP provide encapsulation model, once you instantiate an object, it's behavior to do their own job will not interupt another object, you dont bother if your code will affect another object.
  • OOP provide polymorphism, here you may call any methods in run time which is depend on particular command you provide, but still using the same method implementations.
  • OOP provide modularity mobel once you change part of code which is in separate module, it will not impact any module developed in application

Class and Objects:

  • Everything in our world is considered to be an object. For example, people are objects, animals are objects too, minerals are objects; everything in the world is an object. Easy, But what about classes?
  • In our world we have to differentiate between objects that we are living with. So we must understand that there are classifications (this is how they get the name and the concepts of the Class) for all of those objects. For example, I'm an object, sunil is object too, lakshmi is another object. So we are from a people class (or type).
  • I have a dog called Puppy so it's an object. My friend's dog, Snoopy, is also an object so they are from a Dogs class (or type).
  • A third example: I have a Pentium 3; this is an object. My friend has a Pentium 4, so this is another object and they are from a Computers class (or type). Makes sense right...
  • Class features can be used only after creating object called instance Types.

Classes and Objects Behaviour:

1)Static Behaviour :Static classes can be used when there is no data or behavior in the class that depends on object identity.

  • Example:Consider you have a company .Assume that there are 10 employees in that company.Here we could declare company class as static ,because company it self as an independent entity and it is common for all 10 employees.So you dont require to assign different company names for each employee.
  • When you consider employee class its not good to declare static as values will keep changing for each employee like name, sal,age etc..
  • Static classes will always have a common memory where as normal classes are not
  • Its not possible to inherit static classes .If you try it will throw compilation error
  • Only static members are allowed in static classes
  • Its not possible to create objects to static classes.Can be accessed via class name
  • when you declare static variables in normal class ,those cant be accessed from object but can be inherited.
  • Its not possible to declare interface as static

2)Constructor :

  • It is also a method with out return type
  • It gets invoked while creating object
  • Private Constructor:
  • A private constructor is a special instance constructor.
  • It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class
  • Private constructors are used to prevent the creation of instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the entire class static.

Example : public class Counter

{

private Counter() { }

public static int currentCount;

public static int IncrementCount() { return ++currentCount; }

}

  • Static Constructor:
  • A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

Abstract Behaviour:

Scenario's:

  • abstract class:In most cases, using an abstract class is the "right" thing to do if you're trying to create a common class from which others will derive and which isn't fully specified.
  • Use abstract class when you want to provide some functionality to the user and at the same time would like to enforce some skeleton (structure) for the users of your class
  • Interfaces: are nice if you don't want to force classes to have a single root class in their hierarchy, as a single class can implement multiple interfaces.
  • Interfaces are used to define a skeleton. Classes who want to confirm to that skeleton implementsts the interface
  • Concrete classes should be used if it's fully specified--i.e. subclasses don't have any hooks into which they must provide functionality.
  • Concrete class - Use a concrete class to represent an object (data & methods) which can be extended or reused.



No comments: