i need example for java abstraction. where we use
abstraction and why we need abstraction.
Answers were Sorted based on User's Feedback
Answer / brijesh singh
basicall i need to know that u say abstration are use to hide the the unnecessary data but in c or c++ it is also done. through printf and so on we hide the unnecessary data ,so how it is the feature of oops? descrives in details plzzzzzzzz.
Is This Answer Correct ? | 4 Yes | 3 No |
Answer / pushpa
Abstraction is the hiding of information behind other,
usually simpler, information. In programming, abstraction
is often used to hide implementation or system details
behind a simple interface. For example, in the standard
C library, we open a disk file with fopen(); this hides
the details of disk I/O and system calls behind the
abstraction named "fopen".
In Java, abstraction is used specifically in the Object
Oriented Programming (OOP) sense: hiding the details of
the implementation of an object behind the interface
composed of its methods.
For example, the Java Collections Framework defines the
abstraction called java.util.Map. It is an abstract view
of a software object that maps from one set of objects
into another. The class java.util.TreeMap implements the
Map abstraction.
Abstraction in Java is mostly implemented with interfaces
and abstract classes, but you can actually implement it
anywhere that you hide details behind a method.
Here is a simple example. The abstraction of a factorial
of a positive integer N is that it is the product of N
times the factorial of (N-1). The factorial of 1 is 1.
Exactly how we compute the product is irrelevant, all that
matters is that our code obey the factorial abstraction.
public class Factorial {
public static long factorial(int x) {
if (x < 1) throw new RuntimeException("Bad X");
if (x == 1) return 1L;
else {
long y;
y = 1;
while(x > 1) {
y = y * x;
x = x - 1;
}
return y;
}
}
}
So, in this example, we computed the factorial in a
slightly different way than the definition, but it
always gives the right answer! We've hidden the details
behind an abstraction.
You can learn more about abstraction in OOP from these
articles:
http://www.kbcafe.com/articles/OOP.Concepts.pdf
http://www.campusprogram.com/reference/en/wikipedia/a/ab/abstraction__computer_s...
http://javaboutique.internet.com/tutorials/JavaOO/index.html
Is This Answer Correct ? | 2 Yes | 6 No |
The following program reads data (details of students) from a file named students.txt and converts it into e-mail addresses. The results are written to a file named studentemail.txt. students.txt consists of a number of lines, each containing the data of a student in colon delimited format: Last Name:First Name:Student Number Each input record is converted to an e-mail address and written to studentemail.txt in the following format: the first character of the last name + the first character of the first name + the last four digits of the student number + “@myunisa.ac.za” import java.io.*; public class EmailConverter { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader ("students.txt")); PrintWriter output = new PrintWriter(new FileWriter ("studentemail.txt")); String line = input.readLine(); while (line != null) { // Extract the information for each student String[] items = line.split(":"); // Generate the email address String email = "" + items[0].charAt(0) + items[1].charAt(0) + items[2].substring(4,8) + "@myunisa.ac.za"; email = email.toLowerCase(); // Output output.println(email); line = input.readLine(); } input.close(); output.close(); } } Rewrite the class so that it handles possible errors that may occur. In particular, it should do the following: • It should catch at least three appropriate exceptions that might occur, and display suitable messages. • At this stage, the program will not run correctly if there is an empty line in the input file. Change the program so that if an empty line is encountered, an exception is thrown and the empty line is ignored. This exception should be handled with the display of a suitable error message. • Before the e-mail address is added to the output file, check if the student number has 8 digits. If not, throw an InvalidFormatException (which the program should not handle itself)
What is meant by bytecode?
How are variables stored in memory?
What kind of variables can a class consist?
Program to print 1 1 2 1 2 3 1 2 3 4 like that
Difference between static methods, static variables, and static classes in Java.
Can we have any code between try and catch blocks?
how do you store phone numbers using java collections
Which variables are stored in stack?
What do you mean by static variable?
What are the uses of synchronized keyword?
Explain different forms of polymorphism?