Skip to content

Commit 9384d06

Browse files
committed
Object Oriented Concepts
1 parent 0a47854 commit 9384d06

9 files changed

+299
-0
lines changed

src/26_class_and_constructor.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
* Class, Primary Constructor, Secondary Constructor and Init Block
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var student = Student("Steve", 10)
8+
9+
println(student.id)
10+
}
11+
12+
class Student(var name: String) {
13+
14+
var id: Int = -1
15+
16+
init {
17+
println("Student has got a name as $name and id is $id")
18+
}
19+
20+
constructor(n: String, id: Int): this(n) {
21+
// The body of the secondary constructor is called after init block
22+
this.id = id
23+
}
24+
}

src/27_inheritance.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/*
3+
* Inheritance
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var dog = Dog()
8+
dog.bread = "labra"
9+
dog.color = "black"
10+
dog.bark()
11+
dog.eat()
12+
13+
var cat = Cat()
14+
cat.age = 7
15+
cat.color = "brown"
16+
cat.meow()
17+
cat.eat()
18+
19+
var animal = Animal()
20+
animal.color = "white"
21+
animal.eat()
22+
}
23+
24+
open class Animal { // Super class / Parent class / Base class
25+
26+
var color: String = ""
27+
28+
fun eat() {
29+
println("Eat")
30+
}
31+
}
32+
33+
class Dog : Animal() { // Sub class / Child class / Derived class
34+
35+
var bread: String = ""
36+
37+
fun bark() {
38+
println("Bark")
39+
}
40+
}
41+
42+
class Cat : Animal() { // Sub class / Child class / Derived class
43+
44+
var age: Int = -1
45+
46+
fun meow() {
47+
println("Meow")
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
/*
3+
* 1. Method Overriding
4+
* 2. Property Overriding
5+
* */
6+
fun main(args: Array<String>) {
7+
8+
var dog = MyDog()
9+
10+
println(dog.color)
11+
12+
dog.eat()
13+
}
14+
15+
open class MyAnimal {
16+
17+
open var color: String = "White"
18+
19+
open fun eat() {
20+
println("Animal Eating")
21+
}
22+
}
23+
24+
class MyDog : MyAnimal() {
25+
26+
var bread: String = ""
27+
28+
override var color: String = "Black"
29+
30+
fun bark() {
31+
println("Bark")
32+
}
33+
34+
override fun eat() {
35+
super<MyAnimal>.eat()
36+
println("Dog is eating")
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/*
3+
* Inheritance with Primary and Secondary Constructors
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var dog = TheDog("Black", "Pug")
8+
}
9+
10+
open class TheAnimal { // Super class / Parent class / Base class
11+
12+
var color: String = ""
13+
14+
constructor(color: String) {
15+
this.color = color
16+
println("From Animal: $color")
17+
}
18+
}
19+
20+
class TheDog : TheAnimal { // Sub class / Child class / Derived class
21+
22+
var bread: String = ""
23+
24+
constructor(color: String, breed: String): super(color) {
25+
this.bread = breed
26+
27+
println("From Dog: $color and $breed")
28+
}
29+
}

src/30_abstract_class.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/*
3+
* Abstract Class
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
// var person = MyPerson() // Not allowed. You cannot create instance of abstract class
8+
9+
var person = Indian() // Allowed. Abstract Super class reference variable
10+
// pointing to child class object.
11+
person.name = "Steve"
12+
person.eat()
13+
person.goToSchool()
14+
}
15+
16+
abstract class MyPerson { // you cannot create instance of abstract class
17+
18+
abstract var name: String
19+
20+
abstract fun eat() // abstract properties are 'open' by default
21+
22+
open fun getHeight() {} // A 'open' function ready to be overridden
23+
24+
fun goToSchool() {} // A normal function
25+
}
26+
27+
class Indian: MyPerson() {
28+
29+
override var name: String = "dummy_indian_name"
30+
31+
override fun eat() {
32+
// Our own code
33+
}
34+
}

src/31_interface.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/*
3+
* INTERFACE
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var myButton = MyButton()
8+
myButton.onTouch()
9+
myButton.onClick()
10+
}
11+
12+
interface MyInterfaceListener { // You cannot create the instance of interface
13+
14+
fun onTouch() // Methods in interface are abstract by default
15+
16+
fun onClick() { // Normal methods are public and open by default but NOT FINAL
17+
println("MyInterfaceListener: onClick")
18+
}
19+
}
20+
21+
interface MySecondInterface { // You cannot create the instance of interface
22+
23+
fun onTouch() { // Normal Method
24+
println("MySecondInterface: onTouch")
25+
}
26+
27+
fun onClick() { // Normal methods are public and open by default but NOT FINAL
28+
println("MySecondInterface: onClick")
29+
}
30+
31+
}
32+
33+
class MyButton: MyInterfaceListener, MySecondInterface {
34+
35+
override fun onTouch() {
36+
super<MyInterfaceListener>.onClick()
37+
super<MySecondInterface>.onClick()
38+
}
39+
40+
override fun onClick() {
41+
super.onTouch()
42+
}
43+
}

src/32_data_class.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*
3+
* Data Class
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var user1 = User("Sam", 10)
8+
9+
var user2 = User("Sam", 10)
10+
11+
println(user1.toString())
12+
13+
if (user1 == user2)
14+
println("Equal")
15+
else
16+
println("Not equal")
17+
18+
var newUser = user1.copy(id = 25)
19+
println(newUser)
20+
}
21+
22+
data class User(var name: String, var id: Int)

src/33_object_declaration.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/*
3+
* 1. Object Declaration
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
CustomersData.count = 98
8+
CustomersData.typeOfCustomers()
9+
10+
println(CustomersData.typeOfCustomers())
11+
12+
CustomersData.count = 109
13+
println(CustomersData.count)
14+
15+
CustomersData.myMethod("hello")
16+
}
17+
18+
open class MySuperClass {
19+
20+
open fun myMethod(str: String) {
21+
println("MySuperClass")
22+
}
23+
}
24+
25+
object CustomersData: MySuperClass() { // Object Declaration
26+
27+
var count: Int = -1 // Behaves like a STATIC variable
28+
29+
fun typeOfCustomers(): String { // Behaves like a STATIC method
30+
return "Indian"
31+
}
32+
33+
override fun myMethod(str: String) { // Currently, behaving like a STATIC method
34+
super.myMethod(str)
35+
println("object Customer Data: $str")
36+
}
37+
}

src/34_companion_object.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/*
3+
* 1. Companion Object
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
MyClass.count // You can print it and check result
8+
9+
MyClass.typeOfCustomers()
10+
}
11+
12+
class MyClass {
13+
14+
companion object {
15+
16+
var count: Int = -1 // Behaves like STATIC variable
17+
18+
@JvmStatic
19+
fun typeOfCustomers(): String { // Behaves like STATIC method
20+
return "Indian"
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)