Hibernate configuration file

1.hibernate.dialect:
  • This property hibernate.dialect makes Hibernate generate the appropriate SQL for the given database.
2.hibernate.connection.driver_class:
  • This property hibernate.connection.driver_class specifies jdbc driver class name 
3.hibernate.connection.username:
  • This property hibernate.connection.username specifies database user name.
4.hibernate.connection.password:
  • This property hibernate.connection.password specifies password.



hibernate.cfg.xml file structure


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.  
  7. <session-factory>
  8.  
  9. <!-- Related to the connection START -->
  10. <property name="connection.driver_class">Driver Class Name </property>
  11. <property name="connection.url">URL </property>
  12. <property name="connection.user">user name</property>
  13. <property name="connection.password">password</property>
  14. <!-- Related to the connection END -->

  15. <!-- Related to hibernate properties START -->
  16. <property name="show_sql">true/false</property>
  17. <property name="dialet">Database dialet class</property>
  18. <property name="hbm2ddl.auto">create/update or what ever</property>
  19. <property name="hibernate.jdbc.batch_size">hibernate container that every N rows to be
  20. inserted as batch.</property> 
  21. <!-- Related to hibernate properties END-->
  22.  
  23. <!-- Related to mapping START-->
  24. <mapping resource="hbm file 1 name .xml" />
  25. <mapping resource="hbm file 2 name .xml" />
  26. <!-- Related to the mapping END -->
  27.  
  28. </session-factory>
  29.  
  30. </hibernate-configuration>




Example hibernate.cfg.xml. file  for mysql


hibernate configuration file java

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name="connection.url"> jdbc:mysql://localhost/test</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.MySQLDialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>


Example hibernate.cfg.xml. file for oracle Db

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration SYSTEM 
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration><session-factory>
  6.  
  7. <!-- Related to the connection for Test DataBase START -->
  8. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  9. <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:xe</property>
  10. <property name="connection.user">user </property>
  11. <property name="connection.password">password</property>
  12. <!-- Related to the connection END -->
  13.  
  14. <!-- Related to hibernate properties START -->
  15. <property name="show_sql">true/false</property>
  16. <property name="dialet">org.hibernate.dialect.Oracle9Dialect</property>
  17. <property name="hbm2ddl.auto">create</property>
  18. <property name="hibernate.jdbc.batch_size">50</property>
  19. <!-- Related to hibernate properties END-->
  20.  
  21. <!-- Related to mapping START-->
  22. <mapping resource="hbm_file1.xml" />
  23. <mapping resource="hbm_file2.xml" />
  24. <!-- Related to the mapping END -->
  25.  
  26. </session-factory>
  27. </hibernate-configuration>



Constructor in interface ?

Can we write constructor inside interface in java?

  • No. Interfaces does not allow constructors.
  • Why interface does not have constructor? The variables inside interfaces are static final variables means constants and we can not create object fro interface so there is no need of constructor in interface that is the reason interface doesn't allow us to create constructor.
  • what happens when a constructor is defined for an interface



What will happens if we try to create constructor inside interfaces in java

  • If we try to create constructor in interface compile time error will come.
  • Error description: Interfaces cannot have constructors.


  1. public interface sample{
  2.  
  3.  int a=10;

  4. sample(){//Interfaces cannot have constructors.
  5.  
  6. }




constructor in interface java





Interfaces in Java 8:

  • Before java 8 interfaces allows only public abstract methods.
  • If we declare any method in interface with default it will be treated as public abstract method.
  • Interface methods doesn't have body. The class which implements interfaces are responsible for implementing unimplemented methods of interface.
  • But in Java 8 static and default methods added.

Default methods:

  • Defaults methods are also  known as defender methods or virtual extension methods
  • Default methods will help us to avoid utility classes.
  • We can define utility methods inside the interface and use it in all classes which is implementing.
  • One of the major reason to introduce this default methods in java 8 is to support lambda expressions in collections API and to enhance.


  1. package com.instanceofjava;
  2. interface Java8InterfaceDemo{
  3.  
  4. abstract void print();
  5.   
  6. default void display(){
  7.  
  8. System.out.println("default method of interface");
  9.  
  10. }
  11.  
  12. }

  1. package com.instanceofjava;
  2. class Sample implements Java8InterfaceDemo{
  3.  
  4. void print(){
  5. System.out.print("overridden method ")
  6.  }
  7. public static void main(String[] args){
  8.   
  9. Sample obj= new Sample();
  10.  
  11. obj.print(); // calling implemented method
  12. obj.display(); // calling inherited method
  13.  
  14. }
  15.  
  16. }

Output:

  1. overridden method
  2. default method of interface
  3. default method of interface

Static methods in Java 8:

  • These static method will act as helper methods.
  • These methods are the parts of interface not belongs to implementation class objects.

  1. package com.interfacesinJava8;
  2. interface StaticInterface{
  3.  
  4. Static void print(String str){
  5.  
  6. System.out.println("Static method of interface:"+str);
  7.  
  8. }
  9. }

  1. package com.instanceofjava;
  2. class Demo implements StaticInterface{
  3.  
  4. public static void main(String[] args){
  5.   
  6.  StaticInterface.print("Java 8")
  7.  
  8. }
  9.  
  10. }

Output:

  1. Static method of interface: Java 8

Can we create private constructor in java

1.Can a constructor in Java be private?
  • Yes we can declare private constructor in java.
  • If we declare constructor as private we can not able to create object of the class.
  • In singleton design pattern we use this private constructor. 

private constructor in java example program


 2.In what scenarios we will use private constructor in java.

  •  Singleton Design pattern
  • It wont allow class to be sub classed.
  • It wont allow to create object outside the class.
  • If All Constant methods is there in our class we can use private constructor.
  • If all methods are static then we can use private constructor.



 3.What will happen if we extends a class which is having private constructor.

  •  If we try to extend a class which is having private constructor compile time error will come.
  • Implicit super constructor is not visible for default constructor. Must define an explicit constructor

private constructor in java


Singleton Design pattern:


  1. package com.privateConstructorSingleTon;
  2.  
  3. public class SingletonClass {

  4. private static SingletonClass object;
  5.  
  6. private SingletonClass ()
  7. {
  8.         System.out.println("Singleton(): Private constructor invoked");
  9. }
  10.  
  11. public static SingletonClass getInstance()
  12. {
  13.  
  14. if (object == null)
  15. {
  16.  
  17. System.out.println("getInstance(): First time getInstance was called and object created !");
  18. object = new SingletonClass ();
  19.  
  20.  }
  21.  
  22. return object;
  23.  
  24. }
  25.  
  26. }
     



  1. package instanceofjava;
  2.  
  3. public class SingletonObjectDemo {

  4. public static void main(String args[]) {
  5.  
  6.      SingletonClass s1= SingletonClass .getInstance();
  7.      SingletonClass s2= SingletonClass .getInstance();
  8.      System.out.println(s1.hashCode());
  9.      System.out.println(s2.hashCode());
  10.  
  11. }
  12. }

Output:

  1. getInstance(): First time getInstance was called and object created !
  2. Singleton(): Private constructor invoked
  3. 655022016
  4. 655022016

Java Static Constructor : is it possible to create static constructor?

1.Is it possible to create static constructor in java?
  • No. We can not create constructor with static.
  • If we try to create a static constructor compile time error will come: Illegal modifier for the constructor
Static construcotr in java interview question



2.What is the real use of constructor in java? 

  • Constructors will be used to assign instance variables with default values.
  • Whenever object is created constructor will be called so the default values for the instance variables will be assigned in this constructor.
  • Top 15 Java Interview Questions on Constructors

  1. public class ConstructorDemo {
  2.  
  3.  int a,b;

  4. ConstructorDemo (int x, int y){
  5.  
  6. a=x;
  7. b=y;
  8.  
  9. }
  10. public static void main(String[] args) {
  11.  
  12.         ConstructorDemo ob= new ConstructorDemo ();
  13.  
  14.     }
  15. }


3. What is the use of static in java?
  • Static keyword is mainly used for memory management.
  • Static variables get memory when class loading itself.
  • Static variables can be used to point common property all objects.
4.Is there any alternative solution for static constructor in java

  • Static means class level.
  • Constructor will be use to assign initial values for instance variables
  • static and constructor are different and opposite from each other.
  • To assign initial values for instance variable we use constructor.
  • To assign static variables we use Static Blocks
  • We can use static blocks to initialize static variables in java.


5.what is static block in java?

  • Class loading time itself these variables gets memory
  • Static methods are the methods with static keyword are class level. without creating the object of the class we can call these static methods.

    1. public static void show(){ 
    2.  
    3. }

  • Static block also known as static initializer
  • Static blocks are the blocks with static keyword.
  • Static blocks wont have any name in its prototype.
  • Static blocks are class level.
  • Static block will be executed only once.
  • No return statements.
  • No arguments.
  • No this or super keywords supported.
  •  
    1. static{ 
    2.  
    3.  }
6. write a java program to assign static variables using static block


  1. package com.staticInitializer;
  2.  
  3. class StaticBlockDemo 
  4. {
  5.  
  6. static{
  7.       System.out.println("First static block executed");
  8. }
  9.  
  10. static{
  11.      System.out.println("Second static block executed");
  12. }
  13.  
  14. static{
  15.      System.out.println("Third static block executed");
  16. }
  17.  
  18. }
     
Output:
  1. First static block executed
  2. Second static block executed
  3. Third static block executed

  1.Static Variables 

  2.Static Methods 

  3.Static Blocks

  4.Main method

How to check internet connection using java

  • Hi friends now we are going to check our pc is having internet connection or not using java code 
  • yes we can check internet connection using java code java.net package providing some useful classes.
  • Using java.net.URL and java.net.URLConnection classes we need to check we can check we have a connection or not.
  • Lets see the program to detect internet connection using java
#1:


  1. package InternetConnections;
  2. import java.net.URL;
  3. import java.net.URLConnection; 

  4. public class CheckInternetConnection{
  5.  
  6. public static void main(String[] args){
  7.   
  8. try 
  9. {
  10.         URL url = new URL("http://www.instanceofjava.com");
  11.  
  12.         URLConnection connection = url.openConnection();
  13.         connection.connect();   
  14.  
  15.         System.out.println("Internet Connected");   
  16.             
  17.  }catch (Exception e){
  18.      
  19. System.out.println("Sorry, No Internet Connection");     
  20.                                                             

  21. }
  22. }

Detect internet connection using java program



#2:
  • By using  getRuntime() method of java.lang.Runtime  we can also test internet connection in java
  • The output will be 0 if internet connection available 1 if not.


how to check internet connection in java



#3:


  1. package InternetConnections;
  2.  
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.util.Enumeration;

  6. public class CheckInternetConnection{
  7.  
  8. public static void main(String[] args){
  9.  
  10. Enumeration<NetworkInterface> interfaces = null;
  11.  
  12. try {
  13.            
  14.      interfaces = NetworkInterface.getNetworkInterfaces();
  15.  
  16.   } catch (SocketException e) {
  17.             e.printStackTrace();
  18.  }  
  19.  
  20.  while (interfaces.hasMoreElements()) {  
  21.  
  22.       NetworkInterface nic = interfaces.nextElement();   
  23.       System.out.print("Interface Name : [" + nic.getDisplayName() + "]");  
  24.      
  25. try {
  26.       
  27.  System.out.println(", is connected : [" + nic.isUp() + "]"); 

  28. } catch (SocketException e) {
  29.                 e.printStackTrace();
  30. }  
  31.  
  32. }   

  33. }
  34. }



Output:

  1. Interface Name : [Software Loopback Interface 1], is connected : [true]
  2. Interface Name : [WAN Miniport (SSTP)], is connected : [false]
  3. Interface Name : [WAN Miniport (L2TP)], is connected : [false]
  4. Interface Name : [WAN Miniport (PPTP)], is connected : [false]
  5. Interface Name : [WAN Miniport (PPPOE)], is connected : [false]
  6. Interface Name : [WAN Miniport (IPv6)], is connected : [false]
  7. Interface Name : [WAN Miniport (Network Monitor)], is connected : [false]
  8. Interface Name : [WAN Miniport (IP)], is connected : [false]
  9. Interface Name : [RAS Async Adapter], is connected : [false]
  10. Interface Name : [WAN Miniport (IKEv2)], is connected : [false]
  11. Interface Name : [Realtek PCIe GBE Family Controller], is connected : [true]
  12. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter], is connected : [false]
  13. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  14. Interface Name : [Microsoft Virtual WiFi Miniport Adapter], is connected : [true]
  15. Interface Name : [null], is connected : [false]
  16. Interface Name : [Microsoft Virtual WiFi Miniport Adapter #2], is connected : [false]
  17. Interface Name : [null], is connected : [false]
  18. Interface Name : [Teredo Tunneling Pseudo-Interface], is connected : [false]
  19. Interface Name : [Apple Mobile Device Ethernet], is connected : [false]
  20. Interface Name : [Microsoft 6to4 Adapter], is connected : [true]
  21. Interface Name : [Microsoft ISATAP Adapter #4], is connected : [false]
  22. Interface Name : [Microsoft ISATAP Adapter], is connected : [false]
  23. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify WLAN
  24. LightWeight Filter-0000], is connected : [false]
  25. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Connectify NDIS
  26. LightWeight Filter-0000], is connected : [false]
  27. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet
  28. Scheduler0000], is connected : [false]
  29. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter
  30. Driver-0000], is connected : [false]
  31. Interface Name : [Realtek PCIe GBE Family Controller-Connectify NDIS LightWeight Filter
  32. 0000], is connected : [false]
  33. Interface Name : [Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000], is
  34. connected : [false]
  35. Interface Name : [Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000], is
  36. connected : [false]
  37. Interface Name : [Realtek PCIe GBE Family Controller-Deterministic Network Enhancer
  38. 0000], is connected : [false]
  39. Interface Name : [WAN Miniport (IPv6)-Connectify NDIS LightWeight Filter-0000], is
  40. connected : [false]
  41. Interface Name : [WAN Miniport (IPv6)-Deterministic Network Enhancer-0000], is connected
  42. : [false]
  43. Interface Name : [WAN Miniport (IPv6)-QoS Packet Scheduler-0000], is connected : [false]
  44. Interface Name : [WAN Miniport (IP)-Connectify NDIS LightWeight Filter-0000], is
  45. connected : [false]
  46. Interface Name : [WAN Miniport (IP)-Deterministic Network Enhancer-0000], is connected :
  47. [false]
  48. Interface Name : [WAN Miniport (IP)-QoS Packet Scheduler-0000], is connected : [false]
  49. Interface Name : [WAN Miniport (Network Monitor)-Connectify NDIS LightWeight Filter
  50. 0000], is connected : [false]
  51. Interface Name : [WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000], is
  52. connected : [false]
  53. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter
  54. Driver-0000], is connected : [false]
  55. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-Deterministic Network
  56. Enhancer-0000], is connected : [false]
  57. Interface Name : [Atheros AR9485WB-EG Wireless Network Adapter-WFP LightWeight
  58. Filter-0000], is connected : [false]
  59. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000], is
  60. connected : [false]
  61. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Deterministic Network Enhancer
  62. 0000], is connected : [false]
  63. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify NDIS LightWeight
  64. Filter-0000], is connected : [false]
  65. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-QoS Packet Scheduler-0000], is
  66. connected : [false]
  67. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-WFP LightWeight Filter-0000], is
  68. connected : [false]
  69. Interface Name : [Microsoft Virtual WiFi Miniport Adapter-Connectify WLAN LightWeight
  70. Filter-0000], is connected : [false]

Top 10 Interview Programs and questions on method overriding in java

1.What is method overriding in java?
  • Defining multiple methods with same name and same signature in super class and sub class known as method overriding. 
  • Method overriding is type of polymorphism in java which is one of the main object oriented feature.
  • Redefined the super class method in sub class is known as method overriding.
  • method overriding allows sub class to provide specific implementation that is already defined in super class.
  • Sub class functionality replaces the super class method functionality (implementation).
2. Can we override private methods in java?



3. Can we override static methods of super class in sub class?
  • NO.Its not possible to override static methods because static means class level so static methods not involve in inheritance.

 4. Can we change the return type of overridden method in sub class?
  • No. Return type must be same in super class and sub class.


  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }



  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. int add(){    //Compiler Error: The return type is incompatible with Super.add()
  5.  
  6. System.out.println("Sub class add method");
  7. return 0; 

  8. }

  9. }

5.Can we change accessibility modifier in sub class overridden method?
  • Yes we can change accessibility modifier in sub class overridden method but should increase the accessibility if we decrease compiler will throw an error message.


Super class method  Subclass method
protected protected, public
public      public
default     default , public
6.What happens if we try to decrease accessibility from super class to sub class?
  • Compile time error will come.
  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add(){ //Compiler Error: Cannot reduce the visibility of the inherited method
  5. from Super
  6.  
  7. System.out.println("Sub class add method");

  8. }

  9. }


method overriding example program interview question


7.Can we override a super class method without throws clause to with throws clause in the sub class?

  • Yes if super class method throws unchecked exceptions.
  • No need if super class method throws checked exceptions. But it is recommended to add in sub class method in order to maintain exception messages.

 8.What are the rules we need to follow in overriding if super class method throws exception ?


 9.What happens if we not follow these rules if super class method throws some exception.

  •  Compile time error will come.

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws Exception{ //Compiler Error: Exception Exception is not compatible with
  5. throws clause in Super.add()
  6. System.out.println("Sub class add method");

  7. }

  8. }

  1. package MethodOverridingExamplePrograms;
  2. public class Super{
  3.  
  4. public void add(){
  5.  System.out.println("Super class add method");
  6. }

  7. }

  1. package MethodOverridingInterviewPrograms;
  2. public class Sub extends Super{
  3.   
  4. void add() throws NullPointerException{ // this method throws unchecked exception so no
  5. isuues
  6. System.out.println("Sub class add method");

  7. }

  8. }

10.Can we change an exception of a method with throws clause from unchecked to checked while overriding it?
  •  No. As mentioned above already
  • If super class method throws exceptions in sub class if you want to mention throws  then use  same class  or its  sub class exception.
  • So we can not change from unchecked to checked 

 You might Like:

 

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

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


abstract class interview questions


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



abstract class interview programs


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

You Might Like:



Select Menu