Heidi is a sharing satire.
And welcome to the next Mordieu of object oriented programming in case of Cartland now in Godling we
have some concept of object declaration and also companion objects.
But before learning that let us first check out what is singleton in Scotland.
Now in case of Codlin whenever we declare a clause that we're doing so far we create instance of that
class and then access all the variables and methods but in case of Singleton we have just one instance
of the class in the whole application.
So once we define class as singleton then we just have one instance and that instance is just created
by Cartland internally.
That is we do not have any control over creating the instance of that singleton class.
No it means that Suppose we have a class today which is declared as singleton.
Then we cannot create the objects of this class that is there exists only one object for this class
by default which is created by the Codlin internally.
In simple words we can say we cannot create objects like student 1.
STUDENT 2 and so on.
I hope I am clear at this point.
And now in Java they define singleton by using the keyword of static such as we have static variables
static methods present inside the class.
So in that case the static variables and metrics are not a part of the class.
They are just part of class definition not the objects right.
So in guess of Gorlin as well.
We don't have any static keyword present but alternative to that.
We have some object declaration and also the concept of companion objects.
No proceeding forward in Gondolin we can not declare static variables and methods compared to Jawa.
So we have an alternative to diked such as we can declare object and these objects are actually acting
as a class that can contain the static variables and methods without using the keyword of static.
Now when we declare something as object that Coquelin internally behind the curtain simply creates a
singleton object for us when the program is running.
So when we are in the program we get one instance of that object.
So how do we declare the object in case of Codlin.
So in Cartland.
Let's check out the code snippet.
Suppose we have object customer.
Now here we don't have Klasky word we are using object keyword.
So object space custom.
This is syntax instead of.
Suppose I have variable The other type of integer.
Now this ID is actually acting as a static variable compared to Java.
This is not exactly a static but behaving like a static variable like we call it static.
In case of Java but I similarly suppose I have a function register customer.
So this method as well is behaving as a static method.
Now then we call a static variable and static method.
If you are from Java background then you must be knowing how to access the static variables and methods
not to access these variables and methods.
We need to use simply use customer door Id like say 27.
So here we are simply using customer.
This customer is the name of this object or right.
So class name or the object name dot ID equal to 27.
That looks similar to the code snippet from the Java background.
And next we can call customer d'arte register customer and our record inside the function will be executed
non-nudist.
Here we are calling without creating any instance.
That is we don't need to declare it as the object explicitly.
The Codlin by default creates an object instance that is a singleton instance for our customer class
or the object so let us proceed and check out the cold inside their Damore application.
So here inside the intelligent ID I will simply declare the class of customer.
So here inside the intelligent ID I've simply created an automated class of customers data that contains
the customer's account and also type of customer that simply returns a string value.
Now in order to access discount variable and also this method inside the main we need to do these called.
So here in order to access the count variable and this method we are simply calling the object name
Darktown an object name dot method.
Right.
By simply creating the customer's data object.
Now if I want to use this class as a singleton so what I would do is I would simply use sort of class.
I would simply use object.
So this object keyword simply creates a singleton object for this customers data.
So we no longer can create the instance of this customer data manually because we can create the instance
of the single Going to us.
It is done automatically for you.
So here I have to remove this at the top level code.
And now in order to access the scanned variable what I need to do is we need to simply use copy this
last name and pasted in Baystate here and now again paste it here.
So here discussing variable is actually acting like a static variable.
And also this method is acting like a static method.
Right.
So this is how we access the members of the object like this.
And now if aproned legacy a print Ellen let's say customers data are gone.
Let's see the output.
So here in the output we get 98 right.
So this Gounder value is currently ninety eight that we have defined here.
Now suppose if I again change the value.
Let's see.
Two hundred and nine.
And let's see what happens if we are we're into splendored.
Now this time we get a hundred and nine.
So the value has been updated.
Two hundred and nine.
So whenever you access this account and change the value it will be changed to that particular instance
that was created by Cartland.
Right.
Because we don't have any name for that object.
We are simply using the class name here.
That is our object plus dog count and something like that.
Similarly now the same thing applies for this method as well.
Call it anywhere and it will be returning Indian string.
Right.
And now at the end this object customers data.
And also in properties from the superclass.
So it us define a class here.
Let's call it my superclass.
So here this customer data and also inherit the properties of my superclass.
Now since this is my superclass So we need to treat it like object by using the primary constructor
here.
Right.
And now let us make this class as open so actually so as to inherit all the properties and also if I
want to inherit this my method or override this my method inside that customer's data.
Then also they're disposable so far that to simply use the Bengie word here.
And then simply override this function.
My method simply or what I did now inserted is if you weren't going to call the super dark matter this
year and now here.
Let us spring just like this.
And now you can notice this object is actually behaving like a class because it is actually inheriting
all the properties of my superclass.
So we can call it our derived class.
And this is now acting as a superclass.
Now what about this my method.
How can we access this might make it so far that again inside the main method what we do is I would
simply use customer's data Dorte my method and simply pass.
Let's see.
Hello.
So this my method is now becoming the static member of this object.
Right.
So we can write the command line here.
So this method is similar to this method is now acting as a static method inside this object of customer's
data.
And now if you create the instance of my superclass then you have to access this method here with the
help of the object of this superclass.
It is only behaving stocktake after all what I did inside the object customer's data.
So let us know then the code and let's see what it prints.
So here we get my superclass.
Has God rendered just because of this statement.
Superdog muttered an object customer data.
Hello is getting printed which is present here.
Right.
So in this video we learn about how do we declare the object and also how can we find the alternative
for the static variables and methods in case of Gorlin similar to what we do in Gears of Java.
So at the end of this video and let us summarize the properties of the object so to later object we
use the key word of object and Cartland internally creates a class.
And also the instance an object of that particular class.
But that object is only one Enchong we cannot create more object or instance again and these objects
can have properties methods and also initializers.
And now one thing I forgot to show you that inside the customer's data object we can also use the init
block like this.
And you can now initialize whatever you want to inside the init block.
Right like this.
So we can have initializers in case of object.
And next we can not have constrictors inside the object as it makes sense because because we do not
have any right to do so to create an instance manually and the object can also have a superclass like
we saw in this video that is simply supports inheritance fine.
So this was all about to object in case of Cartland.
So that's all for this video.
Have a nice day and get you guys in the next video.
This is showing hard signing off.
Thank you.