Answers for "abstract class vs interface c# examplea"

3

abstract class vs interface

***************************Abstract class*****************************
1) Abstract class can have abstract and non-abstract methods.
2) Abstract class doesn't support multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.
4) Abstract class can provide the implementation of interface.
5) The abstract keyword is used to declare abstract class.
6) An abstract class can extend another Java class and implement multiple Java interfaces.
7) An abstract class can be extended using keyword "extends".
8) A Java abstract class can have class members like private, protected, etc.
9)Example:
public abstract class Shape{
public abstract void draw();
}


*****************************Interface********************************
1) Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Interface supports multiple inheritance.
3) Interface has only static and final variable
4) Interface can't provide the implementation of abstract class.
5) The interface keyword is used to declare interface.
6) An interface can extend another Java interface only.
7) An interface can be implemented using keyword "implements".
8) Members of a Java interface are public by default.
9) Example:
public interface Drawable{
void draw();
}
Posted by: Guest on May-13-2021
2

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
3

abstract class vs interface

***************************Abstract class*****************************
1) Abstract class can have abstract and non-abstract methods.
2) Abstract class doesn't support multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.
4) Abstract class can provide the implementation of interface.
5) The abstract keyword is used to declare abstract class.
6) An abstract class can extend another Java class and implement multiple Java interfaces.
7) An abstract class can be extended using keyword "extends".
8) A Java abstract class can have class members like private, protected, etc.
9)Example:
public abstract class Shape{
public abstract void draw();
}


*****************************Interface********************************
1) Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Interface supports multiple inheritance.
3) Interface has only static and final variable
4) Interface can't provide the implementation of abstract class.
5) The interface keyword is used to declare interface.
6) An interface can extend another Java interface only.
7) An interface can be implemented using keyword "implements".
8) Members of a Java interface are public by default.
9) Example:
public interface Drawable{
void draw();
}
Posted by: Guest on May-13-2021
2

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 "abstract class vs interface c# examplea"

Browse Popular Code Answers by Language