Thursday, February 25, 2016

oops Interesting....



Interface
Abstract class
we can not declare a static method in an interface, because interfaces have no implementation.
xxxxxx
public interface Iproduct
{
 
static void test();
 
}
xxxxx
declare a method as static in an abstract class,
 
 because abstract classes can have implementations of static methods
ex: public static void test()
 {
 }
Thus by above point :interface cannot implement methods
abstract class can implement methods
An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
An abstract class can contain fields, constructors, or destructors and implement properties
Based on above point, we can say ,: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.
No access modifiers are allowed in Interface
Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes




Abstract classes are faster than interfaces.


--------------------------------------


Sealed class


----------------------------------------


 


public sealed class classSealed


 {


 // Class members here.


 public string ID;


 public double Price;


 }


-------------------------------------------------------------------------------------------------


A sealed class cannot be used as a base class.


For this reason, it cannot also be an abstract class.


Sealed classes are primarily used to prevent derivation.


Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.


Sealing a method means one cannot override it.


 In C# structs are implicitly sealed; therefore, they cannot be inherited.


 


Ex:Humanàmale class,female class(Here we can say male and female classes are sealed)


public class TestClass : classSealed


 {


 }


 


In C# a method can not be declared as sealed.


However when we override a method in a derived class, we can declare the overridden method as sealed as shown below.


By declaring it as sealed, we can avoid further overriding of this method.


public class testClass


 {


 public int x;


 public int y;


 public virtual void testMethod(){


 }


 }


 public class TestClass: testClass


 {


 public override sealed void testMethod(){


 


 }


 }


-------------------------------------------------------------------------------
Prove that only 1 instance of the object is created for static classes?

static constructor:only one 1 instance of the object is  created then this constructor should run only once.

Static class:we can define static memebers(like public static int icount)

if we invoked the method twice/but constructor will fire only once.

public static class Staticclasses
    {
    public static int icount;
    static Staticclasses()
    {

    }
    static void somemethod()
    { }

    }

What is the use of private constructor ? This is used for utility classes

1.a class with a private constructor can not be inherited

2. we can not create a object of the class which has private constructor
3. many times we do not want to create instances of certain classes like utility ,common routine classes



public class class1
{
private class1()
{
}
public static string getdata()
{
return "hello"
}
}

Client can use by using:

string str=class1.getdata()  (SO we are not create instance,we can access method)


Can we define abstract class as a static?

never forget  we can never define abstract class as static



What is the use of c# (Csharp) Shadowing ?

shadowing is replaces current element of the parent where as override is replace the implementations(it does not replace data type or (datatype /datamethod)



Abstraction_Encapsulation:
namespace Abstract_encapsulation
{
    public class customer
    {
        //step1:what does customer(Abstraction)//show only what is neccessary
        //step2:How(Validate,createdbobjects)
        //step3:made complicated method as private (Encapuslation):means reduce the complexity/hide complexity to client

        public string customercode = "";
        public string customername = "";
        //public void Add()
        //{

        //}

        //Granular


        //public bool validate()
        //{
        //    //cust code and name
        //    return true;
        //}
        //public bool createDbobjects()
        //{
        //    return true;
        //}


        //Now simplified to expose to clients

        public void Add()
        {
            validate();
            createDbobjects();
        }
        private bool validate()
        {
            //cust code and name
            return true;
        }
        private bool createDbobjects()
        {
            return true;
        }

    }
}

Inline image 1

Inline image 2

No comments:

Post a Comment