Yes, create a new class using Right Click (on the package where you want to create the class) > New > Class, and in the SuperClass field, you can select your abstract class. Be sure that the checkbox Which method stubs would you like to > Inherited abstract methods is checked.
If you have already created your class, you can make it extend you abstract class by hand, and use Right Click (on the source code) > Source > Override/Implement Methods... and choose which methods you want to implement (if they are not already implemented).
An other way to do it, is to extend your abstract class by hand, and use the Quick Fix provided by Eclipse to Add unimplemented methods. You can call the Quick Fix tooltip by highlighting the error and using Ctrl+1, or by clicking on the error icon on the left of the code.
Videos
Yes, create a new class using Right Click (on the package where you want to create the class) > New > Class, and in the SuperClass field, you can select your abstract class. Be sure that the checkbox Which method stubs would you like to > Inherited abstract methods is checked.
If you have already created your class, you can make it extend you abstract class by hand, and use Right Click (on the source code) > Source > Override/Implement Methods... and choose which methods you want to implement (if they are not already implemented).
An other way to do it, is to extend your abstract class by hand, and use the Quick Fix provided by Eclipse to Add unimplemented methods. You can call the Quick Fix tooltip by highlighting the error and using Ctrl+1, or by clicking on the error icon on the left of the code.
Use the New Class as normal but then specify your abstract class as the Superclass. Then check Which methods stubs would you like to create -> inherited abstract methods
Quick methods:
Hold Ctrl, hover over the method name, and select "Open Implementation".
Click on the method name and press CtrlT.
Right-click on the method name → "Quick Type Hierarchy".
For more navigation power, see the post by ADTC.
Great answers here! I found an additional trick to quickly navigate from implementation to implementation.
It is rather strange that Eclipse uses a temporary pop-up to show "Types implementing or defining 'Class.method()'" instead of a permanent view that can be pinned to the sides.
The advantage of this pop-up is, when you click on an implementing type, you are taken directly to the method implementation. The disadvantage of this pop-up is that it closes as soon as you do so, and there is no way to pin it permanently.
The Type Hierarchy shows the same list of implementing types (as they are subclasses of the abstract class). Since it is a view, it can be pinned permanently on the side. However, if you double-click on a type name here, you are taken to the declaration line of the type. You then have to find the implementation of the abstract method yourself.
Except, you don't have to, with the following trick:
- Right-click on the abstract method and click Open Type Hierarchy.1 In the Type Hierarchy view, you will notice the following:
- All the subtypes of the abstract class type are listed in the primary list (P).2
- All the members of the abstract class are listed in the secondary list (S).3
- The abstract method you right-clicked on is selected in the secondary list.4
- Click on the Lock View and Show Members in Hierarchy button (1) above the secondary list.
- The selected method and its implementations will be listed in the primary list under each of the subtypes as well as the parent type.
You can now quickly navigate between implementations by clicking or double-clicking on the methods listed in the primary list.
1 You can also click on the method name and press F4.
2 If they are not listed, click either one of the Show the Type Hierarchy (2) or Show the Subtype Hierarchy (3) buttons above the list.
3 If there's no secondary list, click on the View Menu (4), choose Layout > [any option other than Hierarchy Only].
4 If it is not selected, just click on it to select it.
An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass:
- Define methods which can be used by the inheriting subclass.
- Define abstract methods which the inheriting subclass must implement.
- Provide a common interface which allows the subclass to be interchanged with all other subclasses.
Here's an example:
abstract public class AbstractClass
{
abstract public void abstractMethod();
public void implementedMethod() { System.out.print("implementedMethod()"); }
final public void finalMethod() { System.out.print("finalMethod()"); }
}
Notice that "abstractMethod()" doesn't have any method body. Because of this, you can't do the following:
public class ImplementingClass extends AbstractClass
{
// ERROR!
}
There's no method that implements abstractMethod()! So there's no way for the JVM to know what it's supposed to do when it gets something like new ImplementingClass().abstractMethod().
Here's a correct ImplementingClass.
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
}
Notice that you don't have to define implementedMethod() or finalMethod(). They were already defined by AbstractClass.
Here's another correct ImplementingClass.
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
}
In this case, you have overridden implementedMethod().
However, because of the final keyword, the following is not possible.
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
public void finalMethod() { System.out.print("ERROR!"); }
}
You can't do this because the implementation of finalMethod() in AbstractClass is marked as the final implementation of finalMethod(): no other implementations will be allowed, ever.
Now you can also implement an abstract class twice:
public class ImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("abstractMethod()"); }
public void implementedMethod() { System.out.print("Overridden!"); }
}
// In a separate file.
public class SecondImplementingClass extends AbstractClass
{
public void abstractMethod() { System.out.print("second abstractMethod()"); }
}
Now somewhere you could write another method.
public tryItOut()
{
ImplementingClass a = new ImplementingClass();
AbstractClass b = new ImplementingClass();
a.abstractMethod(); // prints "abstractMethod()"
a.implementedMethod(); // prints "Overridden!" <-- same
a.finalMethod(); // prints "finalMethod()"
b.abstractMethod(); // prints "abstractMethod()"
b.implementedMethod(); // prints "Overridden!" <-- same
b.finalMethod(); // prints "finalMethod()"
SecondImplementingClass c = new SecondImplementingClass();
AbstractClass d = new SecondImplementingClass();
c.abstractMethod(); // prints "second abstractMethod()"
c.implementedMethod(); // prints "implementedMethod()"
c.finalMethod(); // prints "finalMethod()"
d.abstractMethod(); // prints "second abstractMethod()"
d.implementedMethod(); // prints "implementedMethod()"
d.finalMethod(); // prints "finalMethod()"
}
Notice that even though we declared b an AbstractClass type, it displays "Overriden!". This is because the object we instantiated was actually an ImplementingClass, whose implementedMethod() is of course overridden. (You may have seen this referred to as polymorphism.)
If we wish to access a member specific to a particular subclass, we must cast down to that subclass first:
// Say ImplementingClass also contains uniqueMethod()
// To access it, we use a cast to tell the runtime which type the object is
AbstractClass b = new ImplementingClass();
((ImplementingClass)b).uniqueMethod();
Lastly, you cannot do the following:
public class ImplementingClass extends AbstractClass, SomeOtherAbstractClass
{
... // implementation
}
Only one class can be extended at a time. If you need to extend multiple classes, they have to be interfaces. You can do this:
public class ImplementingClass extends AbstractClass implements InterfaceA, InterfaceB
{
... // implementation
}
Here's an example interface:
interface InterfaceA
{
void interfaceMethod();
}
This is basically the same as:
abstract public class InterfaceA
{
abstract public void interfaceMethod();
}
The only difference is that the second way doesn't let the compiler know that it's actually an interface. This can be useful if you want people to only implement your interface and no others. However, as a general beginner rule of thumb, if your abstract class only has abstract methods, you should probably make it an interface.
The following is illegal:
interface InterfaceB
{
void interfaceMethod() { System.out.print("ERROR!"); }
}
You cannot implement methods in an interface. This means that if you implement two different interfaces, the different methods in those interfaces can't collide. Since all the methods in an interface are abstract, you have to implement the method, and since your method is the only implementation in the inheritance tree, the compiler knows that it has to use your method.
A Java class becomes abstract under the following conditions:
1. At least one of the methods is marked as abstract:
public abstract void myMethod()
In that case the compiler forces you to mark the whole class as abstract.
2. The class is marked as abstract:
abstract class MyClass
As already said: If you have an abstract method the compiler forces you to mark the whole class as abstract. But even if you don't have any abstract method you can still mark the class as abstract.
Common use:
A common use of abstract classes is to provide an outline of a class similar like an interface does. But unlike an interface it can already provide functionality, i.e. some parts of the class are implemented and some parts are just outlined with a method declaration. ("abstract")
An abstract class cannot be instantiated, but you can create a concrete class based on an abstract class, which then can be instantiated. To do so you have to inherit from the abstract class and override the abstract methods, i.e. implement them.
Define a constructor in the abstract class which sets the field so that the concrete implementations are per the specification required to call/override the constructor.
E.g.
public abstract class AbstractTable {
protected String name;
public AbstractTable(String name) {
this.name = name;
}
}
When you extend AbstractTable, the class won't compile until you add a constructor which calls super("somename").
public class ConcreteTable extends AbstractTable {
private static final String NAME = "concreteTable";
public ConcreteTable() {
super(NAME);
}
}
This way the implementors are required to set name. This way you can also do (null)checks in the constructor of the abstract class to make it more robust. E.g:
public AbstractTable(String name) {
Objects.requireNonNull(name, "Name may not be null");
this.name = name;
}
I think your confusion is with C# properties vs. fields/variables. In C# you cannot define abstract fields, even in an abstract class. You can, however, define abstract properties as these are effectively methods (e.g. compiled to get_TAG() and set_TAG(...)).
As some have reminded, you should never have public fields/variables in your classes, even in C#. Several answers have hinted at what I would recommend, but have not made it clear. You should translate your idea into Java as a JavaBean property, using getTAG(). Then your sub-classes will have to implement this (I also have written a project with table classes that do this).
So you can have an abstract class defined like this...
public abstract class AbstractTable {
public abstract String getTag();
public abstract void init();
...
}
Then, in any concrete subclasses you would need to define a static final variable (constant) and return that from the getTag(), something like this:
public class SalesTable extends AbstractTable {
private static final String TABLE_NAME = "Sales";
public String getTag() {
return TABLE_NAME;
}
public void init() {
...
String tableName = getTag();
...
}
}
EDIT:
You cannot override inherited fields (in either C# or Java). Nor can you override static members, whether they are fields or methods. So this also is the best solution for that. I changed my init method example above to show how this would be used - again, think of the getXXX method as a property.