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:



Can we call super class static method from subclass in java

  • If you want to call static method of a class we can call directly from another static method or by using its class name we can call static method of that class.
  • let us see a program on how to call a static method of a class

  1. package com.instanceofjava;
  2. public class Sample{

  3. public static void show(){
  4.  
  5.  System.out.println("show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10.  show();
  11. Sample.show();
  12.  
  13. }
  14. }

  • We can also call static method like this but this is not recommended.

static method in java interview questions




 Output:
  1. show() method called
  2. show() method called

  • Now our question is can we call super class static method from sub class?
  • Yes we can call super class static method inside sub class using super_class_method();
  • We can also call super class static method using Sub_class_name.superclass_staticMethod()


  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public static void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }



  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void print(){
  4.  
  5.  System.out.println("Sub class print() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo.show();
  11. SubDemo.show();
  12. }
  13. }

Output:
  1. Super class show() method called
  2. Super class show() method called

  • If the same static method defined in sub class also then we can not call super class method using sub class name if we call them sub class static method will be executed.

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public static void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public static void show(){
  4.  
  5.   System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo.show();
  11. SubDemo.show();
  12. }
  13. }

 Output:
  1. Super class show() method called
  2. Sub class show() method called

Key points to remember:
  1. Can we Overload static methods in java
  2. Can we Override static methods in java

Inheritance interview programming questions : Part 2

1. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13. }
  14. }






2. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }



  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SuperDemo();
  11.  supobj.show(); 
  12.  
  13.  SubDemo subobj=new SubDemo();
  14.  subobj.show();
  15.  
  16.  }
  17. }





3. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7. }
  8.  public static void main(String args[]){
  9.  
  10. SuperDemo supobj= new SubDemo();
  11. supobj.show(); 
  12.  
  13. }
  14. }





4. what is the output of following program:

  1. package com.instanceofjava;
  2.  
  3. public class SuperDemo{

  4. public void show(){
  5.  
  6.   System.out.println("Super class show() method called");
  7.  
  8. }
  9.  
  10.  public void superMethod(){
  11.  
  12.         System.out.println("Super class superMethod() called");
  13.  
  14. }
  15.  
  16. }

  1. package com.instanceofjava;
  2. public class SubDemo extends SuperDemo{

  3. public void show(){
  4.  
  5.  System.out.println("Sub class show() method called");
  6.  
  7.  
  8. public void subMethod(){
  9.         System.out.println("Sub class subMethod() called");
  10. }

  11.  public static void main(String args[]){
  12.  
  13. SuperDemo supobj= new SubDemo();
  14. supobj.superMethod();
  15.  
  16. // supobj.subMethod();  // compile time error: The method subMethod() is undefined for the
  17. type Super

  18.  ((SubDemo)supobj).subMethod();
  19. ((SuperDemo)supobj).show();
  20. }
  21. }







Constructor chaining in java using this and super Keywords

Constructor chaining example program in same class using this keyword.


  1. package instanceofjava;
  2. class ConstructorChaining{
  3. int a,b 
  4. ConstructorChaining(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8.  
  9. ConstructorChaining(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. ConstructorChaining(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20.  
  21. public static void main(String[] args){
  22.  
  23.  ConstructorChaining obj=new ConstructorChaining();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2

Constructor chaining example program in same class using this and super keywords.

  1. package instanceofjava;
  2. class SuperClass{
  3.  
  4. SuperClass(){
  5. this(1);
  6.  System.out.println("Super Class no-argument constructor");
  7.  
  8.  
  9. SuperClass(int x ){
  10.  
  11. this(1,"constructor chaining"); 
  12. System.out.println("Super class one -argument constructor(int)");
  13.  
  14. }
  15.  
  16. SuperClass(int x , String str){
  17.  System.out.println("Super class two-argument constructor(int, String)");
  18.  
  19. }




  1. package instanceofjava;
  2. class SubClass extends SuperClass{

  3. SubClass(){
  4. this(1);
  5.  System.out.println("Sub Class no-argument constructor");
  6.  
  7.  
  8. SubClass(int x ){
  9.  
  10. this("Constructor chaining"); 
  11.  System.out.println("Sub class integer argument constructor");
  12.  
  13. }
  14.  
  15. ConstructorChaining(String str){
  16. //here by default super() call will be there so it will call super class constructor
  17.   System.out.println("Sub class String argument constructor");
  18.  
  19. public static void main(String[] args){
  20.  
  21.  SubClass obj=new SubClass();
  22.  
  23.  
  24. }
  25. }

Output:

  1. Super class two-argument constructor(int, String)
  2. Super class one -argument constructor(int)
  3. Super Class no-argument constructor
  4. Sub class String argument constructor
  5. Sub class String argument constructor
  6. Sub class integer argument constructor




Select Menu