Classes and Objects
Basically, classes are templates that allow you to create your own types of objects and define their properties and functions.
object types are defined using classes. for example:
var x = 6
this creates an Int object with a value of 6.
When you create an Int object. the compiler checks the Int class and sees that it requires an integer value and has functions such as toLong and toString.
You can define your own class
The things an object knows about itself are its properties. They represent an object state(the data) and each object of that type can have unique values.
For example:
class Dog(val name: String, var weight :Int ,val breed:String)
{
fun bark()
{
println(if (weight < 20) "Yip" else "woof")
}
}
A function that's defined inside a class is called a member function. Its sometimes called a method. This is just like the functions only difference is that it's defined inside the class body.
Now let's create object:
var myDog = Dog("Name",70 ,"Mixed")
How to access properties and functions
var myDog = Dog("Name",70 ,"Mixed")
pritln(myDog.name)
It's like saying go to myDog and get its name.
you can also use the dot operator to call an object's function.
var myDog = Dog("Name",70 ,"Mixed")
pritln(myDog.name)
myDog.bark()
It's like go to myDog and call its bark function.
What if the Dog is in a Dog Array?
var dogs = arrayOf(Dog("FirstDog",50,"Mixed"),Dog("SecDog",50,"Mixed"),Dog("ThirdDog",50,"Mixed"))
This code creates three Dog objects and them to an array<Dog> array named dog. You can still access the properties and functions of each dog in the array-like
dogs[0].weight = 15
dogs[1].bark()
The compiler knows that dogs[] is a Dog object so you can access the Dog properties and call its functions.
A constructor runs when you instantiate an object. It is used to define properties and initialized them. The above example shown is called the primary constructor.
The Dog constructor defines properties and each property is really just a variable that's local to the object. A value is then assigned to that variable.
An object is sometimes known as an instance of a particular class so its properties are sometimes called instance variables.
So here you can ask a few questions:
Does the constructor allocate the memory for the object that's being created?
Ans: No the system does. The constructor initializes the object so it makes sure that the object properties are created and that they are assigned their initial values. All memory is managed by the system.
Can I define a class without defining a constructor?
Ans: Yes you can.
In java, you don't have to initialize the variables that you declare inside a class.is there a way of not initializing class properties in Kotlin?
Ans: If you are completely certain that you cant assign an initial value to the property when you call the class constructor .you can prefix it with lateinit.This tells the compiler that you are aware that the properties have not been initialized yet, and you will handle it later. If you want to mark the temperament property for late initialization for example:
lateinit var temperament: String
Doing this will allows the compiler to compile your code.
What happens if I try to use a property value before it's been initialized?
Ans: if you do not initialize a property before you try and use it.you will get a runtime error when you run the code.
Can I use lateinit with any type of property?
Ans: you can only use lateinit with properties defined using var and you can use it with any of the following types Byte ,Shot, Int,Long, Double, Float,Char or Boolean. This means that properties of any of these types must be initialized when the property is defined or in an initializer block.
Small Example;
class TapeDeck
{
var hasRecoder = false
fun playTape()
{
println("Tape is playing")
}
fun recordTape()
{
println("Tape is recording")
}
}
fun main(args:Array<String>)
{
t.hasRecoder = true
t.playTape()
t.recordTape()
}
let's take big code understand the class and objects:
class Dog(val name : String , weight_param :Int,breed_param:String) // primary constructor
{
init{
print("Dog $name has been created") // init block
}
var activities = arrayOf("Walks")
val breed = breed_param.toUpperCase()
init
{
println("The breed is $breed")
}
var weight = weight_param
set(value){
if(value > 0) field = value
}
val weightInkgs : Double
get() = weight / 2.2
fun bark()
{
println( if(weight < 20 ) "Yip" else "woof")
}
}
fun main(args: Array<String>)
{
val myDog = Dog("Dog1", 80 , "Mixed")
myDog.bark()
myDog.weight = 75
println("Weight in Kgs is ${myDog.weight}")
myDog.activities = arrayOf("Walks","Fetchig balls',"Frisbee")
for(item in myDog.activities)
{
println("My dog enjoyed $item")
}
}
Bullet Points:
Basically, classes are templates that allow you to create your own types of objects and define their properties and functions.
object types are defined using classes. for example:
var x = 6
this creates an Int object with a value of 6.
When you create an Int object. the compiler checks the Int class and sees that it requires an integer value and has functions such as toLong and toString.
You can define your own class
- The things each object knows about itself.
- The things each object can do.
The things an object knows about itself are its properties. They represent an object state(the data) and each object of that type can have unique values.
For example:
class Dog(val name: String, var weight :Int ,val breed:String)
{
fun bark()
{
println(if (weight < 20) "Yip" else "woof")
}
}
A function that's defined inside a class is called a member function. Its sometimes called a method. This is just like the functions only difference is that it's defined inside the class body.
Now let's create object:
var myDog = Dog("Name",70 ,"Mixed")
How to access properties and functions
var myDog = Dog("Name",70 ,"Mixed")
pritln(myDog.name)
It's like saying go to myDog and get its name.
you can also use the dot operator to call an object's function.
var myDog = Dog("Name",70 ,"Mixed")
pritln(myDog.name)
myDog.bark()
It's like go to myDog and call its bark function.
What if the Dog is in a Dog Array?
var dogs = arrayOf(Dog("FirstDog",50,"Mixed"),Dog("SecDog",50,"Mixed"),Dog("ThirdDog",50,"Mixed"))
This code creates three Dog objects and them to an array<Dog> array named dog. You can still access the properties and functions of each dog in the array-like
dogs[0].weight = 15
dogs[1].bark()
The compiler knows that dogs[] is a Dog object so you can access the Dog properties and call its functions.
A constructor runs when you instantiate an object. It is used to define properties and initialized them. The above example shown is called the primary constructor.
The Dog constructor defines properties and each property is really just a variable that's local to the object. A value is then assigned to that variable.
An object is sometimes known as an instance of a particular class so its properties are sometimes called instance variables.
So here you can ask a few questions:
Does the constructor allocate the memory for the object that's being created?
Ans: No the system does. The constructor initializes the object so it makes sure that the object properties are created and that they are assigned their initial values. All memory is managed by the system.
Can I define a class without defining a constructor?
Ans: Yes you can.
In java, you don't have to initialize the variables that you declare inside a class.is there a way of not initializing class properties in Kotlin?
Ans: If you are completely certain that you cant assign an initial value to the property when you call the class constructor .you can prefix it with lateinit.This tells the compiler that you are aware that the properties have not been initialized yet, and you will handle it later. If you want to mark the temperament property for late initialization for example:
lateinit var temperament: String
Doing this will allows the compiler to compile your code.
What happens if I try to use a property value before it's been initialized?
Ans: if you do not initialize a property before you try and use it.you will get a runtime error when you run the code.
Can I use lateinit with any type of property?
Ans: you can only use lateinit with properties defined using var and you can use it with any of the following types Byte ,Shot, Int,Long, Double, Float,Char or Boolean. This means that properties of any of these types must be initialized when the property is defined or in an initializer block.
Small Example;
class TapeDeck
{
var hasRecoder = false
fun playTape()
{
println("Tape is playing")
}
fun recordTape()
{
println("Tape is recording")
}
}
fun main(args:Array<String>)
{
t.hasRecoder = true
t.playTape()
t.recordTape()
}
let's take big code understand the class and objects:
class Dog(val name : String , weight_param :Int,breed_param:String) // primary constructor
{
init{
print("Dog $name has been created") // init block
}
var activities = arrayOf("Walks")
val breed = breed_param.toUpperCase()
init
{
println("The breed is $breed")
}
var weight = weight_param
set(value){
if(value > 0) field = value
}
val weightInkgs : Double
get() = weight / 2.2
fun bark()
{
println( if(weight < 20 ) "Yip" else "woof")
}
}
fun main(args: Array<String>)
{
val myDog = Dog("Dog1", 80 , "Mixed")
myDog.bark()
myDog.weight = 75
println("Weight in Kgs is ${myDog.weight}")
myDog.activities = arrayOf("Walks","Fetchig balls',"Frisbee")
for(item in myDog.activities)
{
println("My dog enjoyed $item")
}
}
Bullet Points:
- Classes let you define your own types
- A class is a template for an object. One class can create many objects.
- A property is a variable that's local to the class.
- The class keyword defines a class
- A constructor runs when you initialize an object.
- You can define a property in the primary constructor by prefixing a parameter with all or var. you can define a property outside the constructor by adding it to the class body.
- Initialized blocks run when an object is initialized.
- Behind the scenes, the compiler adds a default getter and setter to every property.
2 comments:
Nice informative content. Thanks for sharing such worthy information.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
Spoken English Course Online
Spoken English Classes in Coimbatore
Thank you for the useful information. Share more updates.
Irregular Verbs
Auxiliary verbs
Post a Comment