Answers for "what is abstract class in c sharp"

12

what is abstract class in c sharp

when we use abstract classes? supposably we have class Car. 
you don't want to use it directly. you need extend this class. 
thats why you must declare this class as abstract. 
then you only can extend this class with all methods.
Posted by: Guest on October-16-2021
11

what is abstract class in c#

Abstract class: is a restricted class that cannot be used to create objects 
(to access it, it must be inherited from another class).
Posted by: Guest on September-29-2020
1

c# abstract class and interfaces

// ------------------- ABSTRACT vs INTERFACES ------------------------ //
// Both are used to define the architecture of the application


// ---------- ABSTRACT CLASS --------- //
// -> The methods can have an implementation (be defined)
// -> It's elements are private by default
// -> It can contain fields
// -> It can inherit from another abstract class or interface 
// -> It can have abstract and non-abstract methods. The abstract classes have to be implemented in the child class. 

public abstract class AbsParent { 
  
  	private int count = 0;
  
    public void foo1()    
    {
    	Console.WriteLine("Hi there, I'm a normal method that will be inherited!");
    }
  
    public abstract void foo2();  // You can't have an implementation for this abstract method 
  
    public virtual void foo3()    
    {
    	Console.WriteLine("Hi there, I'm a virtual method!");
    }  
} 


// --------- INTERFACES --------- //
// -> It's not a class, it's an entity
// -> The methods can't have an implementation
// -> It's elements are public by default and can't have acesso modifiers 
// -> It can't contain fields like for example, "private int count", but it can contain properties 
// -> It can only inherit from another interface 
// -> A class can inherit from multiple interfaces
// -> A class inheriting from an interface, has to implement all it's methods 

public interface IParent { 
  
  string myProterty{get; set;} 
  
  void foo1(); // You can't have an implementation for these methods
  void foo2(); 
  
} 



// -------- INHERITANCES --------- // 

// class 'AbsParent' inherit in child class 'Child1' 
public class Child1 : AbsParent { 
  
    public override void foo2()    // The implementation of the AbsParent abstract methods is mandatory 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 
    
  
  	public override void foo3() 
    { 
        Console.WriteLine("Class name is Child1"); 
    } 
} 
  

// class 'IParent' inherit in another child class 'Child2' 
public class Child2 : IParent { 
 
    public void foo1()    // The implementation of all the IParent methods is mandatory 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 
    
  
  	public void foo2() 
    { 
        Console.WriteLine("Only in the child/derived class, can I be defined!"); 
    } 

} 



// ---------- MAIN --------- //
public class main_method { 
  
    // Main Method 
    public static void Main() 
    { 
        AbsParent obj = new AbsParent();  // ERROR!! You can't create an object of an abstract class
                                          // it is used only for inheritance purposes. The same logic 
                                          // goes to interfaces.
    } 
}
Posted by: Guest on September-24-2020

Code answers related to "what is abstract class in c sharp"

Browse Popular Code Answers by Language