Back to Top

Sunday, 11 August 2013

JAVA: Keyword: abstract


abstract:

     This is used to define a class or a method as an abstract operation. When a method is declared as abstract, that means, the method where it is declared (within the class) have no body of the method rather the method can be overridden, through inheritance and define it's body.
     When a class  contains any abstract method, it must be declared as abstract.
An abstract class cannot be final.
An abstract method cannot be finalstatic nor native.




Code:
package com.interview.question.java;

abstract class SampleAbstractClass {
  /**
    *
    * Sample abstract class.
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    */
     
      // Sample abstract method.
      public abstract void sampleabstractmethod() ;
     
      public void sampleNormalMethod(){
            System.out.println("SampleAbstractClass.sampleNormalMethod()");
      }
     

}


public class TestAbstractClass{
     
      /**
    *
    * Sample class to call SampleAbstractClass.
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    */
      public static void main(String[] args) {
           
            SampleAbstractClass objSampleAbstractClass = newSampleAbstractClass() {
                 
                  @Override
                  public void sampleabstractmethod() {
                       
                        System.out
                                    .println("TestAbstractClass.main(...).new SampleAbstractClass() {...}.sampleabstractmethod()");
                       
                  }
            };
           
            objSampleAbstractClass.sampleabstractmethod();
      }
     
}

Output:

TestAbstractClass.main(...).new SampleAbstractClass() {...}.sampleabstractmethod()

6 Responses to “ JAVA: Keyword: abstract ”

Anonymous said...
1 September 2013 at 15:20

When a class is declared as an abstract, the class should have at least one abstract method.
I think this statement is WRONG..


Unknown said...
6 September 2014 at 21:27

I like it thanks for sharing such a brilliant information, great blog

Picgrant Singapore 2014


IICT said...
27 March 2020 at 15:28

Great article with lots of useful information on Java Training in Chennai wonderful explanation on Java Courses.
Java Training Center in Chennai | Best Java Training in Chennai | Java Training Institute in Chennai


Arpan Jha said...
8 August 2020 at 21:52

Nice Post Dude


Kaparthicynixit said...
13 September 2021 at 19:25

Best article, very useful and explanation. Your post is extremely incredible. Thank you very much.
Visit us: Java Online Training Hyderabad
Visit us: Core Java Online Course


pdusu ba 2nd year result 2022 said...
5 August 2022 at 13:00

This may be an issue with my internet browser because I’ve had this happen before.


Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar