The Artima Developer Community
Sponsored Link

Java Buzz Forum
Top 15 Java abstract class and abstract method interview questions and programs

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Top 15 Java abstract class and abstract method interview questions and programs Posted: Feb 16, 2016 8:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Top 15 Java abstract class and abstract method interview questions and programs
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.What is abstract class in java?
  • Hiding the implementation  and showing the function definition to the user.
  • Abstract class contains abstract methods and concrete methods(normal methods)
2.How can we define an abstract class? 
  • Using abstract keyword we can define abstract class.
  • Check below code for abstract class example program. 

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   //
  5. }




3. How to declare an abstract method?

  • By Using abstract keyword in the method signature we can declare abstract method.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.   abstract void show(); 

  5. }

 4. Can we define abstract class without abstract method?

  •  Yes we can define abstract class without abstract methods.
  • It is not mandatory to create at-least one abstract method in abstract class.

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  //
  4.  

  5. }

 Read more at Can you define an abstract class without any abstract methods? if yes what is the use of it?


 5.Can we create object object for abstract class?
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly.
  • Through sub class abstract class members will get memory. Whenever we create sub class object of abstract class  abstract class object will be created. i.e abstract class members will get memory at that time.


  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractClass obj= new AbstractClass (); 
  13. // ERROR: Cannot instantiate the type AbstractClass
  14.  
  15. }
  16. }

 6. Is is possible to declare abstract method as static?
  • No. its not possible to declare abstract method with static keyword.
  • If we declare abstract method with static compiler will throw an error.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 7.Can we declare abstract method as final?
  • No. its not possible to declare abstract method with final keyword.
  • If we declare abstract method with final compiler will throw an error.
  • Because abstract methods should be override by its sub classes.


  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

8. Is it possible to declare abstract method as private?
  • No. its not possible to declare abstract method with private .
  • If we declare abstract method with private compiler will throw an error.
  • Because abstract methods should be override by its sub classes.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     private abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
9. Is it possible to declare abstract method as public ?
  • Yes we can declare abstract methods as public.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     public abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
 
10. Is it possible to declare abstract method with default?
  • Yes we can declare abstract methods with default.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.    abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

11. Is it possible to declare abstract method with protected modifier?
  • Yes we can declare abstract methods as protected.
  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     protected abstract void add(); 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 

12.What are the valid and invalid keywords or modifier with abstract class?
  • public,  protected and default are valid.
  • static,  final and private are invalid.

13.Can abstract method declaration include throws clause?
  • Yes. We can define an abstract class with throws clause.

  1. package Abstraction;
  2. public abstract class AbstractClassExample {
  3.  
  4.     abstract void add() throws Exception; 
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
 14.What happens if sub class not overriding abstract methods?
  • If sub class which is extending abstract class not overriding abstract method compiler will throw an error.
  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public class Sample extends AbstractClass {
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }
15. Can we escape of overriding abstract class in sub class which is extending abstract class?
  • Yes we can escape from overriding abstract method from super abstract class by making our class again as abstract.
  • The class which is extending our class will get responsibility of overriding all abstract methods in our class and in super class.

  1. package Abstraction;
  2. public abstract class AbstractClass {
  3.  
  4.     abstract void add() throws Exception; 
  5.  

  6. }

  1. package Abstraction;
  2. public abstract class Sample extends AbstractClass {
  3.  
  4.    
  5. }

  1. package Abstraction;
  2. public class Example extends Sample{
  3.  
  4.     //Compiler Error: The type Sub must implement the inherited abstract method Super.add()
  5. }

Read: Top 15 Java abstract class and abstract method interview questions and programs

Topic: How to Evolve from RDBMS to NoSQL + SQL Previous Topic   Next Topic Topic: Kids : HouseHold Items v1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use