Core: Compiler Rules for Class Construction
what we will learn today is compiler rules for class construction
In the last video, we saw how objects are constructed from the inside out in Java.
And the way this happens is really through compiler rules.
We're gonna talk about what those compiler rules are and how those compiler rules
work, in order to ensure, again, that the object is created from the inside out.
But you've said at the end but wait, I don't remember extending object.Where did we do this? This introduces our first rule associated with a compiler. So let's revisit where the compiler plays a role in our process.
So what is a Java compiler doing and what are these rules?
Core: Variable Initialization in a Class Hierarchy
In the past couple of videos, we've been starting to look at how class creation, or
object creation happens in Java.
specifically how does inheritance apply to object creation.
Now these compiler rules are what the compiler does when it looks at your code.
Now the reason we want to know about these rules,
is because it's going to help us trace our code.
In fact, we're going to see an error in our code here in just a little bit,
which comes about because of these rules.
If you start to recognize that I'm not allowed to say this.name, you're right.
That's a private variable in the person class.
I'm not allowed to direct the access of that in the student class,
I'd have to use a getter or a setter to do so.
But I don't have a getter or a setter. Is there a way for me to do this?
Can I initialize name without having the public getter setter?
The answer is yes.
All I have to do is change this now to call the superclass constructor
that takes an argument, which is gonna initialize to be named.
Concept Challenge: Inheritance Constructors 1:
Concept Challenge: Inheritance Constructors 2
Core: Method Overriding
So goal here today is where the create methods are override from superclass methods and will the contrast overloading from method overriding.
So we wanna have common code in one pairing class. We do this if we don't override a method. So if a method just exists in the superclass we get back common code. But if we want our subclasses to behave differently all we have to do is override a method and now we get that different behavior.