Developer community
+1 vote
103 views
why don't create object of Abstract class n Interface in java?
asked by Jr Member (230 points) 2 4 | 103 views

4 Answers

+1 vote
An abstract type is defined largely as one that can't be created. You can create subtypes of it, but not of that type itself. The CLI will not let you do this. An abstract class has a protected constructor (by default) allowing derived types to initialize it. For example, the base-type Stream is abstract. Without a derived type where would the data go? What would happen when you call an abstract method? There would be no actual implementation of the method to invoke.
answered by
+1 vote


The short answer: because that's how they are defined.

Definitions...

Instantiate - to create one instance of a class. The newly created object is a "kind of" the class.

Interface - a declaration of methods that are expected of a class. If the class declares they implement the interface, it must provide implementations for all the methods declared by the interface.

Abstract Class - a class that cannot be instantiated. Often it will include one or more methods that are also declared abstract and must be implemented by subclasses for the subclass to be concrete (opposite of abstract), and therefore able to be instantiated. Does not have to include abstract methods; the documentation may otherwise specify what is required of a valid concrete subclass.

answered by
+1 vote
The reason behind this is , the abstract class contains 
abstract methods which cant do any job. Calling such 
function is meaningless. So it is not allowed to 
instantiate abstract class
answered by
+1 vote

Because it's abstract and an object is concrete. An abstract class is sort of like a template, or an empty/partially empty structure, you have to extend it and build on it before you can use it.

Take for example an "Animal" abstract class. There's no such thing as a "pure" animal - there are specific types of animals. So you can instantiate Dog and Cat and Turtle, but you shouldn't be able to instantiate plain Animal - that's just a basic template. And there's certain functionality that all animals share, such as "makeSound()", but that can't be defined on the base Animal level. So if you could create an Animal object and you would call makeSound(), how would the object know which sound to make?

 

answered by

Related questions

0 votes
1 answer
asked by smith Geek (15.3k points) 17 42 62 | 7 views
0 votes
1 answer
asked by keem Expert (13.2k points) 27 43 59 | 39 views
0 votes
1 answer
asked by john Guru (27.1k points) 33 50 100 | 32 views
0 votes
2 answers
asked by augustaina Sr Member (4.8k points) 10 25 31 | 83 views
0 votes
1 answer
asked by smith Geek (15.3k points) 17 42 62 | 118 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users