Ticker

6/recent/ticker-posts

Abstraction & Method Overloading



Abstraction

It the mechanism by which we hide data that is not required by a user. The advantage of abstraction is that the user can work only with the required data and does not need to view unwanted data.

Let's take the example of the car to understand the concept of abstraction. As the owner of the car, you must know how to drive it. This obviously implies that you know about the various components of the car and how to use them. Lets take example, you know that the accelerator pedal is used to increase the speed of the car and pressing the brake pedal stops it. To perform these simple actions, you just need to know how to use these components, and not how they function. Similarly, in OOP, you need to know about the specific classes or methods that are called to implement specific program logic, without knowing how these classes or methods function

Below Listing 1: implementing the Concept of Abstraction

class Abstraction

{

private int x ;

private string str;

private double bal;

private double sal;

private double credit;

public void disp(int anum, String name, double bal )

x=anum;

str=name;

bal=bal;

System.out.println ("Account number=”+x);

System.out.println ("Customer name"+str);

System.out.println (Total Balance=$”+bal);

}

}

public class Abstraction instance {

public static void main(Strtng args[])

{

Abstraction b=new Abstraction ();

b.disp(125,"Deepak",500000);

}

}

 

Up In Listing 1, we have assumed that an accountant working in a finance company is liable to view a customer's information such as his account number, name, and balance amount. However, the accountant is not permitted to view confidential information related to the company, such as staff salaries, the profit and loss of the company, or the total amount of loan that the company has given to its customers. Therefore, the data that an accountant is not authorized to view can be abstracted from his view, whereas the same data can be viewed by a manager of the company. In this way, data can be abstracted according to a user's point of view. In Listing 1, first, a class named Abstraction is created in which five private variables, x, str, bal, sal, and credit are declared. Next, the disp () method is created, which can accept three parameters: anum, name, and bal. The values received by the anum, nam, and bal parameters are assigned to the variables x, str, and bal respectively. The disp() method accesses only customer-related information such as account number of a customer, his name, and the total balance in his account; however, this method cannot access the confidential information of the company, such as an employee's salary or the total amount of loan the company has given to its customers.This means the sal and credit variables cannot be accessed by the disp () method, because these variables are hidden from the method.

Method Overloading: When a subclass includes a method definition that has the same name, number and types of parameters as a method already defined in its superclass, the definition of the method created in the subclass replaces the definition of the method created in the superclass.In other words, you can create a new definition for an already existing method through method overriding.

Suppose you have the calculate () method in the Vehicle superclass, which is used to calculate the speed of a vehicle, but cannot display the speed of the vehicle. However, you want to create a method that can both calculate and display the speed of the vehicle. Now, rather than creating a new method, you can extend the Vehicle superclass and override its calculate° method by appending the code in it to display the speed in the superclass.In this way, method overriding saves your time and helps in creating a new application quickly. Listing 2 declares the Vehicle superclass and Car subclass to allow you to understand the concept of method overriding:

 

For more understanding you can also read below mentioned topic from online:

abstraction in java atoz knowledge


Post a Comment

0 Comments