Posted in C#, Object Oriented Programming, Static

Static keyword in C#

Static class cannot be instantiated using the “new” keyword

 

Static items only access other static items. Share resources between multiple users

 

Static constructor in non-static class: runs only once when class instantiated first time

 

In static class: runs only once when any of its static members accessed for the first time
Static members are allocated in high frequency heap area of the memory

 

 

Posted in Abstract Class, C#, Interface, Object Oriented Programming

C# – Abstract Class vs Interface

Abstract Class Interface
Multiple inheritance Doesn’t support support
Data Member contains Doesn’t contain
Constructor declaration contains Doesn’t contain
Definition May contain definition with no implementation Just a pattern, an empty shell
Methods Can have both abstract & non-abstract methods Can only have abstract methods
Implementation Can contain both definition and its implementation

Incomplete class, cannot be instantiated

Can only have the signature of the functionality without any code
Access Modifier Can have all access modifiers for member declaration of functions, subs and properties. Not allowed (all methods must be implicitly defined as public).

All the members assumed as implicitly public.

Homogeneity is used for implementations of the same type, behavior and status Is used for implementations that share only method signatures
Declaration Acts as a base class for all other classes, can declare/ use any variable Not allowed to declare any variables
Core vs Peripheral Core identity of a class & for objects of the same data type Used to define Peripheral ability of a class
Rigid vs Supple More supple More rigid
Fields definition Predefined fields & constants Fields cannot be defined in interfaces
Posted in Abstract Class, C#, Concrete Class, Object Oriented Programming

Abstract classes vs Concrete Classes

 

Abstract Class Concrete Class
Type Base class Default class
Methods May contain partially implemented methods All methods are completely implemented
Functions Some or all declared functions are purely virtual No purely virtual functions
Instantiation Cannot be instantiated Can be instantiated

Image Courtesy: tutorials.jenkov.com, stackoverflow.com

Posted in Core Java, Object Oriented Programming

Generics

A class, interface or method that declares one or more type variables (type parameters).

Advantages:

  • Type-Casting
  • Type-Safety (single type of objects)
  • compile-time checking

public Class Box{                                     ——–>              public Class Box<T>{

private Object obj;                                                                              private T t;

public void set (Object obj){                                                             public void set(T t){

this.obj=obj;                                                                                          this.t=t;

}                                                                                                        }

public Object get(){                                                                               public T get(){

return obj;                                                                                              return t;

}                                                                                                           }

}                                                                                                          }

Posted in Core Java, Design Pattern, Object Oriented Programming

Singleton Pattern

A Singletone class;

Only one time in JVM per class loader would be instantiated. Same instance would be use for repeated calls.

public class OnlyOne {

            private static OnlyOne one = new OnlyOne();

            // private constructor, so this class cannot be instantiated from outside and prevents

            //subclassing.

private OnlyOne();

//a global point of access

public static OnlyOne getInstance() {

            return one;

}

}

To use it:

OnlyOne myOne = OnlyOne.getInstance();

 

Posted in Core Java, Object Oriented Programming

String vs StringBuffer == and .equals methods

  str1==str2 str1.equals(str2)
String Compares Objs location Compares Objs String contents
StringBuffer, StringBuilder Compares Objs location If and only if str1==str2 would be True.

 

So if we need to compare or sort in StringBuffer / StringBuilder we need to turn them into String first. For example:

if (str1.ToString() = str2.ToString())   {

System.out.println(“True”);

}   else   {

System.out.println(“False”);

}