Classes and Objects:
The programming paradigm we have learned so far is called Procedural Paradigm. In Procedural programming paradigm, programmer writes a program as collections of independently behaving procedures and makes use of procedure call to access those procedures. As we already discussed in Part8, how procedural programming is a better choice than a simple unstructured programming. It has been a long time, programmers have started thinking about new paradigm called Object oriented paradigm (OOP) which provides a better way to develop big and complex system than procedural programming. We don't want to confuse readers by putting too complex terms and advantages of OOP at this stage, we will discuss when required.
In OOP, a class is a language construct that is used as a template or blue print to create instances of class called objects. This tutorial explains step by step to make the things clear.
Creating Class
Example#1
The example below creates a class called Person. It has two instance variables; name and address and few methods. The instance variables are not accessible outside the class when they are defined as private; we need methods to access them. But public variable can be accessed outside the class. All instance variables should be kept private and there should be some methods to access those variables. Here, we have SET and GET methods which are be public. All methods should be public; otherwise they cannot be accessed outside the class.
After the end of class, we have created a instance "p" of class "Person". "p" is called object of class "Person". Note that, "p" object binds instance variables and methods together, that property of OOP is known as encapsulation.
The output of the above code will be:
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia
Constructor and Destructor
When we create an object constructor method is called, hence it is useful to initialize the instance variables before they are ready to use. Similarly, destructor method is called when we destroy the objects.
Example#2
The code we have in this example produces the same output as in example#1, but has been implemented in a different way. Here we initialize the instance variables using constructors.
This code will also produces the same output as in example#1, that is:
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia
Inheritance
As the name suggests, inheritance is a principle by which child class can inherit the methods form its parent class and the methods performs their original functionality.
Example#3
In this example, we extend a class called Student from the class Person. The subclass Student inherits printMe methods and all instance variables from the parent class Person. The printMe method of Person class has been rewritten with the same name in Student class. If a method with same name exists in parent and child class, the child class object has two version of the same module. In this situation the child class method overrides the parent class method, it known as method overriding. When we call printMe method of Student object, it calls printMe of Student class not the printMe of Person class. Remember that all data members of Person class are available in Student class.
No comments:
Post a Comment