Thursday, October 26, 2017

Inheritance


 Inheritance

  • inheritance is a mechanism of establishing the relationship between two classes.
  • it is the mechanism of creating the a new class from existing class.
  • the symbol ":" is called inheritance operator.
  • only methods and instance variables are participate in inheritance 
  • constructors are participate in inheritance
Example 

    public class A
    {
           public void show()
                {
                         Console.WriteLine(" super class Show method");
                 }
    }

    public class B:A
    {
        public void display()
        {
            Console.WriteLine("Derived class Display method");
        }
    }
       class Program
        {
                  static void Main(string[] args)
                       {
                              B obj = new B();
                              obj.display();
                              Console.ReadLine();
                         }
        }


  • Inheritance always create the object for Derived class
  • If we creates the object for Derived Class then the object of Base Class will not create 
  • but in the memory allocation point of view when we create a object for Derived Class the Memory is created for "Derived Class instance variable" and "Super Class Instance variable" in stack memory of RAM.
  • But the object of Derived class is created as reference pointer in stack and the object will stored in  Heap memory at run time for instance of class B and Instance of class A.
  • The Derived class object can refer either by using Base Class name or Derived Class name
                    A obj1= new B();
                    B obj2 = new A();



1) What is UpCasting?
2) What Methods we can Access with Super Class reference?
3) Can we access Derived Class methods with super class reference?
4) What is DownCasting?



No comments: