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


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.


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:
kotlinCopy codeclass ClassName { // properties // methods}
Task: Define a simple Person class with a name property and a greet method.
kotlinCopy codeclass 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:
kotlinCopy codeval objectName = ClassName()
Task: Create a Person object and assign it to a variable called person1.
kotlinCopy codeval person1 = Person()

Step 3: Access Properties and Methods

You can access an object's properties and methods using the dot notation:
kotlinCopy codeobjectName.propertyNameobjectName.methodName()
Task: Set the name property of person1 and call its greet method.
kotlinCopy = "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.
kotlinCopy codeclass 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.
kotlinCopy codeclass 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.
kotlinCopy codeopen 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.
kotlinCopy codeclass 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:
kotlinCopy codeinterface InterfaceName { // methods}
To implement an interface, use the : symbol followed by the interface name:
kotlinCopy codeclass ClassName : InterfaceName { // implementation
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.