Pages

Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Tuesday, May 7, 2013

What is object slicing?

When a derived class object is assigned to base class object, only base class properties copied to base class object and leave the derived class properties.This is called object slicing.

Ex:Class Base
{
 public: int i;
 }
 
 class Derived :Base
 {
 public : intj;
 }
 
 int main()
 {
    Base Bobj;
    Derived Dobj;
    Bobj=Dobj;//Here Dobj contains both i and j
    //But only i is copied to Bobj
 }

Wednesday, April 17, 2013

Inheritance

 1.Simple Inheritance

 class baseClass
    {
        public baseClass()
        {
            Console.WriteLine("Base Class Constructor Called");
        }
        public void print()
        {
            Console.WriteLine("Base Class Print Method Called");
        }
    }
    class derivedClass:baseClass
    {
        public derivedClass()
        {
            Console.WriteLine("Derived Class Constructor Called");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            derivedClass obj = new derivedClass();
            obj.print();
            Console.ReadKey();
        }
    }
   
    2.Use of base and new keywords in Inheritance
   
     class Program
    {
        class baseClass
        {
            public baseClass()
            {
                Console.WriteLine("Base Class Constructor Called");
            }
            public baseClass(String newString)
            {
                Console.WriteLine(newString);
            }
            public void Print()
            {
                Console.WriteLine("Base Class Print Method Called");
            }
        }
        class derivedClass:baseClass
        {
            public derivedClass():base("From Child")
            {
                Console.WriteLine("Child Constructor Called");
            }
            public new void Print()
            {
                base.Print();
                Console.WriteLine("Child Class Print Method Called");
            }

        }
        static void Main(string[] args)
        {
            derivedClass obj = new derivedClass();
            obj.Print();
            ((baseClass)obj).Print();
            Console.ReadKey();
        }
    }
   
    keyword base call the base class constructor with the matching parameter list
    Using the base keyword, you can access any of a base class public or protected class members
    Another way to access base class members is through an explicit cast
    Notice the new modifier on the Child class print() method. This enables this method to hide the Parent class print() method and explicitly states your intention that you don't want polymorphism to occur.

Thursday, March 28, 2013

What is Abstract method?


Abstract method doesnot provide implementation and forces derived class to override the method.

There are following ways we can make use of Abstract Classes and Methods

1.We can create Non-Abstract methods inside Abstract Class, and Inherited Class can use this method.

See the Example below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractMethod1
{
    abstract class Test
    {
        public void MethodInAbstract()
        {
            Console.WriteLine("NonAbstract Method Called");
            Console.ReadKey();
        }
    }

    class Inherit : Test
    {
    }
    class Program
    {
        static void Main(string[] args)
        {
            Inherit obj = new Inherit();
            obj.MethodInAbstract();
        }
    }
}

In above program Test is Abstract Class MethodInAbstract is Non Abstractmethod. Another Class Inherit inherited the Abstract Class Test. Now we created object for
Inherit and called the MethodInAbstract method.

More Observations:
Abstract class will starts with keyword 'Abstract'
We can not create object for abstract class
Test obj=new Test(); it will show the error message 'Cannot create an instance of the abstract class or interface 'AbstractMethod1.Test'

2.Abstract method is meant by method which is not having any body, Who ever class inherited this Abstract Class that class must be override the
abstract methods.

Ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractMethod2
{
    abstract class Myclass
    {
       public abstract void AbstractMethod();
    }

    class MyInheritedClass : Myclass
    {
        public override void AbstractMethod()
        {
            Console.WriteLine("Abstract method implemented");
            Console.ReadKey();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyInheritedClass obj = new MyInheritedClass();
            obj.AbstractMethod();
        }
    }
}

In above AbstractMethod() is abstract method, which was implemented by derived class MyInheritedClass.

More Observations:
Abstract method must be public and starts with abstract keyword
If we not implement the abstract method in derived class we will get error message ''AbstractMethod2.MyInheritedClass' does not implement inherited abstract member 'AbstractMethod2.Myclass.AbstractMethod()'

Saturday, March 23, 2013

What is Object?

Object is a real world entity, object will be existing physically which means it will occupy memory where as Class is a logical unit.

If we want to use class methods or variables we need to create object, there is no other way to use class methods without creating object.

Ex:

 class A
    {
       public int sum(int a,int b)
        {
            return a + b;
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            A obj = new A(); // Creating Object of Class A to access public methods of ClassA
            int sum = obj.sum(5, 4);
            Console.WriteLine(sum);
            Console.ReadKey();
           
        }
    }

In Class 'Program' used method 'sum' of another Class A by creating object of Class A

What is Class?

Class is a blue print or template which is having state and behavior and which supports objects of which own types.

Class must support oops concepts ie.
1.Abstraction
2.Encapsulation
3.Inheritance
4.Polymorphism