File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract" Watch "abstract" New topic
Author

abstract

nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
Hi I tried the code below bt getting compile error which says:

-------------------------------------------------
SubClass5.java:29: incompatible types
found : SubClass5
required: A.SuperClass
SuperClass sb = new SubClass5();
--------------------------------------------------


interface A{

void methodItfA();
abstract class SuperClass {
abstract public void doIt();
void checkImpl(){
System.out.println("superclas5 checkImpl()");
}

}}
public class SubClass5 extends SuperClass implements A{

public void doIt()
{

System.out.println("subclas5 doIt()");

}

public void methodItfA()
{

System.out.println("subclas5 methodItfA()");

}

public static void main(String... args)
{
SuperClass sb = new SubClass5();

sb.doIt();
sb.checkImpl();

A ObjA = new SubClass5();
ObjA.methodItfA();
}
}


Further when I use
SuperClass.A.methodItfA();

I get more errors such as
SubClass5.java:29: incompatible types
found : SubClass5
required: A.SuperClass
SuperClass sb = new SubClass5();
^
SubClass5.java:35: cannot find symbol
symbol : variable A
location: class A.SuperClass
SuperClass.A.methodItfA();
^

How to correct n call methodItfA() in my code.....
Please explain what is actually happening


SOURCE: I didn't get this as mock I was trying to dig the concept
[ December 16, 2008: Message edited by: plenitude gupta ]
[ December 16, 2008: Message edited by: plenitude gupta ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Hi Mr. Gupta! Welcome to javaranch

I think your name is not according to the javaranch naming policy.

Also we have a rule here that you have to Quote Your Sources...

I tried your code and it gives error at the declaration of SubClass5. Change

public class Subclass5 extends SuperClass implements A{

to

public class Subclass5 extends A.SuperClass implements A{


(Edit: added explanation as I think it feels bad if you are reminded rules and not given your answer)
[ December 16, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
Thank you Ankit
I will take care of the details by you
but when I try
public class SubClass5 extends SuperClass

it works fine even if Superclass is inside interface

Is it the convention that when we try to extend a class inside interface and also implement the same interface we need to give the declaration as
you said
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

If I don't use A.SuperClass syntax (whether I implement A or not), I get an error as it should. SuperClass is inside interface A. So you will have to use the <outer>.<inner> syntax. Did you try the code like this??

public class SubClass5 extends SuperClass{

It doesn't compile for me (as it should)...
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
yes the code does compile.
To summarize if I run code below I get OUTPUT as
--------------------------------
subclas5 doIt()
superclas5 checkImpl()
--------------------------------
interface A{

void methodItfA();
abstract class SuperClass {
abstract public void doIt();
void checkImpl(){
System.out.println("superclas5 checkImpl()");
}

}}
public class SubClass5 extends SuperClass{

public void doIt()
{
System.out.println("subclas5 doIt()");
}

public void methodItfA()
{System.out.println("subclas5 methodItfA()"); }

public static void main(String... args)
{
SuperClass sb = new SubClass5();
sb.doIt();
sb.checkImpl();
}}

Please explain this behaviour if possible
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

This code does not compile. I think you have a different class with the name SuperClass in the same directory. Just copy paste the source file into an empty directory and then compile it from there and then tell the result...
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
Thank you you were very correct!!!
it did not compile,I forgot that before adding interface I compiled the code and hence Superclass.class was accessible...

You really saved me from misconception which I would have otherwise applied in exam....

Can you suggest somway to escape from such errors during prep for SCJP
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Well I have a lot of experience with these kind of errors. To escape from these kind of troubles, I think you must try to keep your class names as unique as possible. Also after creating about 15-20 programs, change the directory where you create your test programs...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: abstract