Hello, Jim.
In this lesson, we are going to learn more about one of the key principles of the object oriented programming
approach.
I'm talking about inheritance.
And they will talk not only about inheritance.
Well, on super keyword and you operator instance of I will show you how we can extend classes and what
rules should be followed during inheritance.
So let's start.
I believe you remember theoretical part which we discussed in lesson about inheritance.
So now it is time for practical code examples demo to understand this topic better.
I always try to create real life example for you so that you could understand how this works for real
in this lesson.
Imagine that you have product type in your program.
I didn't create too many fields to not distract attention from inheritance topic.
Just imagine that product can have multiple fields and methods.
And we also have the phone type.
Phone is also a product in our online shop.
It will have similar fields and behavior as product has.
But besides that, it has also some phone specific behavior like ring method, for example.
Now let's create phone object I a product variable and initialize it with foreign object.
Take into account for an object is also a type product and you can see that our product variable has
own methods that are declared in product type.
I have said gather and calculate remaining amount method which is not declared in phone but declared
in product.
So the idea of inheritance is to describe new types based on existing one from technical side.
This allows us to reuse already written code, which is also pretty cool because let's imagine you need
to change the logic how remaining amount in stock is calculated.
Instead of changing this method in all classes where this method is used, I can just do change in one
place in product class and behaviour will become different in old child classes.
And you also see that foreign object doesn't have name field, but somehow I can set the value for name
property of the form.
That is because inheritance of this field from product object.
OK, but let's try to ring right now.
Unfortunately, we have compilation error because Renk method is not defined for the type product,
but we see here that we created for an object and we still don't have that Renk method available.
Why?
Because Java is not mahzarin.
What object will be here in this line during the runtime?
You can initialize the variable with any object of this type and Java doesn't want to guess what object
is there for real.
What Java knows that in case code is compiler, that means object which was assigned to this variable
has the same interface as this type for sure.
That's why we don't see any matters from form type here, because we have variable of type product.
When I create variable or form that I can invoke Renk method without compilation error.
But what if my class or method works with product type and I want to call Remarque method?
I will be able to do that only in case during the runtime.
I have phone object here.
But how I can understand this now I will show you a new keyword and operator which you will be able
to use in your programs.
Here is an instance of operator.
It allows you to check the type consistency with the object.
It is a binary operator which works with two operands.
Let me show you how you can use it if object behind the product variable.
This one is instance of phone type.
The result will be true in terms of returns.
Is a true or false and if instance of returns true, I can cast this variable to type the remember how
Wikus primitive types.
We can just reference types in a similar way.
Right.
Phone and variable name.
Now put the type name in parentheses and name of the variable you want to cast.
Now you can invoke Renk method instance of the return false in case variable reference to object is
incompatible with the type you specified as a second operand.
OK, you know what is instance of and when you can use it.
Let's proceed with inheritance topic in your child class.
You always have a reference to a parent class.
You might want to have the reference to the parent class, for example, to call some parent methods.
Let's take a look here.
Product Class has a message that returns the least of all variant products associated with the current
product.
And our phone object overrides this behavior and throws and supports the separation exception.
This exception indicates that this behavior is not supported in this class.
Later in this course, we will learn that this implementation violates one of the design principles
in the object oriented programming.
But you would face with similar cases many in.
Times, believe me, and now what will happen if in another method calculate the amount of variance
I will call least variance method of this object will be called and program will stop execution because
of the exception?
Let me run this program to show you this.
What do you have to do right now?
You already know that in each class you have reference to a parent class who is super keyword.
So let's call math at least reference not from this class, but let's call method list variants of the
parent class.
This one just right super.
This is a reference to the parent type Dorte and invoke the same method.
What we have now left me around this program one more time.
We won't have any error because this method was called you can always use super keyword to reference
methods or properties of a parent class.
Now imagine the next scenario.
Imagine you don't have the full constructor in your parent object because you can create products only
with names.
How this will impact your phone that you're going to have compilation error.
Why?
Because the grade child object GBM invokes constructor of all parent classes and now you can call constructor
of parent classes because it doesn't know what should be passed.
As a constructor argument, the full constructor often looks like this.
So what we need to do to remove this compilation error, we need to explicitly call constructor of parent
class and pass a string parameter to it that's type super parentheses and pass some string argument
to it.
Here we call parent constructor and you see that now compilation error is gone.
Now Joram will be able to create an object of the form type one more important common here call of the
parent constructor always should go first in child constructor.
I mean you can't call any other methods before super.
Here, let me show you.
If I write here some code, it doesn't matter what exactly are right here.
Let me cancel.
Output will have again compilation error.
You need to remember that invocation of Ben constructor and using of super keyword should always go
first in shall constructor.
I will make a post now.
Is it clear if this is clear, let's move on.
I told you that OP is fun.
Now you know that to create a child's object constructor of a parent type is called what else happens
during child object initialization.
Here is a quick exercise for you that will help you to remember for the rest of your lives what happens
during child object construction.
Here is the code of our exercise.
So let's start.
We have parent type here.
You can see static initialization lock installation, lock constructor.
We have console output everywhere here and it's clearly specified that this output belongs to the parent
type and we have child types that extends parent class.
The child class has the same things that parent has static, initialization block, initialization lock
and constructor.
And you see the text and console output is different.
The question is what will be printed to console when I will create child object?
And another question, what will be printed to console when I will create child object the second time?
Press pause and try to answer this question.
OK, let me answer this.
I will run the program and we will investigate console output and let's see what we have here.
Static initialization block of the parent is executed and after that static installation lock of the
child type is executed.
Why static blocks are executed when classes uploaded into GBM parent class was uploaded first and after
that child class was uploaded after the JVM wants to initialize parent fields first.
That's why it executes parent installation block.
And after that parent constructor and only after that JVM starts to create child object and executes
child and installation work and child constructor home.
This example is clear.
What happens when I try to create child objects a second time?
Everything the same beside the execution of static initialization locks.
Why?
Because everything what is followed by static keyword, including initialization blocks, is associated
not with the object of this class, but with the class itself.
So when classes are uploaded into GBM, these blocks are executed.
They won't be executed two times in a row home.
Now with this example, it has become clearer what is initialization order during the construction of
a giant object.
The last thing you need to know about inheritance is that in Java, multiple inheritance is not supported
that.
Means you can't define new type on the basis of two or more opposite types after extensive keyword.
You can specify only one class, but in some other languages, multiple inheritance is supported.
That's all what we have in our agenda for today.
Let's recap what we have learned today.
We learned what inheritance is and how we can extend the time.
Now, you know, two more key words, super and instance of you learn some specific constraints while
extending classes.
And today you learned what initialization order is of new objects that is extended from another class.
That's all.
Thank you for your attention and see you in the next lesson.