• Introduction to OOPS • Class and Object • Encapsulation • Inheritance • Polymorphism • Interfaces • Delegate • Access Modifiers • Namespace Agenda
About OOPS OOPS is a programming language organized around objects (data) rather than action and logic. It is composed of self sufficient modules (“Class”), each instance (“Object”) contains information to manipulate its data (“Members”) Four main features of OOPS are • Abstraction • Encapsulation • Polymorphism • Inheritance
Object and Class • Object is an instance of a class • Object has state, behavior, and identity • Object can be created by using the NEW keyword • Communicates with other objects through message passing Object • It is a user defined data type • In OOP you program a class as a template for a specific object or groups ob objects that will always have the same features. • Other types of classes • Abstract class • Static class Class
Encapsulation Encapsulation is the process of binding/wrapping the data and methods into a single entity Encapsulation is realized through the classes. Encapsulation guarantees the integrity of the data in the object Three main parts of Object • Interface • Implementation or Behavior • Member or Instance variables
Access modifiers are used to implement encapsulation It reduces system complexity and thus increases robustness, by limiting the interdependencies Abstraction is achieved through encapsulation Object and Properties are examples for encapsulation
Inheritance Inheritance is a way to form new CLASSES using classes that have already been. Inheritance is employed to help REUSE existing code with little or no modification. The new classes, known as Sub-classes (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as Super-classes (or ancestor classes). The inheritance relationship of SUB and SUPER CLASSES gives rise to a HIERARCHY.
Inheritance Purpose • Reuse, extend, and modify the behavior defined in other classes • Allows to build a hierarchy of related classes Only single inheritance is allowed in .NET Three types of inheritance • Implementation • Interface • Visual To avoid inheritance, we need to use ‘sealed’ keyword
Polymorphism Many forms of a single object is called Polymorphism. Polymorphism is important not only to the derived classes, but to the base classes as well. It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name.
Polymorphism Contd… Polymorphism is of two types: • Compile time polymorphism/Overloading • Runtime polymorphism/Overriding Compile Time Polymorphism • Compile time polymorphism is method and operators overloading. It is also called early binding. • In method overloading method performs the different task at the different input parameters.
Polymorphism Contd… • Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. • When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype. When and why to use method overloading ? Runtime Time Polymorphism
Function Overloading Using same name for two or more functions Functions should differ with the function signature Function signature is 1.Number of parameters 2.Different data types of parameters 3.Sequence of parameters Return type is NOT part of function signature
Operator overloading Using • Static member functions using the operator keyword Syntax • <access modifier> <Class Name> operator <operator symbol> (parameter list) • { • .Do something • } Few types of operator overloading • Unary operator overloading • Binary operator overloading
Abstract class Purpose of an abstract class is to provide a common definition of a base class An abstract class cannot be instantiated Abstract classes may also define abstract methods Use abstract keyword Abstract methods have no implementation Use override keyword to implement abstract methods
Virtual members Virtual members enable derived classes to extend a base class Base class member should use virtual and derived class member should use override Fields cannot be virtual
Override virtual members in the base class Inherit base class method without overriding it Define new non-virtual implementation that hide the base class implementations The derived class can choose whether to
Interfaces Interfaces describe a group of related functionalities Interfaces are defined by using the INTERFACE keyword Non-abstract type inheriting the interface must implement all its members Interfaces contain no implementation of methods An interface cannot be instantiated directly
Interfaces Contd… Classes can inherit from more than one interface An interface can itself inherit from multiple interfaces Interfaces members are automatically public The member of class implementing interface member must be of the following • Public and non-static • Same name and signature
Difference Abstract Class Interface Abstract class may contain concrete methods Interface contains methods that must be abstract Abstract class may contain non-public members. Members in an interface are public by default whereas abstract class is used to "extends". Interface is used to "implements"; Single inheritance Multiple inheritance Abstract class can "extends" another class and "implements" multiple interfaces. Interface can "extends" another interface One class can only "extends" one super class interface is more flexible than abstract class because it "implements" multiple interfaces. Any class can extend an abstract class. only an interface can extend another interface
Delegates
Delegates Contd… Multi cast delegate • Refer to multiple methods of matching signature • Derived from System.MulticastDelegate class • Example: to call two methods on a button click event Steps to use delegates • Declare a delegate with a function signature • Instantiate the declared delegate • Use the delegate to execute a function
Access Modifiers All types and type members have an accessibility level which controls whether they can be used from other code in your assembly or other assemblies. Access modifiers are determined by the scope of visibility for variables and methods. Used to implement encapsulation and abstraction
Access Modifiers Types of access modifiers • private • public • protected • internal • internal protected Default access modifier for class is internal Default access modifier for data member is private
Namespace The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types. Within a namespace, you can declare one or more of the following types: 1.another namespace 2.class 3.interface 4.struct 5.enum 6.Delegate
namespace Name1 { class A {} } namespace Name2 { class A {} } We can also use nested namespace namespace csharp_example { // Program start class class NamespaceCE { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("This is the new C# Example Namespace."); } } } Namespace Example
OOPS – General Understanding in .NET

OOPS – General Understanding in .NET

  • 2.
    • Introduction toOOPS • Class and Object • Encapsulation • Inheritance • Polymorphism • Interfaces • Delegate • Access Modifiers • Namespace Agenda
  • 3.
    About OOPS OOPS isa programming language organized around objects (data) rather than action and logic. It is composed of self sufficient modules (“Class”), each instance (“Object”) contains information to manipulate its data (“Members”) Four main features of OOPS are • Abstraction • Encapsulation • Polymorphism • Inheritance
  • 4.
    Object and Class •Object is an instance of a class • Object has state, behavior, and identity • Object can be created by using the NEW keyword • Communicates with other objects through message passing Object • It is a user defined data type • In OOP you program a class as a template for a specific object or groups ob objects that will always have the same features. • Other types of classes • Abstract class • Static class Class
  • 5.
    Encapsulation Encapsulation is theprocess of binding/wrapping the data and methods into a single entity Encapsulation is realized through the classes. Encapsulation guarantees the integrity of the data in the object Three main parts of Object • Interface • Implementation or Behavior • Member or Instance variables
  • 6.
    Access modifiers are usedto implement encapsulation It reduces system complexity and thus increases robustness, by limiting the interdependencies Abstraction is achieved through encapsulation Object and Properties are examples for encapsulation
  • 7.
    Inheritance Inheritance is away to form new CLASSES using classes that have already been. Inheritance is employed to help REUSE existing code with little or no modification. The new classes, known as Sub-classes (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as Super-classes (or ancestor classes). The inheritance relationship of SUB and SUPER CLASSES gives rise to a HIERARCHY.
  • 8.
    Inheritance Purpose • Reuse, extend,and modify the behavior defined in other classes • Allows to build a hierarchy of related classes Only single inheritance is allowed in .NET Three types of inheritance • Implementation • Interface • Visual To avoid inheritance, we need to use ‘sealed’ keyword
  • 9.
    Polymorphism Many forms ofa single object is called Polymorphism. Polymorphism is important not only to the derived classes, but to the base classes as well. It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name.
  • 10.
    Polymorphism Contd… Polymorphism isof two types: • Compile time polymorphism/Overloading • Runtime polymorphism/Overriding Compile Time Polymorphism • Compile time polymorphism is method and operators overloading. It is also called early binding. • In method overloading method performs the different task at the different input parameters.
  • 11.
    Polymorphism Contd… • Runtimetime polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. • When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype. When and why to use method overloading ? Runtime Time Polymorphism
  • 12.
    Function Overloading Using same namefor two or more functions Functions should differ with the function signature Function signature is 1.Number of parameters 2.Different data types of parameters 3.Sequence of parameters Return type is NOT part of function signature
  • 13.
    Operator overloading Using • Staticmember functions using the operator keyword Syntax • <access modifier> <Class Name> operator <operator symbol> (parameter list) • { • .Do something • } Few types of operator overloading • Unary operator overloading • Binary operator overloading
  • 14.
    Abstract class Purpose ofan abstract class is to provide a common definition of a base class An abstract class cannot be instantiated Abstract classes may also define abstract methods Use abstract keyword Abstract methods have no implementation Use override keyword to implement abstract methods
  • 15.
    Virtual members Virtual membersenable derived classes to extend a base class Base class member should use virtual and derived class member should use override Fields cannot be virtual
  • 16.
    Override virtual membersin the base class Inherit base class method without overriding it Define new non-virtual implementation that hide the base class implementations The derived class can choose whether to
  • 17.
    Interfaces Interfaces describe agroup of related functionalities Interfaces are defined by using the INTERFACE keyword Non-abstract type inheriting the interface must implement all its members Interfaces contain no implementation of methods An interface cannot be instantiated directly
  • 18.
    Interfaces Contd… Classes caninherit from more than one interface An interface can itself inherit from multiple interfaces Interfaces members are automatically public The member of class implementing interface member must be of the following • Public and non-static • Same name and signature
  • 19.
    Difference Abstract Class Interface Abstractclass may contain concrete methods Interface contains methods that must be abstract Abstract class may contain non-public members. Members in an interface are public by default whereas abstract class is used to "extends". Interface is used to "implements"; Single inheritance Multiple inheritance Abstract class can "extends" another class and "implements" multiple interfaces. Interface can "extends" another interface One class can only "extends" one super class interface is more flexible than abstract class because it "implements" multiple interfaces. Any class can extend an abstract class. only an interface can extend another interface
  • 20.
  • 21.
    Delegates Contd… Multi castdelegate • Refer to multiple methods of matching signature • Derived from System.MulticastDelegate class • Example: to call two methods on a button click event Steps to use delegates • Declare a delegate with a function signature • Instantiate the declared delegate • Use the delegate to execute a function
  • 22.
    Access Modifiers All typesand type members have an accessibility level which controls whether they can be used from other code in your assembly or other assemblies. Access modifiers are determined by the scope of visibility for variables and methods. Used to implement encapsulation and abstraction
  • 23.
    Access Modifiers Types ofaccess modifiers • private • public • protected • internal • internal protected Default access modifier for class is internal Default access modifier for data member is private
  • 24.
    Namespace The namespace keywordis used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types. Within a namespace, you can declare one or more of the following types: 1.another namespace 2.class 3.interface 4.struct 5.enum 6.Delegate
  • 25.
    namespace Name1 { class A{} } namespace Name2 { class A {} } We can also use nested namespace namespace csharp_example { // Program start class class NamespaceCE { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("This is the new C# Example Namespace."); } } } Namespace Example