I am learning JAVA and following the book JAVA:The Complete Reference by Herbert Shildt.
I learned about abstract classes in Java, but could not understand reason behind this line:
An abstract class cannot be directly instantiated with new operator.
I searched over web and stackoverflow and followed these questions: Why can't we instantiate a abstract class in JAVA? and Why can't we instantiate an interface or an abstract class in java without an anonymous class method?
In one answer, someone wrote as:
Simply, in a good object oriented program, you should never want to instantiate an abstract class or interface. If you do, the design is probably wrong.
I do not understand why it is important not to instantiate an abstract class for good OOP
design. If someone can give a good explanation, then please help.
Well, let's take an example so that you can understand why in object oriented paradigm the abstract classes are not for instantiation, but for extending :
Suppose that we want to design a Zoo Application , where we present each animal by a set of information, but each type would be presented by it's class, if we want to follow a good practice, then we create a class called
Animal
where we put commons information about all animal ( id, price, entryDate...).This would help designing other classes from a structural point of view
Yout answer is in your given statement:
You should never want to instantiate an abstract class.Because abstract class is an uncomplete class whose intstantiation does not make any sense.You define a behavior for classes which will extend your abstact class. It's more about a guy saying "these classes should look like that, and they got that in common, so fill in the blanks!".
So it is to important to do not instantiate an abstract class as according to right design, purpose of abstract class is different,you use it when used when you want to define a template for a group of subclasses and it makes no sense to instantiate the class itself,
For example: let's say you need to create Dog, Cat, Hamster and Fish objects. They possess similar properties like color, size, and number of legs as well as behavior so you create an Animal superclass. However, what color is an Animal? How many legs does an Animal object have? In this case, it doesn't make much sense to instantiate an object of type Animal but rather only its subclasses.
An abstract class should be something that doesn't make sense if it exists completely on its own.
Take a Vehicle for example. Can you describe a Vehicle without describing a specific type of Vehicle at the same time? No - because Vehicle is just a way of describing common features and behaviours of a group of related objects. Alternatively, you could think of them as concepts.
Your quote:
Is spot on. If you've written an abstract class that you want to instantiate completely on its own, then it isn't an abstract class. If you ever find yourself in this situation, you probably need to carry out another level of abstraction to separate out the abstract parts from the bits that actually start to condense the class into something concrete. The only thing you should want to do with an abstract class is to extend it - to turn it into something less vague (or more concrete if you prefer).
Of course, Java can seem a little contradictory at times. In fact, writing a constructor for an abstract class is perfectly valid:
At first this seems a little stupid. Why can I write a constructor, which is designed to allow you to instantiate an object, when I'm not allowed to instantiate the class? The compiler will even create a default constructor for you if you don't write one!
The answer there is that you are allowed to instantiate an abstract class - you're just not allowed to instantiate it directly using the
new
keyword. But the most important part of abstract classes is that they're designed to be extended.When you instantiate a subclass of an abstract class, you either explicitly or implicitly call
super();
inside the constructor:This actually makes sense when you think about it - you can't have a Vehicle on it's own, but my car that's sat in the car park is definitely a Vehicle. Once I have a concrete extension to my concept of a Vehicle,
Car
in this case, then I can have a Vehicle.Probably the most useful thing that this enables you to do is create generic collections. Because Vehicle is the superclass of all of the different types of Vehicle, I can say:
Or if you prefer not to/can't use the diamond operator (
<>
):This allows me to put any type of Vehicle into that collection:
Although there are many other advantages to this, too numerous to cover in this answer.
You can't instantiate an abstract class, bacause it usually contains abstract methods, which have no implementation. In order to instantiate a class, all of its methods must be implemented. Sub classes would implement the abstract methods, and you would instantiate those sub classes.
However, even if you implement all the methods, you would sometimes define the class as abstract if you want to prevent users of the class from instantiating it. You would define a class as abstract if it is meant to be sub-classed, and doesn't contain all that is required for creating a fully functional object.
As for interfaces, they just declare methods without implementation (with the exception of default methods introduced in Java8), and don't have constructors or class variables. You can't instantiate them without implementing all of their methods.
The reason is that sometimes several concrete classes (classes that can be instantiated) all share common functionality. The common functionality can be shared through inheritance. The common methods are put in the abstract classes. The concrete subclasses can use those methods as they are defined in the abstract class, or concrete subclasses can override the methods to implement special behavior.
As analogy, think of these four things: animal, mammal, human, elephant. There are real creatures called "human" and "elephant", but there are no creatures named "animal" or "mammal". Human and elephants are concrete that way. Animals and mammals are abstractions we created to describe traits common to different creatures. Humans and elephants are kinds of mammals. Mammals are kinds of animals.
In the OO world, we could model these four things as classes. Class Mammal is an abstract class that extends the abstract class Animal. Human and Elephant are concrete classes that extend Mammal. Declaring classes Animal and Mammal as abstract tells others that there is no such thing as an "animal" or "mammal" running around in the woods. Animal and mammal are just ideas we invented to describe things that humans, elephants, and other creatures have in common.
it may seem baffling at first , but you will get used to it
well technically it can't be instantiated because -
it does not contain method definitions.so y waste space on its instantiation!
theoretically-
it defeats the entire purpose of abstract class if u can instantiate it..y go thru all the trouble of creating the concept when it is no different than the ordinary class