File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the type of Base?

 
Yingie Pitts
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a question from one of the mock exams. But I have a question related to Base class. It can not be protected and private. If it is public, it has to be declared in a seperate file named Base.java. So what type of the Base is if I save both of the class in one file named Child.java?

class Base{
public void amethod(int i, String s){
System.out.println("Base amethod");
}
Base(){
System.out.println("Base Constructor");
}
}
public class Child extends Base{
int i;
String Parm="Hello";
public static void main(String argv[]){
Child c = new Child();
c.amethod();
}
void amethod(int i, String Parm){
super.amethod(i,Parm);
}
public void amethod(){}
}
1) Compile time error
2) Error caused by illegal syntax super.amethod(i,Parm)
3) Output of "Base Constructor"
4) Error caused by incorrect parameter names in call to super.amethod
answer: 1)
 
Hanna Habashy
Ranch Hand
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
I believe it has defualt access. It will be accessable from classes within the same package.
Hanna
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yingie Pitts:
Below is a question from one of the mock exams. But I have a question related to Base class. It can not be protected and private. If it is public, it has to be declared in a seperate file named Base.java. So what type of the Base is if I save both of the class in one file named Child.java?

class Base{
public void amethod(int i, String s){
System.out.println("Base amethod");
}
Base(){
System.out.println("Base Constructor");
}
}
public class Child extends Base{
int i;
String Parm="Hello";
public static void main(String argv[]){
Child c = new Child();
c.amethod();
}
void amethod(int i, String Parm){
super.amethod(i,Parm);
}
public void amethod(){}
}
1) Compile time error
2) Error caused by illegal syntax super.amethod(i,Parm)
3) Output of "Base Constructor"
4) Error caused by incorrect parameter names in call to super.amethod
answer: 1)

compile time error is due to overriding of amethod() from base class.
It's accessibility is public and in subclass it is default so compile time error.
 
Yingie Pitts
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been considering defualt class as private type class. But it seems default is just default; it does not mean it is of any type. The only thing about default class is that it only can be accessed within the same package. Am I right?
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no default type, we refer it this way when no modifier is specified & it can be accessed within same package.
access relationship from narrow to wide is
private -> default -> protected -> public
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic