Abstraction

Hi,

This is Ram...new to Java Technology

Very much confused withAbstraction

What i understand from Abstraction is::

For example: I'm using methods defined in Object class in my java class..

So,I dont know how the method is implemented in Object class..But I'm using those methods....like equals(),hashcode(),String toString()...

and I'm getting the expected result...

So,I just wan't to confirm is this Abstraction?

If not which is Abstraction ?Searched in internet...unable to find proper result...

Kindly anyone help me in understanding this concept....

Thanks,

Ram

[652 byte] By [ram-javaa] at [2007-11-27 11:39:13]
# 1

By abstraction, do you mean 'abstract' methods and classes?

If so, an abstract class is able to define abstract methods.

When a normal class extends the abstract class, the normal class must declare all the abstract methods in the abstract class that it is extending, and must have the same parameter types and return types.

e.g

public abstract class AbstractClass {

public abstract void abstractMethod(); // Notice these must have semi-colons at the end.

public abstract int abstractMethod1(); // Notice the return type of 'int'

}

public class NormalClass extends AbstractClass {

public void abstractMethod() { // This method MUST be declared for it to compile

}

public int abstractMethod1() { // This MUST have the return type of 'int' as it was defined with this in the AbstractClass

return 0;

}

public void method() { // This method DOESNT have to be decared, as it isnt defined in the AbstractClass.

}

}

Hope this helps.

Futurisdom_Developera at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

Thanks for Explanation..

I'm aware of Abstract class and methods....

I'm not clear with the "Abstraction" concept which also comes under OOPS

what is the difference between Abstraction and Encapsulation?

need help..

Thanks,

Ram

ram-javaa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 3

http://en.wikipedia.org/wiki/Abstraction_(computer_science)

http://en.wikipedia.org/wiki/Information_hiding

Edit:

btw .. top results when googling for either word

Message was edited by:

dwg

dwga at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 4

> Hi,

>

> Thanks for Explanation..

>

> I'm aware of Abstract class and methods....

>

> I'm not clear with the "Abstraction" concept which

> also comes under OOPS

>

> what is the difference between Abstraction and

> Encapsulation?

Quoting from a post in Yahoo!Answers:

"Abstraction is a way to remove the association of the behavior of an object with the actual details behind the scenes which implement that object's behavior.

This 'abstraction' is usually accomplished through the use of base classes with virtual functions; each derived function provides the details that implement the behavior behind that abstraction.

A simple example is using a base class "Animal", with a virtual function "Walk". In the case two-legged versus four-legged animals, both of them walk, but the actual mechanics are different. The "Walk" method abstracts the actual mechanics behind the walking that each "Animal" does."

So an Animal class would have an abstract method called walk().

The Animal class would be abstract, too, because in Java a class has to be declared abstract if it contains an abstract method.

And, quoting from another post in GeekInterviews:

"To design effectively at any level of abstraction, you need to be able to leave details of implementation behind and think in terms of units that group those details under a common interface. For a programming unit to be truly effective, the barrier between interface and implementation must be absolute. The interface must encapsulate the implementation--hide it from other parts of the program. Encapsulation protects an implementation from unintended actions and inadvertent access.

(...)

It might seem, at first, that hiding the information in instance variables would constrain your freedom as a programmer. Actually, it gives you more room to act and frees you from constraints that might otherwise be imposed. If any part of an object's implementation could leak out and become accessible or a concern to other parts of the program, it would tie the hands both of the object's implementor and of those who would use the object. Neither could make modifications without first checking with the other.

(...)

Suppose, for example, that you're interested in the Faucet object being developed for the program that models water use and you want to incorporate it in another program you're writing. Once the interface to the object is decided, you don't have to be concerned as others work on it, fix bugs, and find better ways to implement it. You'll get the benefit of these improvements, but none of them will affect what you do in your program. Because you're depending solely on the interface, nothing they do can break your code. Your program is insulated from the object's implementation.

Moreover, although those implementing the Faucet object would be interested in how you're using the class and might try to make sure that it meet your needs, they don't have to be concerned with the way you're writing your code. Nothing you do can touch the implementation of the object or limit their freedom to make changes in future releases. The implementation is insulated from anything that you or other users of the object might do."

In this case, the programmer would define a Faucet interface with the methods void open(), void close(), int getFlowPercentage(), boolean isOpened(), etc.

A programmer is then free to implement the Faucet interface without worrying about how other programmers would write the code to implement it.

A Google search for Programming Patterns could also help.

Regards,

java_knighta at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks..

Some doubts

Whether the example which i had quoted above can be said as Abstraction?

ram-javaa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 6

Sure, a method is one form of abstraction.

The implementation has been abstracted away so you can just call the method.

dwga at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 7

Thanks..

Can you give me one more example of that sort..

so that my doubt will be cleared

ram-javaa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 8

> Thanks..

>

> Can you give me one more example of that sort..

>

> so that my doubt will be cleared

Google can clear your doubt. :)

Yannixa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 9

public void someLongMethod() {

// some code doing specific thing A

...

// some code doing specific thing B

...

// some code doing specific thing C

}

by refactoring the A,B,C parts into their own methods, we are abstracting those details away from the original methodpublic void aMuchShorterAndNicerMethod() {

doPartA();

doPartB();

doPartC();

}

private void doPartA() {

// some code doing specific thing A

}

private void doPartB() {

// some code doing specific thing B

}

private void doPartC() {

// some code doing specific thing C

}

dwga at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 10

From the example....

what i got is...

A method accessing the stuff in methods A , B and C...

There is no necessary for the Program to Know how it is implemented....

The Program which calls those methods know only that if they give some input they will get their required output....

Is that right?

Thanks,

Ram

ram-javaa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 11

Thanks for the code...

I understood it...

Thanks,

Shriram.

ram-javaa at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...
# 12

> There is no necessary for the Program to Know how it

> is implemented....

>

> The Program which calls those methods know only that

> if they give some input they will get their required

> output....

>

> Is that right?

Exactly right.

dwga at 2007-7-29 17:24:51 > top of Java-index,Java Essentials,Java Programming...