How can we define an abstract class in java?
What is the prototype used for it?
Please provide an example, for clarity, I am new to java.
Thanks.
How can we define an abstract class in java?
What is the prototype used for it?
Please provide an example, for clarity, I am new to java.
Thanks.
abstract class AbstractTestClass {
protected String myString;
public String getMyString() {
return myString;
}
public abstract string anyAbstractFunction();
}
Refer the link for more details
See the Following Example which can help you
import java.util.*;
// Provider framework sketch
public abstract class Foo {
// Maps String key to corresponding Class object
private static Map implementations = null;
// Initializes implementations map the first time it's called
private static synchronized void initMapIfNecessary() {
if (implementations == null) {
implementations = new HashMap();
// Load implementation class names and keys from
// Properties file, translate names into Class
// objects using Class.forName and store mappings.
// ...
}
}
public static Foo getInstance(String key) {
initMapIfNecessary();
Class c = (Class) implementations.get(key);
if (c == null)
return new DefaultFoo();
try {
return (Foo) c.newInstance();
} catch (Exception e) {
return new DefaultFoo();
}
}
public static void main(String[] args) {
System.out.println(getInstance("NonexistentFoo"));
}
}
class DefaultFoo extends Foo {
}
Generally:
public abstract class SomeClass
{
// A pure virtual function
public abstract void PureVirtual();
// A regular \ virtual function
public String SomeFunction(String arg)
{
return arg;
}
}
public abstract class AbstractMyClass {
// declare fields
int part1 = 4;
int part2 = 5;
//declare non abstract mehots with some code
@Override
public String toString() {
return part1.toString() + part2.toString();
}
//declare abstract methods without code (like in an interface)
abstract void compute();
}
This is an example how to setup abstract classes. More about can be found here: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html