• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubts regarding abstract classes  RSS feed

 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've following doubts regarding abstract classes.

1)When I extend an abstract class and use a print method (System.out.print) in its sub class, it shows error -
Also when I try to take input using Scanner in a sub class of an abstract class same error is reported.
Why is this happening. Does this mean we cannot take input as well as cannot print anything in the sub classes of an abstract class

2) I Know that we cannot create instance of an abstract class, but can we create instance of any another class in the body of an abstract class?I thought this because since abstract class is extended mostly and we may require an obiect of class to be used throughout the sub classes of an abstract classes.

Please explain
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Show the code that gives you this error.
2. You can create instances, initialize fields etc inside an abstract class. An abstract class can have non-abstract methods. It can have constructors.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class differs from normal classes only on some grounds like:

1-You can't use new keyword with abstract classname
2-It can have one or more abstract methods
3-You use it to communicate your design idea to readers of code,will tell them that method inside it do some common things in the related subclasses rather than implementing a single concept

Rest is same as Normal(Concrete) class
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:
2-It can have one or more abstract methods


Abstract class can have no abstract method as well.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:An abstract class... can have constructors.


In fact it must have a constructor, even if it's only the default constructor.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar goel if you will notice
I had written what extra an abstract class has

one more important thing it should have a constructor
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Tushar goel if you will notice
I had written what extra an abstract class has

one more important thing it should have a constructor


I know and i have read it. I commented that abstract class can have 0 abstract method as well.
 
Ray Anderson
Ranch Hand
Posts: 98
Java Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's show identifier expected as you have missed some separators ( braces ) or ( semicolons )

If it still showing error like pawel said please post your code.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Here is the code I'm getting error with.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at line numbers 17,18, 26-29. Are they part of any constructor or method?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:1-You can't use new keyword with abstract classname

Nope.JComponent is an abstract class.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a moot point; what you are actually using in that anonymous class is a concrete subclass of JComponent. But the name is that of an abstract class.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I haven't got answer why I am getting error with the code posted. Can anybody explain ?
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't you read Maneesh's post? He told you what the problem is. Statements must be in a method constructor or initialiser.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Agreed with Maneesh and Campbell.

There is no elaborate detailed answer here (related to abstract class). It is just a syntax error. It doesn't compile because it is simply not valid Java syntax.

Henry
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK got it. Thanks.
But want to know what do you mean by 'statements' exactly ? Does every line which ends with semi-colon is known as a statement ? If that is the case, then we are able to declare or even initialize instance variables right after class declaration outside any method.
For example

Then doesn't this contradict with the fact that "Statements must be in a method constructor or initialiser".

Please elaborate.

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are right. There is no contradiction. Those lines you showed are field declarations. The definition of statements is in the Java® Language Specification, though that can be very difficult to read. Declarations are permitted outside methods etc.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!