Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is use of Parent object instantiating with child class

Please tell me what is of parent object instantiating with child class like:

 public class A
    {
        public A()
        {
            Console.WriteLine("A");
        }
        public virtual void method()
        {
            Console.WriteLine("AM");
        }

    }
    public class B : A
    {
        public B()
        {
            Console.WriteLine("B");

        }
        public new void method()
        {
            Console.WriteLine("BM");

        }
        public void method1()
        {
            Console.WriteLine("BM1");
        }
    }

 class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();// what is use of it?
            obj.method();              
            Console.Read();
        }
        private void methodP1()
        {

        }
    }

please tell me what is use of Parent obj = new Child(); as i we can only call to only public methods of parent class which is possible using Parent obj = new Parent();

is it possible: Child obj = new Parent() ?

like image 832
Dr. Rajesh Rolen Avatar asked Jul 02 '11 16:07

Dr. Rajesh Rolen


People also ask

What is use of parent reference and child object?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.

Can a child class instantiate a parent class?

Following the object oriented methodology, a child class can be instantiated as an object that can inherit the attributes of the parent class. This is the process of inheritance, much like how a benefactor can pass on their wealth or assets to a beneficiary.

Why do we instantiate an object of a class?

An instance of an object can be declared by giving it a unique name that can be used in a program. This process is known as instantiation. A class can also be instantiated to create an object, a concrete instance of the class. The object is an executable file that can run on a computer.

Can I call parent method with child class object?

If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.


2 Answers

please tell me what is use of Parent obj = new Child(); as i we can only call to only public methods of parent class which is possible using Parent obj = new Parent();

This is the basis for polymorphism: Imagine you have several child classes that inherit from you parent class. You want to use all these child classes through the interface / methods defined on your parent class, without worrying about the implementation details in each child class (each might do something different, but with the same overall semantics).

This is possible because the child class has a IS A relationship with its parent class since child inherits from parent.

like image 154
BrokenGlass Avatar answered Nov 10 '22 03:11

BrokenGlass


In your example, B is-a A, but A is-not-a B.

The above is of use when the code using the reference to B can only understand (or needs to understand) types of A. B is a specialisation of A. Imagine something like (pseudo-code)

Shape s;
if (x == 1) {
  s = new Square();
}
else {
  s = new Triangle();
}
// following code knows only about Shapes
s.draw();
s.calculateArea();

The following code doesn't need to know if s is a square or a triangle, just that it's a shape. What use is that ? If you call s.draw(), the shape itself determines how it's going to look. The code calling it doesn't know or care.

This is a key point of object-oriented programming. Asking objects to do things for you rather than determine yourself what's needed.

Note that your final question doesn't intuitively make sense when using this example.

Shape s = new Square(); // fine

vs

Square s = new Shape(); // can you instantiate a "shape" and why then do you decide it's a square?

like image 37
Brian Agnew Avatar answered Nov 10 '22 03:11

Brian Agnew