Share
Explore

Level 3 Lab Workbook Android Application Development with Objects in Kotlin

Learning Outcome:

Extend the work you did in Lab Workbooks 1 and 2 here in the Level 3 Lab workbook: Start learning about the construction, application, and use of OBJECTS.
In earlier programming paradigms, the programmer designed the program by structuring the flow of CONTROL, Here in Object-Oriented land, you as the programmer will now structure the program by controlling the flow OF DATA. This is the unique, special, and exciting aspect of the Object Oriented Programming Paradigm. Complete the exercises in this Lab Level 3 Workbook from start to end to become practiced in how to do this.

Level 3 Lab Workbook: Objects in Kotlin

Introduction

Welcome to Level 3 Lab Workbook! In this workbook, we will dive into the world of objects and their role in Kotlin's object-oriented programming paradigm. Previously, you learned about functions and control structures. Now, you will explore how to structure your program by controlling the flow of data using objects.
Object-oriented programming (OOP) is a paradigm that focuses on representing real-world entities as objects with properties (data) and behavior (methods). By using objects, you can create modular, reusable, and scalable code that is easier to maintain and understand.

Objectives

Upon completion of this workbook, you will be able to:
Define and create classes in Kotlin
Generate objects from classes
Use encapsulation and access modifiers
Apply inheritance and polymorphism
Understand the role of interfaces

Part 1: Defining Classes and Creating Objects

Step 1: Define a Class

In Kotlin, classes are the blueprints for creating objects. They define the structure and behavior of objects by specifying properties (data) and methods (functions). To define a class, use the class keyword followed by the class name:
kotlin
Copy code
class ClassName {
// properties
// methods
}
Task: Define a simple Person class with a name property and a greet method.
kotlin
Copy code
class Person {
var name: String = ""

fun greet() {
println("Hello, my name is $name.")
}
}

Step 2: Create an Object

To create an object, simply call the class constructor, like so:
kotlin
Copy code
val objectName = ClassName()
Task: Create a Person object and assign it to a variable called person1.
kotlin
Copy code
val person1 = Person()

Step 3: Access Properties and Methods

You can access an object's properties and methods using the dot notation:
kotlin
Copy code
objectName.propertyName
objectName.methodName()
Task: Set the name property of person1 and call its greet method.
kotlin
Copy code
person1.name = "John"
person1.greet() // Output: Hello, my name is John.

Part 2: Encapsulation and Access Modifiers

Step 4: Encapsulation

Encapsulation is the process of hiding the internal details of a class from the outside world and providing a way to access and modify them through accessors (getters) and mutators (setters).
Task: Add a private age property to the Person class and create getter and setter methods for it.
kotlin
Copy code
class Person {
var name: String = ""
private var age: Int = 0

fun greet() {
println("Hello, my name is $name.")
}

fun getAge(): Int {
return age
}

fun setAge(newAge: Int) {
if (newAge >= 0) {
age = newAge
}
}
}

Step 5: Access Modifiers

Access modifiers are used to control the visibility of properties and methods in a class. Kotlin provides four access modifiers:
public (default): visible to everyone
private: visible only within the class
protected: visible within the class and its subclasses
internal: visible within the same module
Task: Modify the Person class to use access modifiers for encapsulation.
kotlin
Copy code
class Person {
var name: String = ""
private set
private var age: Int = 0

fun greet() {
println("Hello, my name is $name.")
}

fun getAge(): Int {
return age
}

fun setAge(newAge: Int) {
if (newAge >= 0) {
age = newAge
}
}

fun setName(newName: String) {
name = newName
}
}

Part 3: Inheritance and Polymorphism

Step 6: Inheritance

Inheritance allows a class to inherit properties and methods from a parent class. This helps avoid code duplication and makes it easier to maintain and extend the application.
To inherit from a class, use the : symbol followed by the parent class name. In Kotlin, classes are final by default, so you need to use the open keyword to allow inheritance.
Task: Create a Student class that inherits from the Person class.
kotlin
Copy code
open class Person {
// ...
}

class Student : Person() {
var studentID: String = ""
}

Step 7: Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. This enables you to write more flexible and reusable code.
Task: Override the greet method in the Student class and call the parent class's greet method using the super keyword.
kotlin
Copy code
class Student : Person() {
var studentID: String = ""

override fun greet() {
super.greet()
println("I am a student with ID: $studentID.")
}
}

Part 4: Interfaces

Step 8: Interfaces

Interfaces define a contract that classes can implement, specifying a set of methods that must be provided. This enables you to define common behavior across multiple classes.
To create an interface, use the interface keyword followed by the interface name:
kotlin
Copy code
interface InterfaceName {
// methods
}
To implement an interface, use the : symbol followed by the interface name:
kotlin
Copy code
class ClassName : InterfaceName {
// implementation
}
Task: Define a Greetable interface with a greet method and implement it in the Person and Student classes.
kotlin
Copy code
interface Greetable {
fungreet()
}

class Person : Greetable {
// implementation

override fun greet() {
println("Hello, my name is $name.")
}
}

class Student : Person(), Greetable {
// implementation

override fun greet() {
super<Person>.greet()
println("I am a student with ID: $studentID.")
}
}

Conclusion

In this Level 3 Lab Workbook, you learned about the basics of object-oriented programming in Kotlin. You learned how to define classes, create objects, and use encapsulation and access modifiers to control the visibility of properties and methods. You also explored inheritance and polymorphism and how to use interfaces to define common behavior across multiple classes.
By mastering object-oriented programming, you can create more modular, reusable, and scalable code that is easier to maintain and understand.
Keep practicing and exploring the world of Kotlin to become a skilled programmer!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.