+ 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();
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.
+ 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!
+ 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.. š
+ 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
+ 8
Faris Mohamed
Glad I could help! š
+ 7
Faris Mohamed
Can you give the question?
It's true..
A a = new B();
Can not call any child specific method.
+ 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)..
+ 5
Faris Mohamed
Pleasure is always mine š
Cause it also helps me to learn! š
+ 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.
+ 2
Read this lesson and comments :
https://www.sololearn.com/learn/CSharp/2678/?ref=app
+ 2
@ Minho KR.
Very well explained! Thank you again! life saver!
now i have a clear understanding!
+ 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?
+ 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.
+ 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 š°š·
+ 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
+ 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 ?
+ 1
just wondering how do you guys get notifiy to help others?
+ 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.