Abstract class in Java

后端 未结 14 1270
孤独总比滥情好
孤独总比滥情好 2020-11-22 12:32

What is an \"abstract class\" in Java?

相关标签:
14条回答
  • 2020-11-22 13:13

    An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

    In other words, a class that is declared with abstract keyword, is known as abstract class in java. It can have abstract(method without body) and non-abstract methods (method with body).

    Important Note:- Abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time Polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature in the below example

    abstract class Bike{  
      abstract void run();  
    }  
    
    class Honda4 extends Bike{  
        void run(){
            System.out.println("running safely..");
        }  
    
        public static void main(String args[]){  
           Bike obj = new Honda4();  
           obj.run();  
        }  
    } 
    
    0 讨论(0)
  • 2020-11-22 13:16

    It's a class that cannot be instantiated, and forces implementing classes to, possibly, implement abstract methods that it outlines.

    0 讨论(0)
  • 2020-11-22 13:20

    Little addition to all these posts.

    Sometimes you may want to declare a class and yet not know how to define all of the methods that belong to that class. For example, you may want to declare a class called Writer and include in it a member method called write(). However, you don't know how to code write() because it is different for each type of Writer devices. Of course, you plan to handle this by deriving subclass of Writer, such as Printer, Disk, Network and Console.

    0 讨论(0)
  • 2020-11-22 13:24

    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:

    1. Define methods which can be used by the inheriting subclass.
    2. Define abstract methods which the inheriting subclass must implement.
    3. 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.

    0 讨论(0)
  • 2020-11-22 13:24

    Class which can have both concrete and non-concrete methods i.e. with and without body.

    1. Methods without implementation must contain 'abstract' keyword.
    2. Abstract class can't be instantiated.
    0 讨论(0)
  • 2020-11-22 13:25

    Get your answers here:

    Abstract class vs Interface in Java

    Can an abstract class have a final method?

    BTW - those are question you asked recently. Think about a new question to build up reputation...

    Edit:

    Just realized, that the posters of this and the referenced questions have the same or at least similiar name but the user-id is always different. So either, there's a technical problem, that keyur has problems logging in again and finding the answers to his questions or this is a sort of game to entertain the SO community ;)

    0 讨论(0)
提交回复
热议问题