Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
1)Explain bold Line what does its mean.
            MainClass obj = new DrivedClass();
            obj.ID = 222;
            obj.Name = "Changed";
            obj.addNum(4, 3);
            Console.WriteLine(obj.Name);
            MainClass obj1 = new MainClass();
            
            //DrivedClass obj2 = new MainClass();
            
            Console.ReadKey();
        }
    }

    public class MainClass
    {
        public Int32 ID;
        public String Name;
       
        public MainClass()
        {
            //do some things
        }
        public Int32 addNum(Int32 a,Int32 b)
        {
            return 3 * 5;
        }
    }
    public class DrivedClass : MainClass
    {
        public DrivedClass()
        {
            ID = 123;
            Name = "Qamar";
        }

        public void add()
        {

        }
    }
}

Explain why we not access drive class method in this scenario.we access only method , that are defined in only mainclass why we not access drive class method.
Why the constructor of drive class is first calling then mainclass and again drive class.
Posted
Updated 22-Oct-14 2:15am
v2
Comments
E.F. Nijboer 22-Oct-14 8:12am    
Nice school assignment... good luck with that.
Richard Deeming 22-Oct-14 9:49am    
You'd have more luck understanding this if you listened properly in your lessons.

It's a "derived" class, not a "drived" or "drive" class.

Quote:
MainClass obj = new DrivedClass();

In the above line obj is declared of MainClass type. Then it is assigned with a DrivedClass instance.
That's legal (indeed the compiler doesn't complain), however, due to its very type, you cannot directly invoke on obj the DrivedClass methods. You may however write something like:
C#
DrivedClass  sameobj = obj as DrivedClass;
sameobj.Add();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Oct-14 10:36am    
Here is one mistake here: using "as" only makes sense if you then compare the result (sameobj) with null, to use it only of the cast was successful. This is dynamic cast. It has some performance cost, so it should only be used when you don't know exactly that the DerivedClass is really derived. (Using such techniques is good in a pinch, but this is the violation of OOP, should not replace OOP.)

If you can surely assume that the runtime object is of derived class, you should use "regular" cast, (DerivedClass)obj.

—SA
CPallini 22-Oct-14 13:44pm    
Yes, you may as well use a regular cast in this case.
This can be fully explained by reading this article - Introduction to inheritance, polymorphism in C#[^] or any other starter book on C#.

(P.S. Next time tag your question properly)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900