Engineering Full Stack Apps with Java and JavaScript
Object-Oriented Development uses "objects" to model real world objects. A car or a laptop can be considered as object. While traditional programming views software as a collection of functions, an object oriented system concentrates on the objects that combines data and functionality together.
The traditional approach mostly focussed on structured system development and the technique used was usually referred to as the Structured Analysis and Design Technique (SADT).
An object is something which has its own identity and can be easily compared to a real world object like a car or a laptop. An object contains a state and some behavior. The state of an object is the properties of the object at a particuler time, and behavior is the functions it will perform. The behaviour of an object is usually described using methods, and these methods will be part of the object itself. So you don't have to refer anywhere else for object's functionality, whereas in function based traditional approach you need to remember all the methods and their location. For instance, in java, the state of an object is the set of values of an object's variables at any particular time and the behaviour is implemented as methods.
Objects are grouped into a class. A class can be defined as a group of objects with the same structure and behaviour. Class can be considered as the blueprint or definition or a template for an object and describes the properties and behavior of that object, but without any actual existence. An object is a particular instance of a class which has actual existence and there can be many objects (or instances) for a class. In the case of a car or laptop, there will be a blueprint or design created first and then the actual car or laptop will be built based on that. We do not actually buy these blueprints but the actual objects.
In Object-Oriented Development, we apply object orientation across every system development activity such as requirement analysis, design, programming, testing, and maintenance. For instance, an object oriented analysis (OOA) will usually have the following steps:
Identifying the system functionality
Identifying the involved objects
Recognizing the object classes
Analysing the objects to fulfil the system functionality
Object-Oriented Programming (OOP) is based on object oriented features like Encapsulation, Polymorphism, Inheritance and Abstraction. These features are usually referred to as the OOPs concepts. We will see more about analysis, design, testing and maintenance later.
Encapsulation
Encapsulation is the process of wrapping up of data ( as properties) and behavior (as methods) of an object into a single unit (a class). Encapsulate in plain English means to enclose or be enclosed in or as if in a capsule.
Encapsulation enables data hiding, hiding irrelevant information from the users of a class and exposing only the relevant details required by the user. We can expose our operations to the outside world and hide the details of what is needed to perform that operation. We can thus protect the internal state of an object by hiding its attributes from the outside world, and then exposing them through setter and getter methods. Now modifications to the object internals are only controlled through these methods.
Example from real world: Consider the smart phone you are using now. You are not worried about the internal operations of the smart phone. You only know and care about the operations or functions it exposes to you such as making call, sending SMS or using your apps.
Inheritance
Inheritance describes the relationship between two classes. A class can get some of its characteristics from a parent class and then add unique features of its own. For example consider a Vehicle parent class and its child class Car. Vehicle class will have all common properties and functionalities for all vehicles in common and Car will inherit those common properties from the Vehicle class and then add those properties which are specific to a car. Here, Vehicle is known as base class, parent class, or superclass. Car is known as derived class, Child class or subclass.
In multiple inheritance a class can inherit from more than one parent, but due to its complexity some languages like Java doesn't fully support it. In multi-level inheritance there will be many levels of inheritence like A inheriting from B and B inheriting from C and so on.
Polymorphism
Polymormism means that one operation can be used for different purposes. Poly means many and morph means form. For instance, Java supports different kinds of polymorphism like oveloading, overriding, parametric etc. In overloading, the multiple methods having same name can appear in a class, but with different signature. Overriding is defining a method in a subclass with the same name and type signature as a method in its superclass and the overriden method is called at runtime based on the object at runtime.
Abstraction
In plain English, abstract means a concept or idea not associated with any specific instance and does not have a concrete existence. Abstraction in OOP emphasizes what is important and what is not important at a particular level of detail. Abstraction refers to the ability to make a class abstract at a level and define some of the behaviour at each level, relevant to the current perspective. Abstraction allows the programmer to focus on few concepts at a time. Java provides interfaces and abstract classes for describing abstract types.
Coupling and cohesion are two important concepts in the Object Oriented design and hence we will briefly discuss it here.
Coupling is the degree to which one class knows about another class. If the knowledge is only through exposed interfaces (data hiding), it is called loosely coupled. If the knowledge is more like accessing data members directly, it is called tightly coupled. We should try to make our code as loosely coupled as possible. Even though you make some change in a class adhering strictly to the class's API, tight coupling can make other classes that use this class not working properly after the change.
The term cohesion is used to indicate the degree to which a class has a single, well-focused purpose. The more focused a class is, the higher its cohesiveness, which is a good thing. The key benefit of high cohesion is that such classes are typically much easier to maintain than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.
In summary, loose coupling and high cohesion are desirable, whereas tight coupling and less cohesion can lead you to problems in the long run.
Some of the advantages of object oriented software development are:
Less maintenance cost mostly because it is modular.
Better code reusability due to features such as inheritance and hence faster development.
Improved code reliability and flexibility
Easy to understand due to realworld modelling.
Better abstraction at object level.
Reduced complexity during the transitions from one development phase to another.
Comments
using getters and setters
Hi Heartin,
I am a Beginner in java. I have doubt on the following,
why we use getters and setters, it has the same use of making the properties public?
Thanks in advance,
Chinnu
Good question and you are
Good question and you are somewhat correct. Efectively it has the effect of making the variable public and few even consider it as non-object oriented.
But in certain cases, it does make sense, like to have some kind of extra validation for the data before setting or even having a an additional logic for getting data.
Consider a linked list. Imaging that you have a size variable and people are directly accessing it to get size. But later you decide to change the logic to calculate the size every time someone ask for it rather than storing it in a variable. With direct access to the variable, you can never do this without changing client code; but if they were using a getSize() method, they could easily change "return size;" with your new logic and the client code will never need to be changed.
use of setters and getters
Thanks Heartin..now it is clear for me