Anda di halaman 1dari 15

OOPS concepts:

Before oops: 1) Procedural languages i. No concepts of loops ii. No arrays iii. No dynamic memory allocation 2) Structured languages i. No proper implicit procedure for calculations ii. Data type limits are not controlled at run time. iii. No proper security for data

So the solution to avoid the above problems is OOPS 1. 2. 3. 4. 5. Encapsulation Abstraction Polymorphism Inheritance Interface Encapsulation is the concept of binding/hiding the data with the help of private variables. Abstraction is the concept of providing some information (not providing the full functionality of the data) about an entity (object). Polymorphism is the concept of making an entity to perform in many forms. 1. Method Overloading: Writing more than one method with same name and with different parameters/signature. 2. Method Overriding: Writing more than one method with same name and same
signature. 3. Operator Overloading: If an operator is supposed to perform in many ways, operator overloading must be done. Inheritance is the concept of deriving the features from a parent/base class into child/derived class.

**Note: To work with oops concepts classes and objects are required.

..

What is a Class? Collection of fields, properties, methods and events Field: private data of a class Ex: private int x; Properties: Defines the look and feel of the object Methods: What an object can do is called as a method Events: What user can do with an object is called an event. Note: Object is an instance to a class. Syntax to write a class class <class_name> { --fields --methods --events --properties Static void Main() { <class_name> obj = new <class_name()>; obj.(fields, methods, properties,.); }

Syntax to create an object: <classname> <objectname> = new classname(); new is a keyword and its a memory allocation operator Classname() is a constructor. It is a special type of method that has the same name of class name and it is called as a constructor. Access specifier specifies the scope or accessibility/visibility of a variable. Object can access only public data of any other class, but not private data If the data is private with in the class, object can access them. But not out of the class.

Classes and Methods:

1. The variables created inside of a class are also called instance variables. 2. In C#.Net all the classes are inherited from System.Object class by default. 3. System.object contains 4 methods. i. GetHashCode() ii. GetType() iii. ToString() iv. Equals() This: Is a keyword to access the instance variables, it acts like an object for the current class. When instance variable names and local variable names are same then default priority will be given to local variables. Ex: int i=10;//instance variable void m1() {

Int i=11;//local variables Console.WriteLine(this.i); } Static void main(string[]args) { Program p=new program(); p.m1(); } Working with Methods and Arguments: Syntax to write a method: <AccessSpecifier> < returntype> <methodname>(parms) { } Ex: Public void m1(int x,int y) { } C#.net allows passing the arguments into a function in 3 ways. i. Call by value ii. Call by reference iii. Call by Out Call by value & ref: 1) When formal arguments are modified and if modifications are reflected on actual arguments, then the concept is called call by reference. 2) If not reflected then call by value.

3) By default all the variables will be passed by value. 4) ref is a keyword, which is required to pass a variable by reference. 5) Syntax: Public void swap(ref int a,ref int b) { } 6) ref keyword must be used along with actual and formal arguments. 7) ref variables must be initialized before passing to a function. Example for call by value:Public void swap(int x,int y) { Int t=x; X=y; Y=t; Console.WriteLine(x+ +y); } Static void main(string[]args) { Program p=new program(); Int a=10,b=11; p.swap(a,b); Console.WriteLine(a+ +b); } }

Public void swap(ref int x,ref int y) { Int t=x; X=y; Y=t; Console.WriteLine(x+ +y); } Static void main(string[]args) { Program p=new program(); Int a=20,b=21; p.swap(ref a,ref b); Console.WriteLine(a+ +b); } } Call by out: 1) 2) 3) 4) 5) Ex: void add (ref int x, int y,out int z) { Z=x+y; Console.WriteLine(z); out is a keyword. out is 99% same as reference. Out variables can be passed without initialization also. Even if out variable is initialized, then value will not be considered. Out=ref-initialization.

} Static void main(string[]args) { Program p=new program(); Int a=21,c; p.add(ref a,43,out c); Console.WriteLine(c); } . Polymorphism: Poly means many and, morphism means forms Making a single entity to perform many tasks. Method Overloading: 1. Overloading is also called as compile time polymorphism or static polymorphism or early binding concept. 2. Having the same method name and different signature is called as method overloading Ex:class Program { void operation(int x, int y) { Console.WriteLine(x + y); } void operation(string s1,string s2) { Console.WriteLine(s1 + s2); } static void Main(string[] args) { Program p = new Program(); p.operation(22, 23); p.operation("hiii", "hw r u?"); } }

Method Overriding:

1. Overriding is also called as runtime polymorphism, dynamic polymorphism, late binding. 2. Having the same method name and same signature is called as method overriding 3. In overriding, by default importance is given to local class methods. 4. A base keyword is used to call the methods in parent class without creating any object. 5. base keyword can be used with methods and fields also. Ex:class c1 { public virtual void add(int x, int y) { Console.WriteLine(x + y); } class program : c1 { public override void add(int x, inty) { base.add(x, y); base.add(22, 25); base.add(6, 5); } static void Main(string[] args) { program p = new program(); p.add(22, 23); p.add(77, 66); } } }

////////////////////////////////////////////

Operator overloading: 1. int x=1, y=20, z; z= x+y; 30 2. string s1=C#, s2=.Net, s3; s3=s1+s2; C#.Net 3. Let us suppose a class Test and t1, t2, t3 are objects. Then, t3=t1+t2; error

Operator overloading is a concept of providing extra functionality for an existing operator. In the above 3rd case, + operator is to be overloaded to get the desired output i.e., for sum of two objects. All the operators are over-loadable except those contains a . (Dot). Ex: . (Member access operator) : (Inheritance operator) :: (property access operator) ?: (ternary or conditional operator) While overloading relational operators, these must be overloaded in PAIR. (>, <) (>=, <=) (==, !=) Syntax to overload an operator Public static <returntype> operator +(parm1,parm2) { } Ex: Poly_Operator_OL Constructors & Destructors Three types: 1) Default constructor 2) Parameterized Constructor 3) Copy Constructor: We can assign the values of one object to another object. It is same as parameterized constructor. But rather passing arguments we must pass objects. 4) Static constructor

** Note: What are class members? Static members | Static Variables Static Methods Static Constructors Instance members | Instance Variables Instance Methods Instance Constructors

A non-static method can consume the static members of the class directly, where as static method cant consume the non-static members of the class directly. They should only be referred by using the object of the class. Differences between Constructors and destructors Constructor is a special type of method which will be executed automatically while creating an object Destructor is a special type of method which will be executed automatically while destroying an object Constructor name must be same as the class name without any return type. Syntax: Public classname() { } Destructor name must be same as class name with a ~ (tilt) prefix and without return type and without access specifier and also without parameters. Syntax: ~classname() { } Constructors are over-loadable Destructors are not over-loadable Constructors are generally used to initialize instance variables or to open database connection etc. Destructors are used to de-allocate the memory or to close the file and connections etc. Ex:class program { program()//default constructor { Console.WriteLine ("object is created"); } program (string s)//parameterised constructor { Console.WriteLine (s); } ~program ()//default destructor

{ Console .WriteLine ("object is destroyed"); } static void Main(string[] args) { program p1=new program (); program p2=new program ("hii"); program p3=new program (); } }

//////////////////////////// Ex: E:\c#6\ConstDestr_sample

Static: 1. Static is a keyword, which can be used with variables, constructors, methods and classes 2. Static creates common memory for all the objects 3. Static variables will be created only once, while the class is loaded into the memory. Hence static variables are also called as class variables. 4. If the static variable is public then it can be accessed directly with class name. 5. Static constructor is executed only once, while class is loading into the memory. 6. A static constructor must be parameter less. 7. Static constructors can access only static variables. Ex:class program { int x = 11; static int y = 10; static program() { y = 11; } program() { x = x + 1; y = y + 1; Console.WriteLine(x + " " + y); } static void Main(string[] args)

{ program p1=new program (); program p2=new program (); program p3=new program (); } }

Working with static and instance members

1. 2. 3. 4.

Static method can access only static data. Static data need to be called with the help of classname.publicstaticdata And can call directly as <staticdata> if it is with in the class. If a class contains all static members, then it is recommended to declare that class as static. 5. Static classes are not instantiable (creation of an object is not allowed). 6. Static classes are not inheritable.

Inheritance:

Acquiring the properties from parent class to child class. 1. Inheritance leads to code reusability. 2. Inheritance saves memory. Types in Inheritance: i) ii) iii) iv) v) Single level inheritance Multi level inheritance Hierarchal inheritance Multiple inheritance Hybrid inheritance (not possible in C#,vb.net)

Hybrid inheritance is not supported by C#.Net,Vb.net Multiple Inheritances are possible with interfaces.

Working with interfaces

Interfaces: 1. Interface contains only abstract methods, hence it is fully abstract. 2. Abstract class contains abstract methods and non-abstract methods. Hence it is partially abstract. 3. Interface supports a structure like multiple inheritances. 4. Interfaces are not instantiable, but the references can be created. 5. Syntax: interface interface_name { void method1(); void method2(); public abstract void method3(); error } 6. By default all the methods in interface are public abstract.

Dos and Donts in Inheritance C1: C2 C1:C2,C3error (solution is interfaces) (C1: i1 C1:i2) C1:i1, i2 i2:i1 i2: C1error C2: C1, i1 C2: i1, C1error ..

Partial class: When you need a class to be implemented in many locations with same class names, then those classes need to be declared as partial classes. partial class Test { Functionality1 } partial class Test

{ Functionality2 }

Access Specifiers: 1) 2) 3) 4) 5) Public Private Protected Internal Protected Internal

Base keyword: Base key word is used to access the base class data without creating any object to the derived class. Sealed Class:

1) Sealed is a keyword 2) Sealed classes are not inheritable 3) Whenever a class is providing full functionality then recommended declaring that class as sealed.

Sealed Class A { } Class B:A--error (sealed class is not inheritable)

Abstract classes and methods:

1) Abstract is a keyword which can be used with methods and classes. 2) If a class does not provide full functionality, then it is recommended to declare the class as abstract provided that class should have atleast one abstract method. 3) A method without body is called abstract method. Syntax public abstract void methodname();--C#.Net MustOverride Sub Methodname() --Vb.Net 4) When a class contains at least one abstract/mustoverride method then that class must be declared as abstract/mustinherit 5) All the abstract/mustoverride methods must be overridden in derived class. 6) Abstract/MustInherit class provides a set of abstract/mustoverride methods which must be followed (overridden) in derived classes. 7) Abstract/MustInherit classes are not instantiable, but reference can be created. 8) Reference works with the help of child class memory.

Ex:

Shape s=new Shape()object is not possible if Shape class is abstract Shape x;reference is possible, x is not having its own memory.

Ex: Abstract_class_method

Anda mungkin juga menyukai