Skip to content

Commit

Permalink
Inheritance, Polymorphism, Abstract example
Browse files Browse the repository at this point in the history
  • Loading branch information
htk007 committed Aug 29, 2020
1 parent 5d86b6b commit 9f369d8
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/Animal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hasankaya.oopexample

open class Animal {
open fun breathe(){
println("Breathing!")
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/Dog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hasankaya.oopexample

class Dog: Animal() {
fun test(){
super.breathe()
//miras aldığımız sınıftaki fonksiyonu çalıştırır.
}

override fun breathe(){
println("dog breathe")
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/HouseDecor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hasankaya.oopexample
//interface içerisinde property yazmak
interface HouseDecor {
//%100 abstract
// kotlin -> no initialize for properties
//java -> must initialize

var roomName: String
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/Instrument.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hasankaya.oopexample
//interface içerisinde fonksiyon yazmak
interface Instrument {
//java -> no body
//kotlin -> body olabilir de olmayabilir de
fun info()

}
18 changes: 17 additions & 1 deletion app/src/main/java/com/hasankaya/oopexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,24 @@ class MainActivity : AppCompatActivity() {
println("(1): \n"+shopOne.shopName + "\n" +shopOne.shopCode.toString() + "\n"+ shopOne.mallId.toString())
shopOne.shopCode=2000602
println("(2): \n"+shopOne.shopName + "\n" +shopOne.shopCode.toString() + "\n"+ shopOne.mallId.toString())


//static polymorphism
var matcalculate = Matcalculates()
println(matcalculate.sum())
println(matcalculate.sum(1,2))
println(matcalculate.sum(1,2,3))

//dynamic polymorphism
val animal= Animal()
animal.breathe()

val fredy = Dog()
fredy.test()//miras aldığımız sınıftaki methodu çalıştıran
fredy.breathe()//overwrite ettiğimizi çalıştıran

//interface
var myPiano =Piano()
print(myPiano.roomName)

}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/MallShops.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hasankaya.oopexample

class MallShops(shopName: String, shopCode: Int, mallId: Int) : Shops(shopName, shopCode, mallId) {

fun floorType(){
println("floor type")
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/Matcalculates.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hasankaya.oopexample

class Matcalculates {
fun sum(): Int {return 0}
fun sum(x:Int, y:Int):Int{return x+y}
fun sum(x:Int, y:Int, z:Int): Int{return x+y+z}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/hasankaya/oopexample/Piano.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hasankaya.oopexample

class Piano: HouseDecor, Instrument {
override var roomName: String
get() = "kitchen"
set(value) {}

override fun info() {
TODO("Not yet implemented")
}
}
25 changes: 19 additions & 6 deletions app/src/main/java/com/hasankaya/oopexample/Shops.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package com.hasankaya.oopexample

class Shops(shopName: String, shopCode: Int, mallId:Int ) {
//biz sınıfların başına open yazmazsak, default olarak final tipinde tanımlanır ve başka yerde kullandırmaz.
//eğer open yazarsak inheritance alabiliriz .
open class Shops(shopName: String, shopCode: Int, mallId:Int ) {
//encapsulation örneği
var shopName: String?= shopName
private set //değişime izin vermiyoruz bu şekilde.
get //verinin okunmasına izin veriyoruz.
set
get
var shopCode: Int?= shopCode
set
get
var mallId: Int?=mallId
get
private set
get//verinin okunmasına izin veriyoruz.(encapsulation)
private set//değişime izin vermiyoruz bu şekilde.(encapsulation)

private val softwareType: String="Enterprice"

fun returnSoftwareType(password: String):String{
if(password=="hasan"){
return softwareType;
}else{
return "wrong pass!"
}
}



}

0 comments on commit 9f369d8

Please sign in to comment.