Você está na página 1de 14

Points to be Discussed

Inheritance

Interfaces

Inheritance

Inheritance
The mechanism of deriving a new class from an

old one is called inheritance. Another term used for inheritance is reusability. The old class is known as the base class or the super class or parent class and the new one is called the sub class or derived class or child class. Inheritance may take different forms: 1. Single Inheritance:

Class Inheritance
2. Multilevel Inheritance:

3. Multiple Inheritance:

Class Inheritance
4. Hierarchical Inheritance:

Overriding methods
In a class hierarchy, when a method in a subclass

has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.

Abstract Class
An abstract class is a class that is declared

abstractit may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub-classed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void moveTo(double deltaX, double deltaY);

Abstract Class
If a class includes abstract methods, the class itself

must be declared abstract, as in: public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); } When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

Interfaces

Interfaces
An interface is basically a kind of class. Like

classes, interfaces contains methods and variables but with a major difference. All the members of the interface are implicitly public and abstract. This means that interfaces do not specify any code to implement these methods. interface InterfaceName { variables declaration; methods declaration;

Extending interfaces
Like classes, interfaces can also be extended. An

interface can be sub-interfaced from other interfaces. interface name2: name1 { body of name2 }

Implementing Interfaces
Interfaces are used as superclasses whose

properties are inherited by classes. class classname: interfacename { body of classname }

using System; interface Area { double Compute(double x); } class Square: Area { public double Compute(double x) { return (x*x); }} class Circle: Area { public double Compute(double x) { return (x*x); }}

class InterfaceTest2 { public static void Main() { Square sqr=new Square(); Circle cir=new Circle(); Area area; area= sqr as Area; Console.WriteLine(Area of Square= +area.Compute(10.0)); area= cir as Area; Console.WriteLine(Area of Circle= +area.Compute(10.0)); }

Você também pode gostar