Secondary Constructors
the class can also declare secondary constructor helps to include some extra logic while initializing the primary constructor the secondary constructor are prefixed with constructor keyword here is an example
class Myclass(){
constructor (parent:Myclass){
parent.children.add(this)
}
}
constructor (parent:Myclass){
parent.children.add(this)
}
}
if the class has a primary constructor each secondary constructor needs to delegate to the primary constructor either directly or indirectly through another secondary constructors delegation to another constructor of the same class is done using the this keyword
class Myclass (val name :String){
constructor (name:String,parent:Myclass ):this(name){
parent.children.add(this)
}
}
here code in the initializer block effectively becomes the part of the primary constructor the delegation to primary constructor happens as in the first statement of the secondary constructor so the code in the all initializer block is executed before the secondary constructor body even if the class has no primary constructor the delegation still happens implicitly and the initializer blocks are still executed
class Constructors {
init(){
println("its the init block")}
constructor(i : Int){
println("constructor")
}
}
fun main(args: Array<String>) {
Constructors(1)
}
//this will be the output
its the init block
constructor
if no primary and secondary constructor are not declared in non abstract class (abstract class will be explained later ) it will generate a primary constructor with no arguments the visibility of the constructor will be public (means visible everywhere) if you do not wan't to have a public constructor you need to declare empty primary constructor with non default visibility
class nodefault constructor(){
}
If all of the parameters of primary constructor have default values the compiler will generate an additional parameterless constructor which will use the default values this makes it use kotlin with libraries like jackson or JPA that creates class instance through parameterless constructors
Creating Instance of classes
Creating Instance of classes
To create a instance of a class we call the constructor as if it were a regular function the creation of instance is known as instantiation.the objects are created from classes by subroutines called constructors an object is an instance of a class instantiation is then also known as construction
val myclass = Myclass ()
val myshow = Myshow("joy mathew")
val myshow = Myshow("joy mathew")
note that like in java kotlin doesn't have new keyword
Inheritance
Inheritance is which enables the object to take the properties of other object like inheriting from one object this is termed as the inheritance by the child object inherits from the parent object and in kotlin all the classes have a common superclass "Any" this is the default superclass for a class with no supertypes declared. To declare an explicit supertype we place the type after a colon in the class header here is an example :
open class Base (p:Int)
class Derived (p:Int): Base (p)
class Derived (p:Int): Base (p)
The open annotation on a class is the opposite of final keyword in java language(the class with final cannot be subclassed) it allows others to inherit from this class. By default all classes in kotlin are final
If the derived class has a primary constructor then the base class can (and must be) initialized there using the parameters of the primary constructor if the class has no primary constructor then each secondary constructor has to initialize the base type using the super keyword or to delegate to another constructor which does that . In this case different secondary constructors can call different constructors of the base type
If the derived class has a primary constructor then the base class can (and must be) initialized there using the parameters of the primary constructor if the class has no primary constructor then each secondary constructor has to initialize the base type using the super keyword or to delegate to another constructor which does that . In this case different secondary constructors can call different constructors of the base type
class Myview : View {
constructor (ct : Context ): super(ct)
constructor(ct : Context,attr :Attributeset):super(ct)
}
constructor (ct : Context ): super(ct)
constructor(ct : Context,attr :Attributeset):super(ct)
}
Overriding Methods
The overriding a method is a feature in which allows the subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes Kotlin requires explicit annotations for overridable members (we call them open ) and for overrides
No comments:
Post a Comment