public abstract class myBase{//If you derive from this class you must implement this method. notice we have no method body here eitherpublic abstract void YouMustImplement();
//If you derive from this class you can change the behavior but are not required topublic virtual void YouCanOverride(){}}
public class MyBase{//This will not compile because you cannot have an abstract method in a non-abstract classpublic abstract void YouMustImplement();}
public class BaseClass{public void SayHello(){Console.WriteLine("Hello");}
public virtual void SayGoodbye(){Console.WriteLine("Goodbye");}
public void HelloGoodbye(){this.SayHello();this.SayGoodbye();}}
public class DerivedClass : BaseClass{public new void SayHello(){Console.WriteLine("Hi There");}
public override void SayGoodbye(){Console.WriteLine("See you later");}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace TestOO{class Program{static void Main(string[] args){BaseClass _base = new BaseClass();Console.WriteLine("Calling virtual method directly");_base.SayHello();Console.WriteLine("Calling single method directly");_base.SayGoodbye();
DerivedClass _derived = new DerivedClass();Console.WriteLine("Calling new method from derived class");_derived.SayHello();Console.WriteLine("Calling overrided method from derived class");_derived.SayGoodbye();
DerivedClass2 _derived2 = new DerivedClass2();Console.WriteLine("Calling new method from derived2 class");_derived2.SayHello();Console.WriteLine("Calling overrided method from derived2 class");_derived2.SayGoodbye();Console.ReadLine();}}
public class BaseClass{public void SayHello(){Console.WriteLine("Hello\n");}public virtual void SayGoodbye(){Console.WriteLine("Goodbye\n");}
public void HelloGoodbye(){this.SayHello();this.SayGoodbye();}}
public abstract class AbstractClass{public void SayHello(){Console.WriteLine("Hello\n");}
//public virtual void SayGoodbye()//{// Console.WriteLine("Goodbye\n");//}public abstract void SayGoodbye();}
public class DerivedClass : BaseClass{public new void SayHello(){Console.WriteLine("Hi There");}
public override void SayGoodbye(){Console.WriteLine("See you later");}}
public class DerivedClass2 : AbstractClass{public new void SayHello(){Console.WriteLine("Hi There");}// We should use the override keyword with abstract types//public new void SayGoodbye()//{// Console.WriteLine("See you later2");//}public override void SayGoodbye(){Console.WriteLine("See you later");}}}
----------CODE--------------
public abstract class BaseClass{public int MyProperty { get; set; }protected abstract void MyAbstractMethod();
public virtual void MyVirtualMethod(){var x = 3 + 4;}
}public abstract class myClassA : BaseClass{public int MyProperty { get; set; }//not necessary to implement an abstract method if the child class is also abstract.
protected override void MyAbstractMethod(){throw new NotImplementedException();}}public class myClassB : BaseClass{public int MyProperty { get; set; }//You must have to implement the abstract method since this class is not an abstract class.
protected override void MyAbstractMethod(){throw new NotImplementedException();}}
public interface ExampleInterface {
// public void MethodBodyInInterfaceNotPossible(){// }void MethodInInterface();
}
public abstract class AbstractClass {public abstract void AbstractMethod();
// public abstract void AbstractMethodWithBodyNotPossible(){//// };
//Standard Method CAN be declared in AbstractClasspublic void StandardMethod(){System.out.println("Standard Method in AbstractClass (super) runs");}}
public class ConcreteClassextends AbstractClassimplements ExampleInterface{
//Abstract Method HAS TO be IMPLEMENTED in child class. Implemented by ConcreteClass@Overridepublic void AbstractMethod() {System.out.println("AbstractMethod overridden runs");}
//Standard Method CAN be OVERRIDDEN.@Overridepublic void StandardMethod() {super.StandardMethod();System.out.println("StandardMethod overridden in ConcreteClass runs");}
public void ConcreteMethod(){System.out.println("Concrete method runs");}
//A method in interface HAS TO be IMPLEMENTED in implementer class.@Overridepublic void MethodInInterface() {System.out.println("MethodInInterface Implemented by ConcreteClass runs");
// Cannot declare abstract method in a concrete class// public abstract void AbstractMethodDeclarationInConcreteClassNotPossible(){//// }}}