Abstract Class - >>>>>>>>>>>>>>>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestOOPSConcept
{
/// <summary>
/// The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
/// we can call the non abstract method from inherited class using base keyword.
/// if i don't declare any virtual or abstract with function, then inherited class shows warning with that function, then we can use New keyword for hiding
/// We can't create the instance of abstract class or interface.
/// we must implement abstract method in derived class.
/// Abstract class may contain static method.
/// </summary>
abstract class ClassA
{
public int a = 100, b = 10, totalsum, totalsubtract, totalmultiply;
public abstract int Sum(); // Abstract methods have no implementations
public virtual int Subtract()
{
return totalsubtract = a - b;
}
public abstract int Multiply();
public static void Test()
{
Console.WriteLine("Abstract class may contain static method");
}
}
abstract class ClassB : ClassA
{
public override int Sum()
{
return totalsum = a + b;
}
public override int Subtract()
{
totalsubtract = base.Subtract();
return totalsubtract - 40;
}
}
class MyClass : ClassB
{
public override int Sum()
{
return totalsum = a + b + 50;
}
public override int Multiply()
{
return totalmultiply = a * b +80;
}
}
class MainClass
{
//static void Main(string[] args)
//{
// MyClass obj = new MyClass();
// int x = obj.Sum();
// Console.WriteLine("Sum: " + x);
// int y = obj.Subtract();
// Console.WriteLine("Sub: " + y);
// int z = obj.Multiply();
// Console.WriteLine("Multiply: " + z);
// ClassB.Test();
// Console.Read();
// }
}
}
Interface ------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestOOPSConcept
{
/// <summary>
/// If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
/// if u declare public modifier with method in interface, then error comes: by default interface members are public.
/// We can't create the instance of abstract class or interface.
/// No need to override the interface methods.
///
///
/// 1. ExampleClass ex = new ExampleClass(); - When we are instantiating the class by specifying the object as the class name,
/// we will be able to call methods for the interface that is implicitely implemented. In this case IExampleClass2.
///
///
/// 2. IExampleClass1 ex1 = new ExampleClass(); - If we want to call the method that is explicitely implemented,
/// we need to specify the object as the interface name and instantiate the class then we will be able to call the method of this interface. In this case IExampleClass1.
///
///
///3. IExampleClass2 ex2 = new ExampleClass(); - You can call methods of the interface
/// that is implicitely implemented by specifying the name of the Interface as the object name (The way we have done in the 2nd point).
/// This will give the same result as the 1st point above.
/// </summary>
interface interfaceA
{
void Sum();
void Subtract();
}
interface interfaceB
{
void Sum();
void Sum(string abc);
}
interface interfaceC : interfaceA, interfaceB
{
void Multiply();
}
class interfaceClass : interfaceC
{
public void Sum()
{
Console.WriteLine("Implement sum1 function.");
}
public void Subtract()
{
Console.WriteLine("Implement Subtract function.");
}
public void Sum(string abc)
{
Console.WriteLine("Implement sum2 function.");
//return abc;
}
void interfaceB.Sum() // Explicitally call
{
Console.WriteLine("Interface B call sum method");
}
public void Multiply()
{
Console.WriteLine("Implement multiply function.");
}
}
class MaininterfaceClass
{
//static void Main(string[] args)
//{
//interfaceClass objinterfaceClass = new interfaceClass();
//objinterfaceClass.Sum();
//objinterfaceClass.Sum("123");
//objinterfaceClass.Subtract();
//objinterfaceClass.Multiply();
//interfaceB objinterfaceB = new interfaceClass();
//objinterfaceB.Sum();
//Console.Read();
// }
}
}
No comments:
Post a Comment