We have abstract classes as well as abstract method in java but why we do not have abstract variable in java??
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
We have abstract classes as well as abstract method in java but why we do not have abstract variable in java??
vanshika..
Can you give an example of why it would be useful?
If you don't understand my answer, don't ignore it, ask a question.
The reason is same as abstract class and abstract method is useful.
vanshika..
Why is an abstract class or method useful? I've been trying to understand this for days!
Interfaces are very similar to abstract classes, so I'll try to describe on an interface example.
Consider List.
Now consider ArrayList and LinkedList, they're implementations.
It turns out, that this implementations have different performance and each is efficient in different situations. So it might be crucial for you to select the right one for your project.
Now suppose, you're writing a project, who deals with lists extensively. And you have lots of methods, who take, say ArrayList as a parameter. Then suddenly you realize, that ArrayList is completely not what you want, that LinkedList would be right solution. What happens next is tedious process of changing signatures of lots of methods, changing their arguments from "ArrayList" to "LinkedList".
Now imagine, that in the same project all the methods take List as arguments. What happens if you want to change implementation of a collection from ArrayList to LinkedList? Well, you might need to change a declaration of it from "new ArrayList<Foo>()" to "new LinkedList<Foo>()". But you don't have to change signatures of methods: methods don't care about implementation, they care about interface.
So, as you see, the flexibility increases greatly.
abstract variable? funny name. You need to understand the definition of abstract class(similiar to virtual class in C++). The abstract class is for later implementation use in its inheritance classes. And it is could not initialzed at all. Simplely because it is not a 'complete' class. The usefulness for this kind of class is on its inheritance classes which could implement its abstract methods in its own purpose. that is the beauty of abstract class.
I understand what you are saying. I'm just trying to figure out why I would use abstract classes in developing an application program. Can you give me a business example?