Posts

Showing posts with the label Polymorphism

Object-oriented programming (OOP) in Swift.

Image
         Object-oriented programming (OOP) in Swift Object-oriented programming (OOP) is a programming paradigm that is based on the concept of "objects", which can contain both data and behavior. The fundamental concepts of OOP are encapsulation, inheritance, and polymorphism. These concepts are collectively known as the "four pillars" of OOP and are often referred to as the OOP concepts. Encapsulation: Encapsulation is the process of hiding the internal details of an object from the outside world. It is achieved by using access modifiers like "public", "private", and "protected" to control the visibility of the object's properties and methods. This allows for data hiding and data protection, and improves the security of the object. encapsulation is implemented using access control levels and access modifiers. Swift provides three levels of access control: open , public , internal , fileprivate , and private . Open : Classes, metho...

Method Overloading and Method Overriding.

Image
  In Swift, method overloading and overriding are two different concepts used to define methods in a class. Method overloading , also known as " polymorphism ," is the ability for a class to have multiple methods with the same name but different parameters. When a method is called, the correct version of the method is chosen based on the number and types of arguments passed to the method. For example, you can have two methods with the same name calculate but with different parameters: Method overriding, on the other hand, is the ability for a subclass to provide a new implementation for a method that is already defined in its superclass. The subclass can override the method by providing a new implementation with the same method signature. For example, you can have a base class Vehicle with a method drive() and a subclass Car that overrides this method to provide a new implementation: It's important to note that when you override a method, you must use the override ke...