Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# creating an instance of a class and derived class

I tried the below code in my c# program. This is just for learning the OOPs Concepts.

 class a
    {      
        public void testa()
        {
        }
    }
    class b:a
    {     
        public void testb()
        {
        }
    }

  a a1 = new b();

  b b1 = new a();

I have the following queries regarding the code mentioned above.

  1. Why Im getting error in 2nd line?
  2. what is meant by a a1=new b();
  3. why a1.testb() is not accessible even though constructor of b is assigned to a1?
  4. what is the difference between a a1=new a() and a a1=new b()?
like image 892
Tom Cruise Avatar asked Oct 16 '25 14:10

Tom Cruise


2 Answers

1) if you mean this line:

b b = new a();

it is because every b is a but not every a is b

2-3) what is meant by a a1=new b(); why a1.testb() is not accessible even though constructor of b is assigned to a1?

it means that you create object of b class but get reference to it as a(you can treat it trough this reference without casting only as a even it is b)

like image 119
Guru Stron Avatar answered Oct 18 '25 03:10

Guru Stron


I change your class and method names to real world equivalents to better understanding. I name a as Animal, testa as Eat, b as Human, and testb as Talk. So we have:

class Animal {      
    public void Eat() { }
}

class Human : Animal {     
    public void Talk() { }
}

Animal a1 = new Human();

Human b1 = new Animal();

OK, return to your questions.

1) You get error on second line, because every animal is NOT a human. Is it?

2) a1=new b() according to our new naming conventions, turns to a1 = new Human which means Animal a1 = new Human. So, that's correct, because human is a kind of animal.

3) a1.testb() according to our new naming conventions, turns to a1.Talk(). Well, a1 is an animal (Animal a1), and we can not expect an animal to talk.

MORE:

Think a class is a group of attributes and behaviors. For example, we have a group named Animal that defines an Eat behavior. And another group named Human that extends Animal group and also it has its own behavior named Talk. As we know, Human has its super-group behaviors too -e.g. Eat in this example-.

When we have an instance of group Animal, we can expect it to eat. But we can not ask it to talk. It's not possible. In the other hand, every item that we choose from group Human, is actually an Animal. So we can ask him to eat.

When we have a Human instance, we can ask him to behave as a Human and as an Animal too. I mean, we can ask him to Talk and we can ask him to Eat too. Every Human can be located in human group, and in animal group.

1) When we say Human b1 = new Animal(); it says exactly: pick up an item from Animal group - right part - and put it in Human group - left part - which is not possible. Because there are a lot of animals which are not human.

2) When we say Animal a1 = new Human: pick up an item from Human group - right part - and put it in Animal group - left part - which is easily possible.

3) When we say a1.Talk(), we expect an Animal to talk. I mean we expect an Animal to act a Human behavior which is not possible.

like image 38
amiry jd Avatar answered Oct 18 '25 04:10

amiry jd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!