-5

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.

Ofir Farchy
  • 7,657
  • 7
  • 38
  • 58
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • 1
    This should help you : http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – Ken Chan Dec 02 '11 at 07:51
  • 1
    Asking google is one thing, i simply refer to http://meta.stackexchange.com/questions/5280/embrace-the-non-googlers and http://meta.stackexchange.com/questions/15650/ban-lmgtfy-let-me-google-that-for-you-links, but @user1076930 you might have searched for an existing answer on stackoverflow which serves well for your question: http://stackoverflow.com/questions/1320745/abstract-class-in-java – uhm Dec 02 '11 at 08:09

4 Answers4

2
abstract class AbstractTestClass { 
    protected String myString; 

    public String getMyString() { 
        return myString; 
    } 

    public abstract string anyAbstractFunction();
}

Refer the link for more details

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
0

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 {
}
0

Generally:

public abstract class SomeClass
{
    // A pure virtual function
    public abstract void PureVirtual();

    // A regular \ virtual function
    public String SomeFunction(String arg)
    {
        return arg;
    }
}
Ofir Farchy
  • 7,657
  • 7
  • 38
  • 58
0
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

Adreamus
  • 670
  • 7
  • 15
  • It is common to give abstract class names an `Abstract` prefix. So your class should be named `AbstractMyClass` instead of `MyAbstractClass`. – Fabian Barney Dec 02 '11 at 08:00