+ 2

Seeking explanation on inheritance

Class B inherits from Class A. The constructor of Class A displays "a", while the class B constructor displays "b". What is the output to the screen when the code: A a = new B(); The answer is ab. my doubts is, isn't it suppose to be A a = new A() ? throughout the course, it is always left side of the equal sign and right side of the equal sign is of similar class/object. Example: Person A = New Person() Game A = New Game() Company A = New Company() However, the case above is A a = new B();

8th Jan 2021, 3:40 AM
Faris Mohamed
Faris Mohamed - avatar
18 Respostas
+ 13
Ahh... I guess you're on web and that's why you can't access the post link.. Can you access the link which leads you to the lesson on Polymorphism? The post basically clarifies the difference between two approaches... When you write, A a = new B(); If we don't know about Runtime type of object then we can hold the child class reference in parent. ā€¢ We can call only parents class specific method. ā€¢ We can't call child specific method. ā€¢ We can't call private method of parent class. But for, B b = new B(); If we know the exact runtime type of object then we can use this. ā€¢ We can call parent specific method and ā€¢ We can also call child specific method. ā€¢ "What does new actually means?" -> The 'new' operator dynamically allocates memory for an object. It's general form is: class-var = new classname() Here, 'class-var' is a variable of the class type being created and the 'classname' is the name of the class that is being instantiated.
8th Jan 2021, 4:14 AM
Minho
Minho - avatar
+ 10
A super class reference variable can hold reference of sub-class object without casting. This is the basis for polymorphism : Read in this lesson šŸ‘‡ https://www.sololearn.com/learn/CSharp/2681/?ref=app A a = new B(); you're constructing an object a that can do the job of either B or A. B is inherited from A. This way you can access all the properties and methods of A. You can also read this post šŸ‘‡ https://www.sololearn.com/post/225423/?ref=app Hope it helps!
8th Jan 2021, 3:53 AM
Minho
Minho - avatar
+ 10
Lol šŸ˜† We don't get notified.. Just go to the discuss section and sort by Most recent.. If I like the question and think I can answer then I post the answer šŸ˜Š I, myself got a lot of help from this wonderful community.. so if I can contribute I always do.. šŸ™
8th Jan 2021, 4:27 AM
Minho
Minho - avatar
+ 8
Faris Mohamed Ahh... That's because the base class constructor will be called first and then derived class constructor. You have to remember Constructors are not your ordinary methods. They are basically used to initialise the object upon creating a class. https://code.sololearn.com/cseBO2RS0xOT/?ref=app
8th Jan 2021, 5:48 AM
Minho
Minho - avatar
+ 8
Faris Mohamed Glad I could help! šŸ˜Š
8th Jan 2021, 6:16 AM
Minho
Minho - avatar
+ 7
Faris Mohamed Can you give the question? It's true.. A a = new B(); Can not call any child specific method.
8th Jan 2021, 5:37 AM
Minho
Minho - avatar
+ 7
Constructor and methods both are different. When you're writing A a = new B() B() is the default constructor of the class B. Constructors are used to initialise an object upon creating a class. They don't have a return type. Because the implicit return type of a constructor is the class type itself. While methods are a piece of code which performs some task and returns some value or doesn't return anything (i.e. void)..
8th Jan 2021, 5:55 AM
Minho
Minho - avatar
+ 5
Faris Mohamed Pleasure is always mine šŸ™ Cause it also helps me to learn! šŸ˜Š
8th Jan 2021, 4:24 AM
Minho
Minho - avatar
+ 3
The first sentence says that Class B inherits from Class A. In other words, Class B is a child of Class A. Because of this relationship it is possible to declare an object as A a = new B(); It is a very confusing concept that a different example would definitely do a better job of explaning.
8th Jan 2021, 3:56 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
+ 2
8th Jan 2021, 4:17 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
@ Minho KR. Very well explained! Thank you again! life saver! now i have a clear understanding!
8th Jan 2021, 4:21 AM
Faris Mohamed
Faris Mohamed - avatar
+ 2
@Minho KR hang on.. so A a = new B(); - cant call child specific method. but the above question, did call the child specific method, the constructor, thus the answer is ab. if based on the : A a = new B(); 1. Can call only parents class specific method. 2. Cant call child specific method. the answer should just be only a, isn't it?
8th Jan 2021, 4:43 AM
Faris Mohamed
Faris Mohamed - avatar
+ 2
@Minho KR Class B inherits from Class A. The constructor of Class A displays "a", while the class B constructor displays "b". What is the output to the screen when the code A a = new B(); is run? answer = ab. The answer is ab.
8th Jan 2021, 5:41 AM
Faris Mohamed
Faris Mohamed - avatar
+ 2
yup. i got it. i got myself confused with constructor and methods. i always constructor is a type of method. thanks again! you really helped with my understanding in C# Minho šŸ‡°šŸ‡·
8th Jan 2021, 5:58 AM
Faris Mohamed
Faris Mohamed - avatar
+ 1
So. A a = new A() - creating an object a and only can do job of a. A a = new B() - creating an object a and can do job both a and b. what does "New" actually means? To my understanding, new signifies creation of a new object and defining it A a. cant access the link @Minho KR
8th Jan 2021, 3:58 AM
Faris Mohamed
Faris Mohamed - avatar
+ 1
@Elizabeth Kelly Class B inherits from Class A. Which means Class B : A if i create a new object. B b = New B(). It will do the job of A and B, isn't ?
8th Jan 2021, 4:02 AM
Faris Mohamed
Faris Mohamed - avatar
+ 1
just wondering how do you guys get notifiy to help others?
8th Jan 2021, 4:25 AM
Faris Mohamed
Faris Mohamed - avatar
+ 1
@Minho kr I quote : When you write, A a = new B(); ā€¢ We can call only parents class specific method. ā€¢ We can't call child specific method ->>> So can call child method? Constructor a method or Constructor and method is two different thing? ā€¢ We can't call private method of parent class.
8th Jan 2021, 5:52 AM
Faris Mohamed
Faris Mohamed - avatar