Tuesday, March 4, 2014

Difference b/w Abstract class and Interfaces(C#)

Difference b/w Abstract class and Interfaces(C#)

.An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

·An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.

·An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.

·A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.

·Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
·Abstract classes are faster than interfaces.


Method Modifiers


There are several modifiers that can be applied to method declarations. Some of the modifiers can be combined (for example, final static).

The following table describes the method modifier keywords of the X++ language.
Modifier
Description
abstract
The method is declared but not implemented in a parent class. The method must be overridden in subclasses.
If you try to create an object from a subclass where one or more of the abstract methods belonging to the parent class have not been overridden, you will get a compiler error.
NoteNote
Classes can also be abstract. Sometimes a class represents an abstract concept, but it should not be instantiated: Only subclasses should be instantiated. Such base classes can be declared abstract. Consider the case where the programmer wants to model the concept of an account. Accounts are abstract—only derived classes (ledger accounts and so on) exist in the real world. This would be a clear case for declaring the Account class abstract.
client
Establishes the location where the method is to be executed (on the client).
Can only be used on static methods. If the method is not static, specify the location by using the class property RunOn.
display
Indicates that the method's return value is to be displayed on a form or a report. The value cannot be altered in the form or report.
The return value is typically a calculated value, for example, a sum.
For more information about the display and edit modifiers, see Using the display Method Modifier.
edit
Indicates that the method's return type is to be used to provide information for a field that is used in a form. The value in the field can be edited.
final
Indicates that the method cannot be overridden in any class that derives from its class.
public
Methods that are declared as public are accessible anywhere the class is accessible and can be overridden by subclasses. Methods that have no access modifier are implicitly public.
protected
Methods that are declared as protected can only be called from methods in the class and in subclasses that extend the class where the method is declared.
private
Methods that are declared as private can be called only from methods in the class where the private method is declared.
server
Establishes the location where the method is to be executed (on the server).
Can only be used on static methods. If the method is not static, you need to specify the location using the class property RunOn.
static
Specifies that the method is a class method and does not operate on an object.
static methods cannot refer to instance variables and are invoked by using the class name rather than on an instance of the class (MyClass::aStaticProcedure()).

Method Access Control


In X++, you use the accessor keywords publicprotected, and private to control whether the methods in other classes can call the methods on your class. The accessor keywords on methods also interact with the rules for class inheritance. The following table describes the accessor keywords you use with methods.
public
Methods that are declared as public can be called from anywhere the class is accessible. In addition, a public method can be overridden by a subclass, unless the method is declared as final.
protected
Methods that are declared as protected can be called only from the following:
  • From methods in the class.
  • From methods in a subclass of the class that contains the protected method.
    Methods that are protected can be overridden in subclasses.
private
Methods that are declared as private can be called only from methods in the class where the private method is declared. No private method can be overridden in a subclass.
When you create a new method, the default accessor keyword that appears in the code editor is private. This is the most conservative default for maximum security.
NoteNote
In the Application Object Tree (AOT), all classes under AOT > Classes are public. Explicit use of the public keyword is recommended in the classDeclaration code block for each class, but the class is public even if the public keyword is omitted.

When a method is overridden in a subclass, the overriding method must be at least as accessible as the overridden method. For example, the following X++ compiler rules apply to overriding a protected method in a subclass:
  • public method in a superclass can be overridden only by a public method in the subclass.
  • In a subclass, a public method or a protected method can override a protected method of the superclass.
  • In a subclass, a private method cannot override a protected method of the superclass.

1 comment: