Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Friday, July 24, 2009

Difference between Overriding and Overloading

Difference between Overriding and Overloading

Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.

Overriding
■The return type must exactly match that of the overridden method.
■The access level must not be more restrictive than that of the overridden method.
■The access level can be less restrictive than that of the overridden method.
■The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
■The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
■You cannot override a method marked final.
■If a method can’t be inherited, you cannot override it.
Overloaded method
■Overloaded methods must change the argument list.
■Overloaded methods can change the return type.
■Overloaded methods can change the access modifier.
■Overloaded methods can declare new or broader checked exceptions.
■A method can be overloaded in the same class or in a subclass.

Wednesday, July 1, 2009

What is the difference between static and class method

static objects or members of a class that can be accessed directly from the class, while non-static members can only be accessed from the instance it belongs to.

C# follows a similar principle for the methods. The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed, and furthermore impractical (see below).

In OOP, static variables are used for values which cannot be stored by an instance variable. Example: supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

The methods use a similar principle. They should be used for procedures for which it is impractical to do within an instance of a class. I tend to use them for broad procedures (not a technical term), meaning those that do not require me to instantiate an object. Example, adding two parameters. (This usage may or may not be correct, but I believe it is)

However, if you wanted to add two properties of an object, the method cannot be static, because as you would soon realize, static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class the get these from unless it were told, since it is not part of an instance itself)

Thursday, May 14, 2009

Difference between struct and Class

Difference between struct and Class
1. Struct are value type and stored on stack while class are reference type and store on heap.
2. Struct do not support inheritance while class supports inheritance, However struct can implements interface.
3. Struct should be used when you want to use a small data structure while class is better choice for complex data structure.
4. The fourth significant difference between class objects and struct objects refers to constructors. Constructors are supported by structs in the C# language, and you can define constructors for structs in exactly the same manner as you would for classes. If you are not familiar with constructors, they are methods with the same name as the struct or class. However, the only exception with struct constructors is that you are not permitted to define a constructor that takes no arguments.
5. By default, Structure data members are public but class data members are private.
6. In Structure we can’t declare any default constructor explicitly because it provides by .net framework. But in class we can declare default constructor explicitly.
 
Locations of visitors to this page