2024年05月20号【Polymorphism】

Core: Introduction to Polymorphism
 

Polymorphism

So you're goal from this video is just to be able to explain it's purpose, and 

why we have it.

Now, we've already done some polymorphism. 

You've already had a different super class reference pointing to a sub class object.

--------------------------------------------------------------------------------------------------------------------------------

So our hierarchy, again is the Person class, the Student class, 

the Faculty class. 

And now you notice that I've added to this Faculty class, and 

now it has an employeeID number, which is a string. 

It has toString method just like Person does, just like Student does. 

So all three of our classes now have toString methods. 

We're gonna use this in the next context.

We said in the very first videos on inheritance, that one of the nice features is that you can have a single data structure point to multiple objects of that type.

So I'm allowed to have a person array, which has room for three references. And have one of those references be a person, one of those references be a student, and one of those references be faculty.

 And I want the toString method being called on that third element to be called on the faculty member, and the faculty object, and that's exactly what happens. So. What polymorphism gives us, is the ability to keep all of our objects in one big collection. And then call methods on every single one of our elements and the appropriate method is what actually gets called. But this does get a little confusing at times, because we have a different reference point to a different type of object.

To understand this in more depth, we have to look at

compile time rules

and

run time rules,

and we'll do that in the next video.

Core: Rules to Follow for Polymorphism

Hi, welcome back to our lessons on polymorphism.
So with the lessons today, we're gonna focus on compile time rules versus runtime rules.

So the rules, first off, compiler rules. Compiler can only know the reference type.

Doesn't know the runtime type of the object. It only knows the reference type. Which means when you try call a method on object, it can only look in that reference type class for that method.

The compiler only knows that this is a Person reference. So when you try call toString it's gonna look in the Person class, and find that toString method.

At runtime, you have to follow the exact runtime type of the object to find that method. So what you're gonna do is match the method signature, which was emitted at compile time, to the appropriate method within the actual runtime class.

Well, we're going to be able to do that, but we're going to have to do that through explicit casting. We're going to cover casting in the next video.

so in this lession i learned at compile time , we only need to look at reference 

So we have our Person class, we have our Student class. 

I've got a Person reference point to the Student object, and 

I'm gonna call the toString method on that Student object. 

The compiler only knows that this is a Person reference

So when you try call toString it's gonna look in the Person class, and 

find that toString method.

Then it's gonna emit a compile time, a method signature. A compile time, which is a no parameter toString method, which is to be executed at runtime.

at run time, the exact runtime type of the object to find that method. So what you're gonna do is match the method signature, which was emitted at compile time, to the appropriate method within the actual runtime class.

  • 25
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值