Skip to content

Commit

Permalink
Object Oriented Concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
smartherd committed Sep 16, 2018
1 parent 0a47854 commit 9384d06
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/26_class_and_constructor.kt
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
}
}
49 changes: 49 additions & 0 deletions src/27_inheritance.kt
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")
}
}
38 changes: 38 additions & 0 deletions src/28_overriding_methods_properties.kt
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")
}
}
29 changes: 29 additions & 0 deletions src/29_inheritance_primary_secondary_constructor.kt
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")
}
}
34 changes: 34 additions & 0 deletions src/30_abstract_class.kt
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
}
}
43 changes: 43 additions & 0 deletions src/31_interface.kt
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()
}
}
22 changes: 22 additions & 0 deletions src/32_data_class.kt
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)
37 changes: 37 additions & 0 deletions src/33_object_declaration.kt
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")
}
}
23 changes: 23 additions & 0 deletions src/34_companion_object.kt
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"
}
}
}

0 comments on commit 9384d06

Please sign in to comment.