Abstract Class
A class and its members can be declared as abstract which means an abstract member does not have an implementation in its class an abstract class is open there is no need annotate it with open we can also override non abstract open member with abstract one
open class Base{
open fun f(){}
}
abstract class Derived :Base(){
override abstract fun f(){
}
}
open fun f(){}
}
abstract class Derived :Base(){
override abstract fun f(){
}
}
Interface
This is certain type of classes which contains declaration of abstract methods as well as method implementation of the difference between abstract class and interface is that interface cannot store state interface can have properties but they need to be abstract or to provide accessor implementation
an interface is defined using "interface" keyword .A class or object can implement one or more interfaces we can declare properties in interface a property declared may be abstract or it can provide implementation for accessors
an interface is defined using "interface" keyword .A class or object can implement one or more interfaces we can declare properties in interface a property declared may be abstract or it can provide implementation for accessors
interface Firstinterface{
var age:Int //abstract
var withimplementation:String
get() = "twothousand"
fun mufun(){}
}
interface Secondinterface{
fun sufun(){}
}
class Myclass : Firstinterface,Secondinterface{
//implementing two interfaces
override fun mufun(){
//body
}
override fun sufun(){
//body
}
}
var age:Int //abstract
var withimplementation:String
get() = "twothousand"
fun mufun(){}
}
interface Secondinterface{
fun sufun(){}
}
class Myclass : Firstinterface,Secondinterface{
//implementing two interfaces
override fun mufun(){
//body
}
override fun sufun(){
//body
}
}
Visibility modifiers
The classes , objects, constructors ,interfaces ,functions,properties, and their setters can have visibility modifiers getters always have the same visibility as their property the visibility modifiers are
packages :packages are files declared with package keyword and it can contain declaration of classes functions or interfaces functions, properties and classes ,objects and interfaces can be declared on the top-level directly inside the package
note : local variables ,functions,classes cannot have visibility modifiers
packages :packages are files declared with package keyword and it can contain declaration of classes functions or interfaces functions, properties and classes ,objects and interfaces can be declared on the top-level directly inside the package
note : local variables ,functions,classes cannot have visibility modifiers
- private: if we mark a declaration private it will only be visible inside the file containing the declaration as if it is inside the class then it will be visible inside the class only
- protected: protected is not available for top-level declaration but inside the class it is same as private but visible to the subclasses too if you override a protected member and do not specify the visibility explicitly the overriding member will also have protected visibility
- internal: the internal classes are which if you mark declaration as internal it is visible everywhere inside the module and if it is in the class any client inside this module who sees the declaring class sees its internal members (module in kotlin a module is a set of kotlin files compiled together like : an intelIj IDEA module, a maven project)
- public: by default the declarations are marked public means your declaration will be visible every where in class any client who sees the declaring class sees its public members
//file name is nadeem.kt
package nads
private fun foo(){} //visible inside nadeem.kt only
public var bar:Int = 5 //property is visible every where
private set //setter is only visible inside nadeem.kt
internal val baz = 101 //visible inside the same module
package nads
private fun foo(){} //visible inside nadeem.kt only
public var bar:Int = 5 //property is visible every where
private set //setter is only visible inside nadeem.kt
internal val baz = 101 //visible inside the same module
Object Expressions and Declaration
To modify a class with out explicitly declaring a new subclass for it we use the object expression and object declaration
object expression
to create an object of anonymous class that inherits from sometype we write
object expression
to create an object of anonymous class that inherits from sometype we write
window .addmouselistener(object: MouseAdapter(){
override fun mouseclicked(e:MouseEvent){
}
override fun mouseEntered(e:MouseEvent){
}
})
If a supertype has a constructor appropriate constructor parameter must be passed to it many supertype may be specified as a comma separated list after the colon we can also create an object with no nontrivial supertype
open class First(){
public open val y:Int = x
}
Interface Second{....}
val ab:First = object : First(1),Second{
override val y = 15
}
//an object with no nontrivial supertype
fun foo (){
val adhoc = object {
var x :Int = 0
var y : Int =0
}
print(adhoc.x + adhoc.y)
}
One thing to note here is that anonymous objects can be used as types only in private and local declarations if you use an anonymous object of a return type by using anonymous object as the return type of public function or the type of public property the actual type of that function or property will be declared as the supertype of the anonymous object or "Any".if you didn't declare any supertype members added in the anonymous object will not be accessible
class mised(){
//private function so the return type is anonymous object type
private fun mis = object{
val s :String = "s"
}
//public fun so the return type is Any
fun foo () = object{
val s : String = "s"
}
fun bar(){
val s1 = mis().s //works
val s2 = foo().s//shows error
}
}
//private function so the return type is anonymous object type
private fun mis = object{
val s :String = "s"
}
//public fun so the return type is Any
fun foo () = object{
val s : String = "s"
}
fun bar(){
val s1 = mis().s //works
val s2 = foo().s//shows error
}
}
Code in the object expressions can access variables from the enclosing scope and this is not restricted to final variables
fun changename(window:JComponent){
var name1 = "vas"
var name2 = "vas2"
window.addMouseListener(object: MouseAdapter(){
override fun mouseClicked (e:MouseEvent){
name1 = "nak"}
override fun mouseEntered(e:MouseEvent){
name2 = "vak"
}})}
var name1 = "vas"
var name2 = "vas2"
window.addMouseListener(object: MouseAdapter(){
override fun mouseClicked (e:MouseEvent){
name1 = "nak"}
override fun mouseEntered(e:MouseEvent){
name2 = "vak"
}})}
No comments:
Post a Comment