Difference between revisions of "W1401 Object Oriented Design"

From Coder Merlin
Line 31: Line 31:
</syntaxhighlight>
</syntaxhighlight>


In this example, we have declared the base object as Musicalinstrument. The object has been declared and defined with the key word class. It is customary to use capitalization while defining the base objects. While it is not mandatory to always capitalize the base class, but it is general convention which is followed. While defining the object Musicalinstrument, some property should be defined which will be common for all the instances that we will derive from this object in future viz. smartphone, smart watch etc. Here the property of 'brand' has been declared and defined as the string type. With this definition process the initialization of object 'Gadget' was successfully carried out.     
In this example, we have declared the base object as Musicalinstrument. The object has been declared and defined with the key word class. It is customary to use capitalization while defining the base objects. While it is not mandatory to always capitalize the base class, but it is general convention which is followed. While defining the object Musicalinstrument, some property should be defined which will be common for all the instances that we will derive from this object in future viz. piano, guitar, electric guitar etc. Here the property of 'brand' has been declared and defined as the string type. With this definition process the initialization of object 'Gadget' was successfully carried out.     


== Encapsulation ==
=== Methods ===
=== Methods ===


Line 42: Line 43:
</syntaxhighlight>
</syntaxhighlight>


The tuning  method is a placeholder function which for example say switches the power off of the concerned Gadget when called. Methods like this are often called as abstract since they are very generic and not intended for direct usage. Rather than merely calling the function Error(), it is expected to create a subclass of such method to override the method in order to do something sensible or more specific.   
The tuning  method is a placeholder function which can cause crashes when called. Methods like this are often called as abstract since they are very generic and not intended for direct usage. Rather than merely calling the function Error(), it is expected to create a subclass of such method to override the method in order to do something sensible or more specific.   
Functions that are defined under a class are said as methods since they have the access to the corresponding property. Here, the tuning method has the access to the property brand. This way of organizing the properties and associated functions together is the basis why object oriented designing has been successful in tackling highly complex problems. Such combining practice of property and associated function is called Encapsulation. In other words, class types are known to encapsulate the data (stored properties) and behavior (methods).


There are some key principles of object and its relationship with the class and other objects, which have been detailed as follows:


== Encapsulation ==
== Information hiding ==
== Information hiding ==
== Inheritance ==
== Inheritance ==

Revision as of 03:12, 14 November 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction[edit]

Object Oriented Design is the problem solving approach in programming paradigm where object oriented approach is followed. In simple words object oriented designing is the way of breaking down distinct aspects of a problem into modules called objects and developing a solution by making those objects interact with each other by sending messages or information. This approach is adopted by many programming languages, not only Swift, but also in Java, Java Script, C#, C++, Python, Ruby etc. and dates back to 1950s. While being so, object oriented designing of programming and algorithms have been considered as one of the most efficient ways of problem solving in programming domain while other major paradigm is structured programming approach. While structures programming is considered task-centric, object oriented design is known to be data-centric.

In Object oriented designs, the objects are used to model different components of the problem which ranges from the physical things like a book, a touch on a screen, a database to more complex representations of abstraction like the daily petrol price, fluctuation of rate of interest etc.

Definition of Object[edit]

Object is the encapsulated representation of an entity which combines the data and the associated procedure. The rules and binding ways are formulated in object interface following which the interaction of the objects takes place. Before grasping the advanced and real life usage of objects, for basic familiarity and easy understanding, its better to start with the physical things like taking it literally.

Generally, in object oriented designs, the objects are designed to describe a set of general concepts and then extending to more specific types. Start with the concept of Musical instrument. After defining Musical instrument as an object, you can create (in abstraction) the extended concept of Musical instrument in form of particular instances like Piano, Viola, Violin, Guitar, Electric Guitar etc. which are more specific kinds but can be derived from the same parent object.

Going DeeperGoingDeeperIcon.png
  • The usage of the word 'object' in programming paradigm has its genesis back in late 1950 within the group of Scientists working for Artificial Intelligence based on LISP programming Language at MIT.

Initializing Object in Swift[edit]

Properties[edit]

// Example of declaration of class
class Musicalinstrument{
  // 2
  let brand: string
  // 3
  init(brand: string) {
    //4 
    self.brand = brand
  }
}

In this example, we have declared the base object as Musicalinstrument. The object has been declared and defined with the key word class. It is customary to use capitalization while defining the base objects. While it is not mandatory to always capitalize the base class, but it is general convention which is followed. While defining the object Musicalinstrument, some property should be defined which will be common for all the instances that we will derive from this object in future viz. piano, guitar, electric guitar etc. Here the property of 'brand' has been declared and defined as the string type. With this definition process the initialization of object 'Gadget' was successfully carried out.

Encapsulation[edit]

Methods[edit]

After defining property to the object, the behavior should be added which is done via adding methods.

func tuning() -> String {
  Error("Implement this method for \(brand)")
}

The tuning method is a placeholder function which can cause crashes when called. Methods like this are often called as abstract since they are very generic and not intended for direct usage. Rather than merely calling the function Error(), it is expected to create a subclass of such method to override the method in order to do something sensible or more specific. Functions that are defined under a class are said as methods since they have the access to the corresponding property. Here, the tuning method has the access to the property brand. This way of organizing the properties and associated functions together is the basis why object oriented designing has been successful in tackling highly complex problems. Such combining practice of property and associated function is called Encapsulation. In other words, class types are known to encapsulate the data (stored properties) and behavior (methods).


Information hiding[edit]

Inheritance[edit]

Interfaces[edit]

Polymorphism[edit]

Key Concepts[edit]

Exercises[edit]

References[edit]

[1] [2]