w3resource

Kotlin Class: Person class with nested ContactInfo class


Write a Kotlin program that creates a class 'Person' with a nested class 'ContactInfo' to store the person's contact information.


Pre-Knowledge (Before You Start!)

Before attempting this exercise, you should be familiar with the following concepts:

  • Classes and Objects: Understanding how to define and use classes in Kotlin.
  • Nested Classes: A class defined inside another class. It can be static or non-static (inner class).
  • Inner Classes: A nested class that has access to the outer class's properties.
  • Null Safety: Using nullable types and the safe call operator (?.) to prevent null pointer exceptions.
  • Encapsulation: Using private properties and functions to control access to data.

Hints (Try Before Looking at the Solution!)

Try to solve the problem using these hints:

  • Hint 1: Define a class named Person with a property for the name.
  • Hint 2: Inside the Person class, define a nested class ContactInfo with properties for email and phone.
  • Hint 3: Use an instance variable to store contact information in the Person class.
  • Hint 4: Implement a method setContactInfo() to initialize the contact information.
  • Hint 5: Implement a method displayContactInfo() that prints the contact details if they are available.
  • Hint 6: In the main() function, create a Person object, set the contact details, and display them.

Sample Solution:

Kotlin Code:

class Person(val name: String) {
    private var contactInfo: ContactInfo? = null

    fun setContactInfo(email: String, phone: String) {
        contactInfo = ContactInfo(email, phone)
    }

    fun displayContactInfo() {
        contactInfo?.let {
            println("Contact Information:")
            println("Email: ${it.email}")
            println("Phone: ${it.phone}")
        }
    }

    inner class ContactInfo(val email: String, val phone: String)
}

fun main() {
    val person = Person("Mitko Leida")
    person.setContactInfo("[email protected]", "01234567")
    person.displayContactInfo()
}

Sample Output:

Contact Information:
Email: [email protected]
Phone: 01234567

Explanation:

In the above exercise -

  • First we define a "Person" class with a nested class "ContactInfo". The "Person" class has a name property and a nullable contactInfo property of type ContactInfo.
  • The "setContactInfo()" function sets the person's contact information. It takes email and phone as parameters and creates an instance of the nested ContactInfo class to store the information.
  • The "displayContactInfo()" function displays the person's contact information. It checks if the contactInfo property is not null and prints the email and phone.
  • Inside the "ContactInfo" class, we have email and phone properties that store contact information.
  • In the main function, we create an instance of the Person class and set the contact information using the "setContactInfo()" function. We call the "displayContactInfo()" function to print the contact information.

Kotlin Editor:


Previous: Shape, circle, and rectangle classes with area calculation.
Next: Logger class with companion object for logging.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.