What is similarities/difference between an abstract class and interface?
i. Interfaces provide a form of multiple inheritance. A class can extend only one other class.
Interfaces are limited to public methods and constants with no implementation.
Abstract classes can have a partial implementation, protected parts, static methods, etc.
ii. A Class may implement several interfaces.
But in case of abstract class, a class may extend only one abstract class.
iii. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class.
Abstract classes are fast.
Similarities:
Neither Abstract classes or Interface can be instantiated.
How to define an abstract class?
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:
abstract class testAbstractClass
{
protected String myString;
public String getMyString()
{
return myString;
}
public abstract string anyAbstractFunction();
}
How to define an interface?
Interface defines the methods but does not implement them. Interface can include constants.
A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface:
public interface sampleInterface
{
public void functionOne();
public long CONSTANT_ONE = 1000;
}
What is oops?
OOP is the common abbreviation for Object-Oriented Programming.
Explain the inheritance principle.
Inheritance is the process by which one object acquires the properties of another object.
Explain the polymorphism principle.
The meaning of Polymorphism is something like one name many forms.
Polymorphism enables one entity to be used as as general category for different types of actions.
The specific action is determined by the exact nature of the situation.
The concept of polymorphism can be explained as "one interface, multiple methods".
What are encapsulation, inheritance and polymorphism?
Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse.
Inheritance is the process by which one object acquires the properties of another object.
Polymorphism is the feature that allows one interface to be used for general class actions.
OBJECT
Objects are the building blocks of OOP and are commonly defined as variables or data structures that encapsulate behavior and data in a programmed unit.
Objects are items that can be individually created, manipulated, and represent real world things in an abstract way.
CLASS
A class is a template for an object. Suppose that someone builds a paper pattern for a shirt.
All the shirts done with the same paper pattern will be identical (same design, size, etc.).
In this sample, the paper pattern is the class and the shirt is the object. To build the same exact shirt over and over, you need the paper pattern as a template.
Another great example are house plans and blueprints. The plans and blueprints define the number of rooms, the size of the kitchen, the number of floors, and more. In this real world sample, the house plans and blueprints are the class and the house is the object. In OOP you program a class as a template for a specific
DELEGATE
A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value, as in this example:
C# Copy Codepublic delegate int PerformCalculation(int x, int y);
SHADOWING
When two programming elements share the same name, one of them can hide, or shadow, the other one. In such a situation, the shadowed element is not available for reference; instead, when your code uses the shared name, the Visual Basic compiler resolves it to the shadowing element.
An element can shadow another element in two different ways. The shadowing element can be declared inside a subregion of the region containing the shadowed element, in which case the shadowing is accomplished through scope. Or a deriving class can redefine a member of a base class, in which case the shadowing is done through inheritance.
Events
An event is a way for a class to provide notifications when something of interest happens.
For example, a class that encapsulates a user interface control might define an event to occur when the user clicks on the control.
The control class does not care what happens when the button is clicked, but it does need to tell derived classes that the click event has occurred. The derived classes can then choose how to respond.
Forgot the famous last words? Access your message archive online. Click here.
No comments:
Post a Comment