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()

3 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..


leona fassi said...
6 September 2014 at 21:27

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

Picgrant Singapore 2014


Naviya Nair said...
24 October 2016 at 15:02

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs