why function override has super class reference variable and sub class object ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mukesh KumarPosted Nov 26, 2015, 7:19 AM
Method Overloading means creating multiple methods in the class having same name but different signatures (Parameters). It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures.
Compiler automatically calls required method to check number of parameters and their type which are passed into that method.
Rajeesh MenothPosted Nov 20, 2015, 7:40 AM
Francis SusaimichaelPosted Nov 20, 2015, 7:29 AM
Raja TPosted Nov 20, 2015, 6:55 AM
Hi,
newandvirtual/override.You can imagine, that a class, when instantiated, is nothing more than a table of pointers, pointing to the actual implementation of its methods. The following image should visualize this pretty well:
Now there are different ways, a method can be defined. Each behaves different when it is used with inheritance. The standard way always works like the image above illustrates. If you want to change this behavior, you can attach different keywords to your method.
1. Abstract classes
The first one is
abstract.abstractmethods simply point to nowhere:If your class contains abstract members, it also needs to be marked as
abstract, otherwise the compiler will not compile your application. You cannot create instances ofabstractclasses, but you can inherit from them and create instances of your inherited classes and access them using the base class definition. In your example this would look like:If called, the behavior of
ShowInfovaries, based on the implementation:Both,
Students andTeachers arePersons, but they behave different when they are asked to prompt information about themselves. However, the way to ask them to prompt their information, is the same: Using thePersonclass interface.So what happens behind the scenes, when you inherit from
Person? When implementingShowInfo, the pointer is not pointing to nowhere any longer, it now points to the actual implementation! When creating aStudentinstance, it points toStudentsShowInfo:2. Virtual methods
The second way is to use
virtualmethods. The behavior is the same, except you are providing anoptional default implementation in your base class. Classes withvirtualmembers can be instanciated, however inherited classes can provide different implementations. Here's what your code should actually look like to work:The key difference is, that the base member
Person.ShowInfoisn't pointing to nowhere any longer. This is also the reason, why you can create instances ofPerson(and thus it does not need to be marked asabstractany longer):You should notice, that this doesn't look different from the first image for now. This is because the
virtualmethod is pointing to an implementation "the standard way". Usingvirtual, you can tellPersons, that they can (not must) provide a different implementation forShowInfo. If you provide a different implementation (usingoverride), like I did for theTeacherabove, the image would look the same as forabstract. Imagine, we did not provide a custom implementation forStudents:The code would be called like this:
And the image for
Studentwould look like this:3. The magic `new` keyword aka "Shadowing"
newis more a hack around this. You can provide methods in generalized classes, that have the same names as methods in the base class/interface. Both point to their own, custom implementation:The implementation looks like the one, you provided. The behavior differs, based on the way you access the method:
This behavior can be wanted, but in your case it is misleading.
I hope this makes things clearer to understand for you!