forked from smartherd/KotlinTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
/* | ||
* Class, Primary Constructor, Secondary Constructor and Init Block | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var student = Student("Steve", 10) | ||
|
||
println(student.id) | ||
} | ||
|
||
class Student(var name: String) { | ||
|
||
var id: Int = -1 | ||
|
||
init { | ||
println("Student has got a name as $name and id is $id") | ||
} | ||
|
||
constructor(n: String, id: Int): this(n) { | ||
// The body of the secondary constructor is called after init block | ||
this.id = id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/* | ||
* Inheritance | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var dog = Dog() | ||
dog.bread = "labra" | ||
dog.color = "black" | ||
dog.bark() | ||
dog.eat() | ||
|
||
var cat = Cat() | ||
cat.age = 7 | ||
cat.color = "brown" | ||
cat.meow() | ||
cat.eat() | ||
|
||
var animal = Animal() | ||
animal.color = "white" | ||
animal.eat() | ||
} | ||
|
||
open class Animal { // Super class / Parent class / Base class | ||
|
||
var color: String = "" | ||
|
||
fun eat() { | ||
println("Eat") | ||
} | ||
} | ||
|
||
class Dog : Animal() { // Sub class / Child class / Derived class | ||
|
||
var bread: String = "" | ||
|
||
fun bark() { | ||
println("Bark") | ||
} | ||
} | ||
|
||
class Cat : Animal() { // Sub class / Child class / Derived class | ||
|
||
var age: Int = -1 | ||
|
||
fun meow() { | ||
println("Meow") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
/* | ||
* 1. Method Overriding | ||
* 2. Property Overriding | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var dog = MyDog() | ||
|
||
println(dog.color) | ||
|
||
dog.eat() | ||
} | ||
|
||
open class MyAnimal { | ||
|
||
open var color: String = "White" | ||
|
||
open fun eat() { | ||
println("Animal Eating") | ||
} | ||
} | ||
|
||
class MyDog : MyAnimal() { | ||
|
||
var bread: String = "" | ||
|
||
override var color: String = "Black" | ||
|
||
fun bark() { | ||
println("Bark") | ||
} | ||
|
||
override fun eat() { | ||
super<MyAnimal>.eat() | ||
println("Dog is eating") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
/* | ||
* Inheritance with Primary and Secondary Constructors | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var dog = TheDog("Black", "Pug") | ||
} | ||
|
||
open class TheAnimal { // Super class / Parent class / Base class | ||
|
||
var color: String = "" | ||
|
||
constructor(color: String) { | ||
this.color = color | ||
println("From Animal: $color") | ||
} | ||
} | ||
|
||
class TheDog : TheAnimal { // Sub class / Child class / Derived class | ||
|
||
var bread: String = "" | ||
|
||
constructor(color: String, breed: String): super(color) { | ||
this.bread = breed | ||
|
||
println("From Dog: $color and $breed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
/* | ||
* Abstract Class | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
// var person = MyPerson() // Not allowed. You cannot create instance of abstract class | ||
|
||
var person = Indian() // Allowed. Abstract Super class reference variable | ||
// pointing to child class object. | ||
person.name = "Steve" | ||
person.eat() | ||
person.goToSchool() | ||
} | ||
|
||
abstract class MyPerson { // you cannot create instance of abstract class | ||
|
||
abstract var name: String | ||
|
||
abstract fun eat() // abstract properties are 'open' by default | ||
|
||
open fun getHeight() {} // A 'open' function ready to be overridden | ||
|
||
fun goToSchool() {} // A normal function | ||
} | ||
|
||
class Indian: MyPerson() { | ||
|
||
override var name: String = "dummy_indian_name" | ||
|
||
override fun eat() { | ||
// Our own code | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
/* | ||
* INTERFACE | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var myButton = MyButton() | ||
myButton.onTouch() | ||
myButton.onClick() | ||
} | ||
|
||
interface MyInterfaceListener { // You cannot create the instance of interface | ||
|
||
fun onTouch() // Methods in interface are abstract by default | ||
|
||
fun onClick() { // Normal methods are public and open by default but NOT FINAL | ||
println("MyInterfaceListener: onClick") | ||
} | ||
} | ||
|
||
interface MySecondInterface { // You cannot create the instance of interface | ||
|
||
fun onTouch() { // Normal Method | ||
println("MySecondInterface: onTouch") | ||
} | ||
|
||
fun onClick() { // Normal methods are public and open by default but NOT FINAL | ||
println("MySecondInterface: onClick") | ||
} | ||
|
||
} | ||
|
||
class MyButton: MyInterfaceListener, MySecondInterface { | ||
|
||
override fun onTouch() { | ||
super<MyInterfaceListener>.onClick() | ||
super<MySecondInterface>.onClick() | ||
} | ||
|
||
override fun onClick() { | ||
super.onTouch() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
/* | ||
* Data Class | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
var user1 = User("Sam", 10) | ||
|
||
var user2 = User("Sam", 10) | ||
|
||
println(user1.toString()) | ||
|
||
if (user1 == user2) | ||
println("Equal") | ||
else | ||
println("Not equal") | ||
|
||
var newUser = user1.copy(id = 25) | ||
println(newUser) | ||
} | ||
|
||
data class User(var name: String, var id: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
/* | ||
* 1. Object Declaration | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
CustomersData.count = 98 | ||
CustomersData.typeOfCustomers() | ||
|
||
println(CustomersData.typeOfCustomers()) | ||
|
||
CustomersData.count = 109 | ||
println(CustomersData.count) | ||
|
||
CustomersData.myMethod("hello") | ||
} | ||
|
||
open class MySuperClass { | ||
|
||
open fun myMethod(str: String) { | ||
println("MySuperClass") | ||
} | ||
} | ||
|
||
object CustomersData: MySuperClass() { // Object Declaration | ||
|
||
var count: Int = -1 // Behaves like a STATIC variable | ||
|
||
fun typeOfCustomers(): String { // Behaves like a STATIC method | ||
return "Indian" | ||
} | ||
|
||
override fun myMethod(str: String) { // Currently, behaving like a STATIC method | ||
super.myMethod(str) | ||
println("object Customer Data: $str") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
/* | ||
* 1. Companion Object | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
MyClass.count // You can print it and check result | ||
|
||
MyClass.typeOfCustomers() | ||
} | ||
|
||
class MyClass { | ||
|
||
companion object { | ||
|
||
var count: Int = -1 // Behaves like STATIC variable | ||
|
||
@JvmStatic | ||
fun typeOfCustomers(): String { // Behaves like STATIC method | ||
return "Indian" | ||
} | ||
} | ||
} |