Abstract Classes

Hi,

If i define a class as abstract (public abstract class myClass)

Does this implicitly make all the methods in the class abstract or do i need to define them as abstract.

Also in java if a method is abstract, can a derived class use the method without modifying / defining the method

Many Thanks

Alex

[339 byte] By [alexcurtisa] at [2007-11-26 18:11:33]
# 1

> Hi,

>

> If i define a class as abstract (public abstract

> class myClass)

>

> Does this implicitly make all the methods in the

> class abstract or do i need to define them as

> abstract.

You can have abstract methods or not in one abstract class, but if you have a method abstract in a "normal" class you must declare that class as abstract

>

> Also in java if a method is abstract, can a derived

> class use the method without modifying / defining the

> method

yes

regards,

Manuel Leiria

manuel.leiriaa at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for your helpSo if I say my class is abstract, do i still need to label my methods as abstract (do you even label them as abstract) or is it implicitly labled if the class is labled as abstractCheersAlex
alexcurtisa at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks for your help

>

> So if I say my class is abstract, do i still need to

> label my methods as abstract (do you even label them

> as abstract) or is it implicitly labled if the class

> is labled as abstract

>

>

> Cheers

> Alex

you must label your abstract methods as abstract (if you don't, you'll get a compile error). You know an abstract method doesn't have a body, don't you?

manuel.leiriaa at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 4

> Thanks for your help

>

> So if I say my class is abstract, do i still need to

> label my methods as abstract (do you even label them

> as abstract) or is it implicitly labled if the class

> is labled as abstract

>

>

> Cheers

> Alex

in an abstract class, you must mark abstract methods as such, and you must not mark concrete methods as abstract. the above answer is slightly misleading as it implies one can have abstract methods in a concrete (normal?) class. you can't. having abstract methods on a class that isn't declared as abstract is a syntax error and won't compile

interfaces have only abstract methods, but you don't need to mark them as such. all methods on an interface are implicitly both public and abstract

georgemca at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks fOR yOUR HELP GUYS

I think ou both cleared my problem up.

Thanks Again

-Alex

Edit: Just checking, but you cant directly create an instance of an abstract class can you? Even if there are some methods inside that are not abstract. Also is every method in java implicitly defined as virtual if not otherwise said

Message was edited by:

alexcurtis

alexcurtisa at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 6

> Hi,

>

> If i define a class as abstract (public abstract

> class myClass)

>

> Does this implicitly make all the methods in the

> class abstract or do i need to define them as

> abstract.

>

No not implicitly, you have to declare one or more methods abstract manually/expicitly. You can have an abstract class w/o abstract methods in it, but it still can't be directly implemented.

> Also in java if a method is abstract, can a derived

> class use the method without modifying / defining the

> method

>

>

Use, no. Leave alone and not implement, yes ... but then both that method and that class would also need to be declared abstract.

> Many Thanks

> Alex

abillconsla at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 7

> Thanks fOR yOUR HELP GUYS

>

> I think ou both cleared my problem up.

>

> Thanks Again

> -Alex

>

> Edit: Just checking, but you cant directly create an

> instance of an abstract class can you? Even if there

> are some methods inside that are not abstract. Also

> is every method in java implicitly defined as virtual

> if not otherwise said

>

> Message was edited by:

> alexcurtis

nope, no instances of abstract classes. ever. all methods in java are virtual. there's no way to mark them otherwise

georgemca at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 8

With some "what if" questions you can simple try it and see. Here is a demo:

abstract class A {

public abstract void f();

public void g() {

System.out.println("A.g");

}

}

abstract class B extends A {

public void h() {

System.out.println("B.h");

}

}

class C extends B {

public void f() {

System.out.println("C.f");

}

}

One thing you seem to be confused about is when a method in a class is abstract. That's easy: if a method has a body (like g, above) it is not abstract. If a method declared in a class has no body (= is abstract) then you must declare it with the keyword "abstract" (like f in class A). It's a simple as that.

If a class has an abstract method, either declared in it (like f in class A) or inherited by it (like f in class B) then that class is abstract and must be declared with the "abstract" keyword. An abstract class may have non-abstract methods, too, like g in class A -- this is common.

One more thing: a class can be declared as abstract even if it has no abstract methods:

abstract class D {

}

Two examples of this in the Java API are java.awt.Component and javax.swing.JComponent.

DrLaszloJamfa at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 9

You can't instance a abstract class without abstract methods... but you can simulate an empty subclass to do this, but it has not sense:

abstract class A{

public int x;

public A(){

x=0;

}

public int increment(){

return x++;

}

public static void main(String[] args) throws Exception {

A x=new A(){}; //Empty subclass

}

}

govisagod512a at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 10

> You can't instance a abstract class without abstract

> methods... but you can simulate an empty subclass to

> do this, but it has not sense:

>

> abstract class A{

>public int x;

>

>public A(){

>x=0;

> }

>

> public int increment(){

> return x++;

>

> public static void main(String[] args) throws

> Exception {

>A x=new A(){}; //Empty subclass

> }

>

that's not a simulation. it's an anonymous inner class

georgemca at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 11

> > You can't instance a abstract class without

> abstract

> > methods... but you can simulate an empty subclass

> to

> > do this, but it has not sense:

> >

> > abstract class A{

> >public int x;

> >

> >public A(){

> >x=0;

> > }

> >

> > public int increment(){

> > return x++;

> >

> > public static void main(String[] args) throws

> > Exception {

> >A x=new A(){}; //Empty subclass

> > }

> >

>

> that's not a simulation. it's an anonymous inner class

Yes, with an anonymous inner class you can simulate that you are instancing a object of class A. The problem is that if A is abstract it must have a reason.

govisagod512a at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 12

> > > You can't instance a abstract class without

> > abstract

> > > methods... but you can simulate an empty

> subclass

> > to

> > > do this, but it has not sense:

> > >

> > > abstract class A{

> > >public int x;

> > >

> > >public A(){

> > >x=0;

> > > }

> > >

> > > public int increment(){

> > > return x++;

> > >

> > > public static void main(String[] args) throws

> > > Exception {

> > >A x=new A(){}; //Empty subclass

> > > }

> > >

> >

> > that's not a simulation. it's an anonymous inner

> class

>

> Yes, with an anonymous inner class you can simulate

> that you are instancing a object of class A. The

> problem is that if A is abstract it must have a

> reason.

why is it a "simulation" of anything? it's a straightforward subclass of A

georgemca at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 13

> > that's not a simulation. it's an anonymous inner

> class

>

> Yes, with an anonymous inner class you can simulate

> that you are instancing a object of class A.

Nope. You're instantiating a concrete subclass of A. It's no different than a named subclass, other than the fact that you can't refer to it by name. You're not "simulating" anything.

jverda at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...
# 14

> If i define a class as abstract (public abstract

> class myClass)

>

> Does this implicitly make all the methods in the

> class abstract or do i need to define them as

> abstract.

No that's what an interface does. The purpose of an abstract class is to be able to have both abstract and concrete methods. In principle the compiler could deduct that a method is abstract but it doesn't. In abstract classes you have to explicitly declare abstract methods.

> Also in java if a method is abstract, can a derived

> class use the method without modifying / defining the

> method

Yes, as soon as an abstract method has been defined it can be used. The reason is that there's no way it can actually be called without having first been implemented. This is ensured by the fact that objects can only be instantiated from concrete (fully implemented) classes.

jverda at 2007-7-9 5:44:09 > top of Java-index,Java Essentials,New To Java...