Professional Documents
Culture Documents
Oops Ssss
Oops Ssss
Inheritance is a way to form new classes (instances of which are called objects) using classes that
have already been defined.
Once a behavior (method) or property is defined in a super class (base class),that behavior or
property is automatically inherited by all subclasses (derived class).
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
}
Output:
Parent Constructor.
Child Constructor.
Parent Class.
Encapsulation :
Encapsulation is a procedure of covering up of the data & functions into a single unit called as class
An encapsulated object is often called an abstract data type.
Encapsulation can protect your data from accidental corruption.
Rather than defining the data in the form of public, we can declare those fields as private.
Code Example :
code.
From the above we can see the use of Encapsulation by using properties. The property has two accessor get and set. The get
accessor returns the value of the some property field. The set accessor sets the value of the some property field with the
contents of "value". Properties can be made read-only. This is accomplished by having only a get accessor in the property
implementation.
Abstraction :
Abstraction defines way to abstract or hide your data and members from outside world.
Classes use the concept of abstraction and are defined as a list of abstract attributes.
Simply speaking Abstraction is hiding the complexities of your class or struct or in a generic term
Type from outer world.
Access Modifier
Private
Protected
Only members within the same type. (default for type members)
Only derived types or members of the same type.
Only code within the same assembly. Can also be code external to object as long as
it is in the same assembly. (default for types)
Either code from derived type or code in the same assembly. Combination of
protected OR internal.
Any code. No inheritance, external type, or external assembly restrictions.
Internal
Protected internal
Public
Code Example :
namespace AbstractionExample
{
public abstract class Shape
{
private float _area;
Advantages of abstraction are the hiding of implementation details, component reuse, extensibility,
and testability. When we hide implementation details, we reveal a cleaner, more comprehensible and
usable interface to our users. We are separating our interface from our implementation, and this
makes component reuse more practical. Many, if not all of the object-oriented concepts we have
discussed throughout this document play a role in the abstraction principle. Working together, their
end goal is the same, to produce software that is flexible, testable, maintainable, and extensible.
Polymorphism :
May 11, 2011
Method with same name but with different arguments is called method overloading.
class A1
{
void hello()
{
Console.WriteLine("Hello");
}
void hello(string s)
{
Console.WriteLine("Hello {0}", s);
}
}
Method overriding occurs when child class declares a method that has the same type arguments as
a methoddeclared by one of its superclass.
class parent
{
virtual void hello()
{
Console.WriteLine("Hello from Parent");
}
}
Interface :
May 11, 2011
Interface is nothing but an contract of the system which can be implemented on accounts.
.Net doesn't support the multiple inheritance directly but using interfaces we can achieve
multiple inheritance in .net.
class Program
{
interface BaseInterface
{
void BaseInterfaceMethod();
}
interface DerivedInterface : BaseInterface
{
void DerivedToImplement();
}
class InterfaceImplementer : DerivedInterface
{