Class & Object

Class & Object

  • An object is an instance of a class and has its own state and behavior.
  • You can create multiple objects from the same class, each with its own unique state.
Here is an example of a class in Kotlin:
class Car { var name: String var model: String var year: Int fun getInfo(): String { return "$name $model, year $year" } } fun main() { val myCar = Car() myCar.name = "Toyota" myCar.model = "Camry" myCar.year = 2020 println(myCar.getInfo()) }

Object-Oriented Programming Language
Class and Objects are the basic concepts of object-oriented programming language. These support the OOPs’ concepts of inheritance, abstraction, etc.

Class

Like Java, class is a blueprint for objects having similar properties.
We need to define a class before creating an object and the class keyword is used to define a class.
The class declaration consists of the class name, class header, and class body enclosed with curly braces.
Syntax of the class declaration:
class className { // class header // property // member function }
  • Class name: every class has a specific name
  • Class header: header consists of parameters and constructors of a class
  • Class body: surrounded by curly braces, contains member functions and other property
 
  • Both the header and the class body are optional; if there is nothing in between curly braces then the class body can be omitted.
    • class emptyclass
  • If we want to provide a constructor, we need to write a keyword constructor just after the class name.
Creating constructor:
class className constructor(parameters) { // property // member function }

Object

It is a basic unit of Object-Oriented Programming and represents the real-life entities, which have states and behavior.
Objects are used to access the properties and member functions of a class.
In Kotlin, we can create multiple objects of a class. An object consists of:
  • State: It is represented by the attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.
 
Create an object
We can create an object using the reference of the class.
var obj = classname()
 
Accessing the property of the class:
We can access the properties of a class using an object. First, create an object using the class reference and then access the property.
obj.nameOfProperty
 
Accessing the member function of the class:
We can access the member function of the class using the object.
obj.functionName(parameter)
 

Nested Class

In Kotlin, you can define a class inside another class, which is known as a nested class.
Nested classes have access to the members (fields and methods) of the outer class.
class Car { var make: String var model: String var year: Int inner class Engine { var horsepower: Int = 0 var cylinders: Int = 0 fun getEngineInfo(): String { return "$horsepower horsepower, $cylinders cylinders, in a $make $model" } } fun getInfo(): String { return "$make $model, year $year" } } fun main() { val myCar = Car() myCar.make = "Toyota" myCar.model = "Camry" myCar.year = 2020 val engine = myCar.Engine() engine.horsepower = 250 engine.cylinders = 6 println(engine.getEngineInfo()) }
 
  • A class is declared within another class then it is called a nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating an object of the class.
  • Note: Nested class can’t access the members of the outer class, but we can access the property of nested class from the outer class without creating an object for nested class.
 
// outer class declaration class outerClass { var str = "Outer class" // nested class declaration class nestedClass { val firstName = "Praveen" val lastName = "Ruhil" } } fun main(args: Array<String>) { // accessing member of Nested class print(outerClass.nestedClass().firstName) print(" ") println(outerClass.nestedClass().lastName) }
 
  • Note: In Kotlin, to access the member function of nested class, we need to create the object for nested class and call the member function using it.
 
// outer class declaration class outerClass { var str = "Outer class" // nested class declaration class nestedClass { var s1 = "Nested class" // nested class member function fun nestfunc(str2: String): String { var s2 = s1.plus(str2) return s2 } } } fun main(args: Array<String>) { // creating object of Nested class val nested = outerClass.nestedClass() // invoking the nested member function by passing string var result = nested.nestfunc(" member function call successful") println(result) }

Advantages or Disadvantages:

Advantages of using nested and inner classes in Kotlin:

  1. Encapsulation: Nested and inner classes allow you to group related functionality together and keep it separate from the rest of the code, improving code organization and readability.
  1. Reusability: Nested and inner classes can be reused within the same class or across multiple classes, making it easier to write more maintainable code.
  1. Accessibility: Inner classes have access to the members of the outer class, making it easier to share data between them and reducing code duplication.

Disadvantages of using nested and inner classes in Kotlin:

  1. Complexity: Adding nested and inner classes can make the code more complex and harder to understand, especially if there are multiple levels of nesting or if inner classes are used excessively.
  1. Performance: Using nested and inner classes can slow down the performance of your code, especially if they are heavily used or if they are nested to many levels deep.
  1. Debugging: Debugging nested and inner classes can be challenging, especially if there are multiple levels of nesting or if inner classes are used excessively.
  1. Overall, it’s important to use nested and inner classes in a balanced and appropriate way to take advantage of their benefits while avoiding their disadvantages.