Monday, 19 July 2010

Constructors in C#

http://www.c-sharpcorner.com/UploadFile/puranindia/LearningConstructorsInC-sharp05182009044452AM/LearningConstructorsInC-sharp.aspx

Wednesday, 14 July 2010

polymorpihsm

polymorpihsm ->one name, many form.

have 2 types : 1. Compile time, 2 Run time

1 compile time: also called overloading
have 3 types. 1. Constructor overloading(i.e.,with same constructor name and different no of arguments or different datat types or both ) 2 Function overloading(i.e.,with same function name and different no of arguments or different datat types or both)

3. operator overloading: exaample : String s = "James";

s = s + "Bond";

Runtime poly: is achieved by overridding parent class method:

This is called Dynamic method dispatch.


1. function overloading,

2. Runtime polymorphism --Using Interface reference,

EX:
interface name I ,abstract Method();
implimenting classes A,B

ref I;
objA= new A();
objB= new B();

runtime
objA=I

objA.Method();

objB=I;
objB.Method();

Tuesday, 6 July 2010

Can we call a base class method without creating instance?

Its possible If its a static method.
Its possible by inheriting from that class also.
Its possible from derived classes using base keyword.

You have one base class virtual function how will call that function from derived class?

class a
{
public virtual int m()
{
return 1;
}
}
class b:a
{
public int j()
{
return m();
}
}


Class a
Public Overridable Function m() As Integer
Return 1
End Function
End Class
Class b
Inherits a
Public Function j() As Integer
Return m()
End Function
End Class

Thursday, 17 June 2010

SHADOWING

When global and local varible in the same name.
the local varibale in a mehod or function which use to
override the global is called the shadowing.
ie the Global varible is being shadowed by the
local varible


Public Class Clsparent
Public x As String = 7

End Class

Public Class ClsShadowedparent
Inherits Clsparent
Public Shadows x As String = "dgfdg"

Sub kotha()
Dim k As Integer = MyBase.x
Dim kk As String = Me.x
End Sub
End Class



button_click :
============

Dim ksf As New ClsShadowedparent
ksf.kotha()




Thre are few differences between those.
1. Shadowing is bad programming practice according to OOPs concepts.
2. In shadowing signature could be different.
3. In Shadowing both Derived class methods and Base Class methods are available for use.(So its a bad practice)

Mulutiple interfaces having same method how to implment and call

c# :
interface Interface1
{
void krs();
}


interface Interface2
{
void krs();
}


class MultInterfaces: Interface1,Interface2
{

void Interface1.krs()
{
int k = 740;
}



void Interface2.krs()
{


int k = 340;
}
}




private void Form1_Load(object sender, EventArgs e)
{
MultInterfaces obj = new MultInterfaces();
Interface1 obj1=new MultInterfaces();
Interface2 obj2 = new MultInterfaces();
obj1.krs();
obj2.krs();
}




vb.net :

INTERFACE1

Public Interface Interface1
sub test()
End Interface

INTERFACE2

Public Interface Interface1
sub test()
End Interface

Class1

Public Class Class1
implements Interface1,Interface2

sub Interface1test ()implements Interface1.test
console.WriteLine("i1")
end sub
sub test() implements Interface2.test
console.WriteLine("i2")

end sub
End Class

WINDOWS Form1
Public Class Form1

Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dim obj_I1 as Interface1=new Class1
dim obj_I2 as Interface2=new Class1
obj_I1.test
obj_I2.test
End Sub
End Class

What is the difference between class and structure

1) Structure: Initially (in C) a structure was used to
bundle different type of data types together to perform a
particular functionality. But C++ extended the structure to
contain functions also. The major difference is that all
declarations inside a structure are by default public.

Class: Class is a successor of Structure. By default all
the members inside the class are private.

2) structures in c++ doesn't provide data hiding where as a
class provides data hiding

classes support polymorphism, whereas structures don't
3) class and structure are very similar. the former is
heavyweight while the latter is light weight. reference to
the former rests on the heap..while the latter in whole
(instance and data) rests on the stack. therefor care
should be taken not to make a struct very heavy else it
overloads the stack causing memory hogging. class needs to
have an instance explicitly created to be used. A struct
doesn't have to be explicitly initiated

Polymorphism

4.17. What is Polymorphisms?

Polymorphisms is a generic term that means 'many shapes'. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.

At times, I used to think that understanding Object Oriented Programming concepts have made it difficult since they have grouped under four main concepts, while each concept is closely related with one another. Hence one has to be extremely careful to correctly understand each concept separately, while understanding the way each related with other concepts.

In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding,

4.18. What is Method Overloading?

The method overloading is the ability to define several methods all with the same name.
Collapse
public class MyLogger
{
public void LogError(Exception e)
{
// Implementation goes here
}

public bool LogError(Exception e, string message)
{
// Implementation goes here
}
}

4.19. What is Operator Overloading?

The operator overloading (less commonly known as ad-hoc polymorphisms) is a specific case of polymorphisms in which some or all of operators like +, - or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments.
Collapse
public class Complex
{
private int real;
public int Real
{ get { return real; } }

private int imaginary;
public int Imaginary
{ get { return imaginary; } }

public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}
}
I above example I have overloaded the plus operator for adding two complex numbers. There the two properties named Real and Imaginary has been declared exposing only the required “get” method, while the object’s constructor is demanding for mandatory real and imaginary values with the user defined constructor of the class.

4.20. What is Method Overriding?

Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.

A subclass can give its own definition of methods but need to have the same signature as the method in its super-class. This means that when overriding a method the subclass's method has to have the same name and parameter list as the super-class's overridden method.

Collapse
using System;
public class Complex
{
private int real;
public int Real
{ get { return real; } }

private int imaginary;
public int Imaginary
{ get { return imaginary; } }

public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}

public override string ToString()
{
return (String.Format("{0} + {1}i", real, imaginary));
}
}
In above example I have extended the implementation of the sample Complex class given under operator overloading section. This class has one overridden method named “ToString”, which override the default implementation of the standard “ToString” method to support the correct string conversion of a complex number.
Collapse
Complex num1 = new Complex(5, 7);
Complex num2 = new Complex(3, 8);

// Add two Complex numbers using the
// overloaded plus operator
Complex sum = num1 + num2;

// Print the numbers and the sum
// using the overriden ToString method
Console.WriteLine("({0}) + ({1}) = {2}", num1, num2, sum);
Console.ReadLine();

4.21. What is a Use case?

A use case is a thing an actor perceives from the system. A use case maps actors with functions. Importantly, the actors need not be people. As an example a system can perform the role of an actor, when it communicate with another system.

usercase1.gif

In another angle a use case encodes a typical user interaction with the system. In particular, it:
  • Captures some user-visible function.
  • Achieves some concrete goal for the user.
A complete set of use cases largely defines the requirements for your system: everything the user can see, and would like to do. The below diagram contains a set of use cases that describes a simple login module of a gaming website.

usecaseLogin.gif

4.22. What is a Class Diagram?

A class diagrams are widely used to describe the types of objects in a system and their relationships. Class diagrams model class structure and contents using design elements such as classes, packages and objects. Class diagrams describe three different perspectives when designing a system, conceptual, specification, and implementation. These perspectives become evident as the diagram is created and help solidify the design.

The Class diagrams, physical data models, along with the system overview diagram are in my opinion the most important diagrams that suite the current day rapid application development requirements.

UML Notations:

notation.jpg

differace between Overridable AND Overrides

Public MustInherit Class overridesk


Public Overridable Function Result(ByVal i As Integer, ByVal j As Integer) As Integer
Result = i / j
End Function

Public MustOverride Function add() As Integer

End Class



Class kk
Inherits overridesk
Public Overrides Function add() As Integer

End Function


Public Overrides Function Result(ByVal i As Integer, ByVal j As Integer) As Integer
Result = i + j
End Function
End Class


button!.click
====================
Dim abs As New kk()

abs.Result(1, 3)

===================

----> In VB.NET WRITE ABSTRACT CLASS DECLARE CLASS MustInherit
----> in parent class u write overridable function there is no need must declare ovverrides
----> but u want write overrides in derived class u must declare parent class overridable which function u want
-----> and u decalre parent class mustoverride fuction there is also complsary need to derrived class that function
EX:
PARENT
Public MustOverride Function add() As Integer

derived :
Public Overrides Function add() As Integer

End Function

Difference between abstract class and interface

Re: Difference between abstract class and interface
Answer
# 8

(1) 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.
(2) 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.
(3) 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.
(4) 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.
(5) Various access modifiers such as abstract,
protected, internal, public, virtual, etc. are useful in
abstract Classes but not in interfaces.
(6) Abstract classes are faster than interfaces.

Abstration

abstraction is the hiding the complexity from the end user by providing him set of
interfaces and to derive the code and invoke the code





Data Hiding or Abstraction:

Normally, in a Class, variables used to hold data (like the age of a dog) is declared as Private. Functions or property routines are used to access these variables. Protecting the data of an object from outside functions is called Abstraction or Data Hiding. This prevents accidental modification of data by functions outside the class.