Jump to content
  • Advertisement
Sign in to follow this  
hisDudeness

abstract class madness

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Java applets, can instances of extended classes of an abstract base class' method have a different signature than the original method? For example: abstract class Widget { abstract int method(); } class Child_1 extends Widget { int method(double c) { /* blah */ } } class Child_2 extends Widget { double method(char a, char b) { /* blah */ } } What does the 'abstrac' keyword actually do? How is its usage in 'abstract class Widget' differ from its use in 'abstract int method();'? And can I set any instance of an application-defined class to 'null' and have it be valid? Example: Child_1 c = null; // instead of Child_1 c = new Child_1();

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by hisDudeness
n Java applets, can instances of extended classes of an abstract base class' method have a different signature than the original method? For example:

abstract class Widget
{
abstract int method();
}

class Child_1 extends Widget
{
int method(double c) { /* blah */ }
}

class Child_2 extends Widget
{
double method(char a, char b) { /* blah */ }
}


No. They must provide the exact method as specified, or polymorphism doesn't really work.

Quote:
Original post by hisDudeness
What does the 'abstract' keyword actually do? How is its usage in 'abstract class Widget' differ from its use in 'abstract int method();'?

Abstract allows the superclass to ensure methods in the sub class without actually having to define them. Anytime an abstract class contains an abstract method, the class must be defined as abstract. Basically, you can not instantiate an abstract class (just as you cannot instantiate an interface).

Quote:
Original post by hisDudeness
And can I set any instance of an application-defined class to 'null' and have it be valid? Example:

Child_1 c = null; // instead of Child_1 c = new Child_1();

Yes. c is simply a reference, so you can set it to null if you would like.

Share this post


Link to post
Share on other sites
You can put a method with the same name but different signature in a derived class, but it will not override the method from the abstract class - you'll still need to do that, without changing the signature.

When you declare a class abstract (e.g. abstract class Widget) it informs the compiler that the user (by user I mean programmer using the class in his program) may not instantiate the class. In this example, the user could never say Widget w = new Widget(); he would have to use a derived class, as in Widget w = new FancyWidget();

When you declare a method abstract, it tells the compiler that the method is not defined in the base class definition and that it must be overridden by the user. Any class with an abstract method must be declared abstract itself... clearly the user will not be able to instantiate a class that is not fully defined!

Finally, you can set any reference (any variable except basic ones like "int") to null.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!