Sharing Knlowledge With The World…

Category: Java

Abstraction in object-oriented programming

Abstraction in object-oriented programming

Abstraction came from the Latin word abs, meaning ‘away’ and trahere, meaning ‘to draw’. So we can define Abstraction in object-oriented programming language as a process of removing or taking away the characteristics from something (object) in order to reduce it to a set of essential characteristics.
Through the Abstraction in object-oriented programming, a programmer shows only the relevant data of an object and omitted all unwanted details of an object in order to reduce complexity and increase efficiency.
In the process of abstraction in object-oriented programming, the programmer tries to ensure that the entity is named in a manner that will make sense and that it will have all the relevant aspects included and none of the extraneous ones.
If we try to describe the process of abstraction in real world scenario then it might work like this:

You (the object) are going to receive your father’s friend from railway station. You two never met to each other. So you would take his phone number fron your father and call him when the train arrives.
On the phone you will tell him that “I am wearing white T-shirt and blue jeans and standing near the exit gate”. Means you will tell him the colour of your clothes and your location so he can identify and loacte you.This is all data that will help the procedure (finding you) work smoothly.

You should include all that information. On the other hand, there are a lot of bits of information about you that aren’t relevant to this situation like your age, your pan card number, your driving licence number which might be must information in some other scenario (like opening a bank account). However, since entities may have any number of abstractions, you may get to use them in another procedure in the future.

Continue Reading

Encapsulation in object-oriented language

Encapsulation

Encapsulation in object-oriented language or in Java is packing of data and function in to single component which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.
Encapsulation in object-oriented language can also be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. (Through functions, which are exposed to outer world.)
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Example:
public class UserPin {
private int pin;
public void setPin (int pin){
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from db and return back
}
}

Encapsulation in object-oriented language makes sure that the user of the class would be unaware of how class stores its data. Also it makes sure that user of the class do no need to change any of their code if there is any change in the class.
As in the above code example we store the ‘PIN’ of the user as integer but say, due to security reason we have to encrypt the ‘PIN’ and then store the encrypted ‘PIN’. And the algorithm that we use for encryption requires ‘PIN’ as String.
public class UserPin {
private int pin;
public void setPin (int pin){
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
As we saw there is no change in the signature of the functions so the user of the class do not have to change his code.
Also we can implement the security layer as the user access the field through the function (known as getter and setter).
public class UserPin {
private int pin;
public void setPin (int pin){
//Validate the value of the PIN
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class).

The whole idea behind encapsulation is to hide the implementation details from users. That’s why encapsulation is known as data hiding.

The idea of encapsulation in object-oriented language is “don’t tell me how you do it; just do it.”

Continue Reading

Inheritance in Object-oriented Programming

Inheritance in Object-oriented Programming

Inheritance in Object-oriented Programming is an important concept. Inheritance defines the relationship between the different objects.

Inheritance is the process wherein characteristics are inherited from ancestors. Similarly, in Java, a subclass inherits the characteristics (properties and methods) of its superclass (ancestor).

An object is able is able to inherit characteristics from another object. In other words we can say that an object is able to pass on its state and behavior to its children. The objects need to have characteristics in common with each other for inheritance to work.

For example Mountain bikes, road bikes, tandem bikes, all share common characteristics of bikes like speed, gear. Also all these bikes have some additional functionalities.

Inheritance in Object-oriented Programming allows classes of mountain bike, road bike, tandem bike to having the properties of class bike and bike became the superclass of all the type of bikes. In Java Programming language each subclass can have one direct superclass and superclass can have unlimited number of subclass.

Inheritance in Object-oriented Programming

Inheritance in Object-oriented Programming

 

One Liner: Inheritance is a mechanism wherein a new class is derived from an existing class.

In Java, classes can inherit or we can say acquire the properties and methods of other classes. A class that inherits from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. The keyword “extends” is used to derive a subclass from the superclass.

There is simple syntax for creating a subclass. Use the extend keyword at the beginning of your class declaration, followed by the name of the class to inherit from (superclass):

class MountainBike extends Bike {

 

   // new fields and methods defining

   // a mountain bike would go here

 

}

This gives Mountain Bike all the same fields and methods as Bike, yet allows its code to focus exclusively on the features that make it unique.

Continue Reading

Interface in Object-oriented Programming

Interface in Object-oriented Programming

Objects interact with outside word through the methods that they exposed.

So Interface can be defined as contract which has enlisted all the methods which must be supported by the objects.

One Liner: An Interface in Object-oriented Programming is a blueprint of a class. It has static constants and abstract methods.

The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritances in Java.

Consider ‘receive’ and ‘reject’ buttons on your phone. You know that these buttons will receive and reject the call and must support by the phone that’s why they are in the phone. The buttons acts as interface between you and phone.

Interface Phone {

Void receives ()

Void reject ();

            }

Interfaces cannot be instantiated, but rather are implemented.Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

In Java IS-A relationship is represented by interface.

Interface in Object-oriented Programming

Interface in Object-oriented Programming

 

Continue Reading

Class in Object-oriented Programming

Class in Object-oriented Programming

Class in object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods).

 

One Liner: A Class in Object-oriented Programming is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class.

Class in Object-oriented Programming

Class in Object-oriented Programming

Let’s try to understand the class with the example of making cookies.

Class is like recipe of how to make cookie. The recipe itself is not a cookie. You can’t eat the recipe. If you follow the recipe then you can make cookie. The cookie which we made from the recipe can be referred as an object.

You can make as many cookies as you would like using the same recipe. Similarly you can create as many instances of a class as you would like.

Now assume that you have to create cookies for different people. How you will identify which cookie is for which person. A simple solution is that write name of the person on the cookie. Reference variables work in similar fashion. A reference variable provides a unique name for each instance of a class. In order to work with a particular instance, you use the reference variable it is assigned to.

Creating class Cookie:

Class Cookie {

String flavor;

String taste;

}

 

Creating cookies for different persons:

Cookie cookieForPerson1 = new Cookie ();

Cookie cookieForPerson2 = new Cookie ();

 

 

Continue Reading

Package in JAVA

JAVA

Suppose you and your friend are freelancers and work as a team. You got a project which has to complete in one week. So you decided to divide the project in two modules and each of you works on different module. In this way you can complete the project in time. Say the name of the module on which you working is ‘module-A’ and the module on which on your friend is working is ‘module-B’.

Now both of the modules have the class name ‘Utility’.

When you combine both the module at the last then you have two ‘Utility’ classes. Now how you would identify which ‘Utility’ class belong to whom. Package is the solution for these types of problems.

Both of you write your code in your package and the both the ‘Utility’ classes belong to different packages, so there is no ambiguity.

One Liner: A Package in JAVA is a namespace that organizes a set of related classes and interfaces.

You can relate packages as being similar to the folders on your computer. You can put images in one folder, application in another and movies in another. Software written in Java programming language can be composed of thousands of classes so it is good practice to organize our packages and classes in by putting then in different packages.

When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.

 

Example of Package in JAVA:

 

package car;

 interface Car {

   public void run();   public void break();

}

 

package car; 

public class Honda implements Car {  

public void run(){}  

public void break(){}

}

 

Continue Reading

What is an Object in Object Oriented Programming ?

Object

An object can be a variable, function, or data structure. In the object oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures

Objects refer the state and behavior of the class.

If you see around yourself you would find real world example for object like pen, bike, mobile, etc. If you notice then all these real world object examples share two common things, i.e. state and behavior. Pen have state (color, length) and behavior (write) also, similarly bike have state (brand, color) and behavior (make call, receive call).

Similarly object in Object-oriented programming also have state and behavior

One Liner: Objects are software bundles of data and related methods.

Anything that the Object in object oriented programming knows (state) and can do (behavior) is expressed by the variables and methods within that object.

Let’s try to understand the OOPs object with the example of real world example:

OOPs Object

As shows in the figure OOP object has variable and methods.

Object in object oriented programming have state (variable) and behavior (methods).

If we try to create similar real world object of dogs then it state would be: Color, Breed, Name and behavior can be: Barking, Walking, etc.

Realworld Object

Continue Reading
PageLines