a programming paradigm that focuses on the use of objects to represent real-world entities and concepts. • In OOP, an object is an instance of a class, which is a blueprint or template for creating object
template for creating objects. • It defines the properties and methods that an object will have, but does not actually create the object itself. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
a class. • A class defines the properties and methods that an object will have, while an object is an actual instance of that class with specific values for its properties.
within a class and describe the characteristics of an object. • These properties are accessed and manipulated using the dot notation on an instance of the class. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
inside this class only (including all its members). • protected means that the member has the same visibility as one marked as private, but that it is also visible in subclasses. • internal means that any client inside this module who sees the declaring class sees its internal members. • public means that any client who sees the declaring class sees its public members.
is used to initialize an object when it is created. Constructors have the same name as the class and do not have a return type. class Car(val brandName: String, val engine: Engine, val price: Double) { fun printCarDetails() { println("Car details") println("Brand Name: $brandName") engine.printEngineDetails() println("Price: $price") } }
template for creating objects. • It defines the properties and methods that an object will have, but does not actually create the object itself. open class Engine(var engineType: String) { open fun start() { println("Starting engine...") } }
cannot be instantiated, while an interface has no constructor and cannot be instantiated directly. • A class can only inherit from one abstract class, but it can implement multiple interfaces. • An abstract class can have fields and properties with initial values, while an interface can only have constants. • An abstract class can be marked as open to allow subclassing, while an interface is always open by default. • An abstract class is typically used when you want to provide a base implementation for a set of classes that share common behaviors, while an interface is used when you want to define a set of methods that multiple unrelated classes can implement to provide a specific set of behaviors.
allows objects of different classes to be treated as if they are of a common type. fun testStartEngine(engine: Engine) { engine.start() } val dieselEngine = DieselEngine("Diesel") val petrolEngine = PetrolEngine("Petrol") val electricEngine = ElectricEngine("Electric") val hybridEngine = HybridEngine("Hybrid") testStartEngine(dieselEngine) // Output: Starting diesel engine... testStartEngine(petrolEngine) // Output: Starting petrol engine... testStartEngine(electricEngine) // Output: Starting electric engine... testStartEngine(hybridEngine) // Output: Starting hybrid engine...
class that is designed to hold data. It is defined using the data keyword in Kotlin. Data classes are used to create objects that are used to represent data that is being transferred between different parts of an application. data class Person(val name: String, val age: Int)
methods for data classes, including: • equals(): compares two objects of the same data class for structural equality. • hashCode(): returns a hash code value for the object, which is used for hashing in hash-based data structures. • toString(): returns a string representation of the object. • copy(): creates a copy of the object with some or all of its properties changed.
class • By default, nested classes are static and cannot access the instance variables of the outer class • You can access the outer class instance using the this@OuterClassName syntax
from outer class" class NestedClass { fun printMessage() { println("Message from nested class: ") // Uncomment the following line to access the outer class message // println([email protected]) } } }