OOP
Object Oriented Programming refers to a programming paradigm that arose long before I was born, and will persist long after I am gone. The idea is to bundle functionality and data into Classes. Classes are the blueprints which you use to create a unique object that inherits all the functions and values of the class.
A good example is creating a Customer class, which can have attributes(values) such as: Balance, Credit, Orders, History. Also, functions in classes are called methods, which are specific to the class such as: add_balance, remove_credit, view_history. When you create a Customer object, it is said that you instantiate an object, which gives the object all the functionality of the Customer class(Methods, and Attributes). If we have customers Tom and Alison, then they will both have attributes: Balance, Orders, and the methods: add_balance, view_history. This allows Tom and Alison to be Customers, who share enough characteristics that an object oriented approach is justifiable.
Attributes & Methods
Attributes, as explained above, are simply values that are attached to a given object. We have a customer who has $50 balance, $0 credit, these are attributes of the object.
Methods are functions that are useable only by the class. We have a customer who wants to view their history to see their purchasing patterns. The customer simply uses the method view_history and can view the history associated with his object. In addition, the method for view_history would most likely have attributes that consist of a list of his recent transactions. Often methods and attributes are used together, although not always.
Inheritance
Inheritance is the idea of inheriting the methods and attributes of a given class. The idea here is that if you have two extremely similar customers, you wouldn't create another class again.
A good example is a Normal customer class(NCC) and a Business customer class(BCC). The BCC is a similar to NCC, but the BCC would need some unique features, perhaps: tax information, bulk-orders, contract information. In this case, it makes sense to define a new class, but since we want most of the features of the NCC, we inherit from the NCC. Now we create a BCC that has identical methods and attributes of NCC. Now we customize our BCC by creating attributes and methods that will supersede the NCC, or perhaps entirely new methods and attributes.
I like to think of inheritance as a "is-a,but" relationship, therefore a BCC is a NCC, but with extra features. Although, I am not sure what to do in the case where you want a class to inherit only specific features. In this case, perhaps we can define the methods and attributes and use the pass approach as placeholders.
Encapsulation
Encapsulation is the idea of only allowing methods to interact with a object. Certain languages restrict access and modification to an object, while others allow it, and hope the people using the program will understand. I agree with the former approach, it seems logical to disallow random functions to interact with objects, because it can lead to errors and frustration if someone isn't knowledgeable of how the code works. A neat idea is the public, private, and protected approach to objects that certain languages use. Although, I must confess I haven't thought much about encapsulation, which hopefully should change in the new few weeks.
No comments:
Post a Comment