Wednesday, February 8, 2012

Java as an object-oriented language

1.       Advantages of OOP
2.       Objects and classes
3.       Inheritance and Polymorphism
4.       Summary
Advantages of OOP
Traditional procedural programming breaks up a programming task into a series of steps, or procedures. However, using a procedural approach can make it difficult to divide your program into independent modules.
This means that your programs can be difficult to maintain, extend and test because secondary effects caused by code modifications are more likely, and errors in one section of code can more easily spread to other parts of a program.
In addition, reusable sections of code are more difficult to design and implement.
Object-oriented programming (OOP) is a design to overcome some of the limitations of procedural programming.
Rather than focusing on modelling the procedural of real-life situations in applications, OOP focuses on modelling the objects that appear in them.
This allows you to break programming tasks into independent, manageable modules – which enables you to extend, update, and reuse the code easily.
As a fully object-oriented language, Java benefits from all the advantages of OOP.

Objects and classes
OOP is based on the concept of software objects.
Software objects share certain characteristics with real-life-objects.
Real-life objects have both a state – information associated with an object – and behaviour.
For example, a customer has a state – such as the customer’s a name, address phone number, and credit rating. The customer also has behaviour – such as ordering a product, and paying a bill.
Sometimes the customer’s behaviour can affect its state. For example, by not paying a product on time – an instance of behaviour – the customer alters its credit rating – or state.
Like a real-life object, a software object also has state and behaviour.
A software object’s state is a maintained as data stored in its variables. Its behaviour is implemented in code in its methods.
Because of the way software objects combine variables and methods in the same object, they can easily be used to model real-life objects.
Examples.
Suppose you want to create a software object called Customer that models a real-life customer.
To model a real-life customer’s state, your Customer object can have a variable called name, address, phoneNumber, and creditRating. To model a customer’s behavior, the Customer object can have methods called getCreditRating, orderProduct, and sendBill.
When a Customer object’s sendBill method is called, this method can change the data in the creditRating variable, and so change the Customer object’s state.
OOP’s combining of associated variables and method in the same object is known as data encapsulation.
The use of data encapsulation in OOP is in contrast to traditional procedural programming, in which the data and the code that acts on the data are treated as separate entities.
One the benefits of encapsulation is that the data contained in the object’s variables can be protected from unauthorized access, as you can ensure that it can be accessed only via the methods of the object.
Both real-life objects and software objects have common characteristics and behaviour that they share with other objects.
For example, a real-life customer is one of many customer that share characteristics such as “has a name”, “has an address”, “can order a product”, and “can receive a credit rating”.

However, each individual customer has its own specific state – such as its own name, address, and phone number.
In object-oriented programming, you can create software blueprints called classes that specify all the characteristics that objects of that class possess. You can reuse this template code every time you want to create a similar object.
For example, you can use a Customer class to create objects that represents different individual customers.

Java applications are built using four key programming constructs.
  • Class
    • Classes are the basic building blocks of the Java language. A class is a template that describes the state and behaviour of an instance of that class. For example, a Customer class might specify that all customers have a name and an address, and can get credit rating and receive bills.
    • All Java applications must include at least one public class – a class that is accessible to other classes.
  • Variable
    • A variable is a named space in memory that stores information associated with a class or an object.
    • For example, a Customer object can have variables containing its name and address.
  • Object
    • An object is a specific instance of a class. When you instantiate a class, you create an object of that class. Each object has similar state and behaviour to other instances of the same class.
    • For example, all objects of a Customer class will have a name and address and be able to get credit ratings and receive bills.
  • Method
    • A method specifies behaviour associated with a class or an object. Methods must have a fixed number of arguments and specify a return type.
    • For example, a Customer object can have a method for getting a credit rating or for receiving bills.
This code declares the public class Customer. This class will declare all variables and methods that relate to Customer objects.
Using the public modifier makes the class available to other classes.

Within the Customer class, you then define its variables. You must also assign a data type to each variable.
For example, you define a String data type for the name and address variables because they consist of numerical and alphabetical characters. You define an int data type for the phoneNumber and creditRating variables as they consist of integers only.


You can then add the getCreditRating method to the Customer class. It returns an integer containing the customer’s credit rating.




You can add other useful methods to the class – such as sendBill.

Note: The void keyword before the method name indicates that the method does not return a value.













Inheritance and Polymorphism
As well as encapsulation, the two other key features of OOP are inheritance and polymorphism.
Inheritance and polymorphism allow you to define new classes in terms of other classes.

In its implementation of inheritance, OOP models the nature of real-life objects. For example, a corporate customer and a home customer both types of customers. They share common features – they both place orders and pay bills.
You can say that a corporate customer “is a” customer. That is, a corporate customer has common characteristics shared by all customers. In addition, it also has its own specific characteristics, such as bulk orders.
In OOP, you can use the “is a” relationship to define a hierarchy of classes.

Suppose you create a Customer class that contains the common variables and methods shared by all Customers objects, such as creditRating, orderProduct, and getCreditRating.
To create a CorporateCustomer class – and a CorporateCustomer “is a” Customer – you do not have to create the CorporateCustomer class from scratch. Instead, you can say that the CorporateCustomer class extends the Customer class.
This code extends the Customer class to create the CorporateCustomer class. The new CorporateCustomer class automatically includes all the variables and methods you included in the Customer class. This is known as inheritance.


Because the CorporateCustomer class inherits from the Customer class, it is called a subclass of the Customer class. The Customer class is a superclass of the CorporateCustomer class.

Once you have created a class by extending a superclass, you can create additional methods and variables for the new class that are not in the superclass.
You can also modify the behaviour of the inherited methods. For example, you can write a CorporateCustomer-specific orderProduct method that differs from the orderProduct method of the Customer superclass. This is called overriding a method.
An overridden method requires the same number and types of parameters to be passed to it as the method it override.
Inheritance and method overriding facilitate an OOP concept called polymorphism.
Polymorphism refers to the ability of an object-oriented language to substitute an object of a subclass for an object of a superclass at runtime and to call relevant method of the subclass instead of the superclass method.
Method overriding means that the same call can produce different behaviour depending on the subclass that is called.
This is possible because inheritance guarantees that all subclasses will have the same methods as the superclass.

 
Suppose you have an Employee object that places an order for a customer by using the Customer object’s orderProduct method.
Using polymorphism, you can replace the generic superclass Customer object with a specific subclass CorporateCustomer object.
Because the CorporateCustomer is a subclass of the Customer class and the CorporateCustomer class inherits the Customer class’s orderProduct method, you do not need to change the Employee’s object code for it to call the orderProduct method of the CorporateCustomer object.
This ability to use an object as if it were an object of its superclass, while still using its overridden methods, is the main benefit of polymorphism
It also means that you can write code that doesn’t change when you introduce new subclasses into an application.

Summary
Object-oriented programming (OOP) enables you to create independent modules of code that are easy to manage, extend, and test.
All objects in OOP have a state and behaviour.  Object states are represented by variables, whereas behaviours are represented by methods. Java applications consist of classes which act as templates for related objects, and their variables and methods.
You can extend classes, creating super-and subclasses. Subclasses inherit all variables and methods from the superclass. You can use polymorphism to reference subtype objects as if they were objects of their supertype.

2 comments:

Post a Comment