0

I am new to development and beginner. I am learning OOP in C#. I want to know following concepts:-

  1. Why Derived class object is assigned to base class object, But its opposite is not?
  2. What is the memory allocation flow between derived and base class objects in this scenario?
//I have done this practically

// This is base class

Student student = new Student();

//This is derived class
SchoolStudent schoolStudent = new SchoolStudent();
//derived class object is assigning to base class.. it is ok for the complier
student = schoolStudent;

//base class object is assigning to derived class... complier error
schoolStudent = student;
3
  • 6
    Do you think that every student is schoolStudent ? Commented Apr 4, 2014 at 13:06
  • 6
    A dog is an animal, but an animal is not necessarily a dog. Commented Apr 4, 2014 at 13:07
  • Yes sir i agree with you both i know schoolStudent = (SchoolStudent)student; if i do Explicit casting then there is no problem but i want to know about little depth and creation of objects against these statements Commented Apr 4, 2014 at 13:50

2 Answers 2

5

1) You assign derived object type to it's base for scallablity of your app. The actual type remains derived, but it is presented in the code like base class. You act only on base type object, where it is possible, so don't have all that different object types all arround of your code. To may work that smoothly you use Polymorphism

Human h = new Student(); 

Student, Mechanic, Doctor, Kid are all Humans, so this becomes a tree where Human is a root. Doing that backwards means roll that tree up side down. Who is a root now ? Your code hierarchy breaks.

2) The construction sequence starts from the base class up to the upper level. So in example above: first will be called constructor of Human and after of Student.

Sign up to request clarification or add additional context in comments.

Comments

3

Why do you write a subclass? To add stuff. Different subclasses will add different stuff (or you'd only need one). If you have a reference to the base class, it might refer to an instance of the subclass you have in mind, but it might refer to something else entirely. And what happens then? You'll be trying to interact with a feature the object doesn't even have. Dynamic languages like JavaScript let you do things like that, but strongly typed languages like C# don't, by design. On a large project, this turns out to be very useful. The compiler is keeping an eye on what goes where, so you as the programmer don't have to spend your life writing code that needs to figure out what things are.

class Mammal {
    public void Nurse() { /* */ }
}

class Dog : Mammal {
    public void ChaseCar() { /* */ }
}

class Cat : Mammal {
    public void ClimbTree() { /* */ }
}

Mammal myPet = new Cat();

//  The actual object which myPet refers to may not be a Dog, so the 
//  compiler won't let you do this. The declared type of the reference, 
//  Mammal, is an implicit guarantee that, unless it's null, it's 
//  referring to something that can be treated as a Mammal. 
myPet.ChaseCar();

//  You can force it to try, but in this case, myPet does not refer
//  to a Dog, so this call will fail at runtime and throw an exception. 
((Dog)myPet).ChaseCar();

//  But C# does let you find out what you've really got. In this case, 
//  myPet is not Dog, so this block won't execute. But if it were a 
//  Dog, it would. In the general case, that can only be determined at 
//  runtime. In principle the compiler could make an exception for 
//  cases where the actual runtime type of myPet can be known at 
//  compile time -- as in this example code -- but my guess is that 
//  the compiler designers would see that as adding more complexity 
//  and confusion than it would be worth. 
if ( myPet is Dog ) {
    Dog myDog = myPet as Dog;
    myDog.ChaseCar();
}

Consider this case: Anybody could be passing anything to this method. animal could actually be a Baboon or a Giraffe.

public void CarDrivesBy(Mammal animal) {
    //  What is animal? At this point, you can't know, the compiler 
    //  can't know, so it won't accept this. 
    animal.ChaseCar();
}

If you want to treat animal as a dog, you can ask the compiler to help guarantee that you're getting a Dog, and then treat it as one with (relative) confidence:

public void CarDrivesBy(Dog animal) {
    //  Now the compiler knows what you want, and enforces that on 
    //  anybody who calls this method. 
    animal.ChaseCar();
}

Also, just parenthetically, the word "scope", as it is used in programming, refers to a completely unrelated concept.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.