Oops With C# Example (what , How , Where)



1.Polymorphism 

Method Overloading


polymorphism allows you define
one interface and have multiple implementations
Types of polymorphism
1) Static Polymorphism also known as compile time polymorphism
(Method Overloading)
2) Dynamic Polymorphism also known as runtime polymorphism
Method Overloading: This allows us to have more than
one method having the same name, if the parameters of methods are different in number, sequence and data types of parameters.
(Method Overloading)

class SimpleCalculator
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
}
public class Demo
{
public static void main(String args[])
{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}




Method overriding



Method overriding Declaring a method in sub class which is already
present in 
parent class is known as method overriding.
Method Overriding

  1. using System;  
  2. public class Animal{  
  3.     public virtual void eat(){  
  4.         Console.WriteLine("eating...");  
  5.     }  
  6. }  
  7. public class Dog: Animal  
  8. {  
  9.     public override void eat()  
  10.     {  
  11.         Console.WriteLine("eating bread...");  
  12.     }  
  13.       
  14. }  
  15. public class TestPolymorphism  
  16. {  
  17.     public static void Main()  
  18.     {  
  19.         Animal a= new Dog();  
  20.         a.eat();  
  21.     }  
  22. }  


2.Abstraction


Abstraction is a process where you show only “relevant”
data and “hide” unnecessary details of an object from the user.
Ex:
To give an example of abstraction we will create one superclass called Employee and two subclasses – Contractor and FullTimeEmployee. Both subclasses have common properties to share, like the name of the employee and the the amount of money the person will be paid per hour. There is one major difference between contractors and full-time employees – the tyme they work for the company. Full-time employees work constantly 8 hours per day and the working time of contractors may vary.

2.1


public abstract class Employee {

private String name;
private int paymentPerHour;

public Employee(String name, int paymentPerHour) {
this.name = name;
this.paymentPerHour = paymentPerHour;
}

public abstract int calculateSalary();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPaymentPerHour() {
return paymentPerHour;
}
public void setPaymentPerHour(int paymentPerHour) {
this.paymentPerHour = paymentPerHour;
}
}

2.2
package net.javatutorial;
public class Contractor extends Employee {

private int workingHours;
public Contractor(String name, int paymentPerHour, int workingHours) {
super(name, paymentPerHour);
this.workingHours = workingHours;
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * workingHours;
}
}

2.3

public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String name, int paymentPerHour) {
super(name, paymentPerHour);
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * 8;
}
}


Constructer




A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields:

class Car
{
  public string model;  // Create a field

  // Create a class constructor for the Car class
  public Car()
  {
    model = "Mustang"; // Set the initial value for model
  }

  static void Main(string[] args)
  {
    Car Ford = new Car();  // Create an object of the Car Class (this will call the constructor)
    Console.WriteLine(Ford.model);  // Print the value of model
  }
}

Constructor Parameters


class Car
{
  public string model;

  // Create a class constructor with a parameter
  public Car(string modelName)
  {
    model = modelName;
  }

  static void Main(string[] args)
  {
    Car Ford = new Car("Mustang");
    Console.WriteLine(Ford.model);
  }
}


Access Modifiers


ModifierDescription
publicThe code is accessible for all classes
privateThe code is only accessible within the same class
protectedThe code is accessible within the same class, or in a class that is inherited from that class. You will learn more about inheritance in a later chapter
internalThe code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter



Properties


class Person
{
  private string name; // field

  public string Name   // property
  {
    get { return name; }   // get method
    set { name = value; }  // set method
  }



Example explained

The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.
The get method returns the value of the variable name.
The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.

Inheritance
Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.

    1. Multiple Inheritance(Through Interfaces):In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces. In the image below, Class C is derived from interface A and B.
    2. Hybrid Inheritance(Through Interfaces): It is a mix of two or more of the above types of inheritance. Since C# doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In C#, we can achieve hybrid inheritance only through Interfaces.
Important facts about inheritance in C#
  • Default Superclass: Except Object class, which has no superclass, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class.
  • Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because C# does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by C#.
  • Inheriting Constructors: A subclass inherits all the members (fields, methods) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
  • Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has properties(get and set methods) for accessing its private fields, then a subclass can inherit.

Interfaces

Why And When To Use Interfaces?

1) To achieve security - hide certain details and only show the important details of an object (interface).
2) C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

Multiple interface
_________________________________________________

public interface interfaceA
{
    void DrawA();
}
public interface interfaceB
{
    void DrawB();
}
class Program
{
    static void Main(string[] args)
    {

        TestC c = new TestC();
        c.DrawA();
        c.DrawB();
        Console.ReadLine();

    }
}

class TestA : interfaceA
{
    public void DrawA()
    {
        Console.WriteLine("This is Class One");
    }
}

class TestB : interfaceB
{
    public void DrawB()
    {
        Console.WriteLine("This is Class Two");
    }
}

class TestC : interfaceA, interfaceB
{
    TestA a = new TestA();
    TestB b = new TestB();    
    public void DrawA()
    {
        a.DrawA();
    }
    public void DrawB()
    {
        b.DrawB(); 
    }   
}




Enum
An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).


class TestEmployee { public enum Month { jan,feb,mar,apr,may} public static void Main(string[] args) { int jan1, feb1, mar1; jan1 = (int)Month.jan; feb1 = (int)Month.feb; mar1 = (int)Month.mar; Console.WriteLine(jan1); Console.WriteLine(feb1); Console.WriteLine(mar1); Console.ReadLine(); } }


Comments

Popular Posts