Member-only story
OOPS Concept
Classes: A class is like a blueprint for creating objects. It tells your code what an object should have and what it can do.
Objects: An object is like a real version of the class blueprint. So, you could create an “Apple” object from the “Fruit” class. Each Apple object would have its own properties, like color and size.
Properties: These are like the features or characteristics of an object.
Methods: Methods are like actions that an object can do.
Encapsulation
Binding data (variables) and the methods (functions) that operate on the data into a single unit called a class.
Encapsulation is achieved using access control.
Here’s a simple example using a “Car” class:
public class Car {
private var speed: Int = 0
internal func startEngine() {
print("Engine started")
}
public func drive() {
speed = 50
print("Car is driving at \(speed) mph")
}
private func stopEngine() {
print("Engine stopped")
}
}
In this example, speed
is kept private, so no one from outside the "Car" class can mess with it directly. The startEngine
method is internal, meaning only code within the same module can call it. And the drive
method is public, so anyone can use it, even code outside the module.
Abstraction
Abstraction in programming is like creating a simplified version of something complex. It’s like…