Two Laptop Bag*
The moose likes Beginning Java and the fly likes Abstract Class and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Class and methods" Watch "Abstract Class and methods" New topic
Author

Abstract Class and methods

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
in the code below, an Abstract Class (LCC) has a method getLeader which has no implementation. non-abstract class Ilea extends Lcc but does not provide implementation for getLeader. Is this okay because the method in LCC was not declared "abstract public void getLeader(){}"? ie, it is not an abstract method, and although it exists in an abstract class, no subclasses need provide implementation for it?
abstract class Lcc{
String Leader;
public void getLeader(){
}
}
public class Ilea extends Lcc{
public static void main(String argv[]){
Ilea i = new Ilea();
i.lrb();
}
public void lrb(){
String s1= new String("one");
StringBuffer sb = new StringBuffer("one");
if(s1.equals(sb)){
System.out.println("equals");
}
}
}


giddee up
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Actually I was kind of surprised to find out that the Lcc class compiled!
Although you have flagged it as "abstract" it really isn't since it declares no "abstract" method (I thought the compiler would complain about that).
Having an empty body (like your method in Lcc) does not mean the same thing as "abstract". For it to be "truely" abstract, the method must have the "abstract" keyword and no curly braces in its declaration, e.g. public abstract void methodA();
So, since Lcc is not really abstract, there is no contract that any subclass has to fulfill.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Rikard Qvarforth
Ranch Hand

Joined: Jul 10, 2001
Posts: 107
well the abstract modifier can be applied to classes and methods. So the code here is right you must subclass the abstract class to get an "instance" of the superclass, but if you want to force the subclass to define the implementation of an method you make the method abstact. So in your code you are not forcing the subclass to do the implementation. and so the subclass kan skipp its own implemetation of the method. So i dont relly understand what you are after ? it all comes down to what you are after in your code .
:roll:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, the getLeader method *does* have an implementation: an empty one!
Compare this to an abstract method without implementation:

And, yes, it's totally legal to declare a class without any abstract methods to be abstract.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Thankyou very much Ilja for the clarity
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract Class and methods