×

Java Default Keyword

  1. The Default keyword in java programming language is used as access modifier.
  2. If any of the variable or the constructor or the methods or the classes are not assigned with the values or names, then the code will take the values by itself which we consider as the default value.
  3. The default values will print even if the default keyword is not written in some code.
  4. Default value is an optimal keyword or non mandatory keyword.
  5. Default keyword is also known as the default access modifier.
  6. Generally, we have main usage or applications of default keyword in three cases or places. Below are the following cases:
    • In switch case statements the default keyword helps in specifying the default value.
    • In java annotation default keyword prints the default value.
    • In the interface of the program the default method is executed.

Points to remember:

  • The modifier of the default is provided with in the given package only.
  • It doesn’t behave like the modifiers such as private and protected.
  • Creating the default outer class can be done by not providing any access modifier to it.
  • It doesn’t avoid specifying the class name like the program name.
  • The overridden method should not be more restricted if any overriding method is involved.
  • The methods which are known as default methods rescue the answers, which solves all the queries and it provides interfaces to give methods with their default values.

Syntax:

Default;

Default Keyword Examples in Java

Example 1:

package.java.
 class P {  
 String m="Its too late";


}
package. jtp;
import .java.P;


public class DefaultEx1 {  
 void main (String [] as) {  
Pp=new P ();
   System.out.println(p.m);


}  
}  

Output:

Exception in thread "main" java. lang. Error

Example 2:

Default variable is providing or not outside the class inside the package.

class P { 
 String m="try to get it";
} 
class DefaultEx2 { 
 void main (String [] args) {  
Pp=new P ();
   System.out.println(p.m);
}  
}  

Output:

try to get it

Example 3:

Illustrate whether the default method is provided or not outside the package.

//Q.java is the saved file
package.java.
public class Q {  
void m ()  
 {  
     System.out.println("default method is accessed ");
 }     
}  
package.jtp.
import com.java. Q.


public class DefaultEx2 {  
public static void main (String [] args) {  
Qq=new Q ();
q.m();


}  
}  

Output:

 Error is occurred which is non visible file error

Example 4:

package.java.
public class M {  


  void m()  
 {  
     System.out.println(" inheritance package");
 }      
}   
package.jtp.
import com.java. M;


public class DefaultEx4 extends M {  
public static void main (String [] args) {  
    DefaultEx4 p=new DefaultEx4();
p.m ();


}  
}  

Output:

java. lang. Error:
The method m () from the type Q is not visible

Example 5:

instance of default constructor outside the package
class DefaultEx5 {  
void dis ()  
{  
    System.out.println("outer default class");
}  
public static void main (String [] args) {  
    DefaultEx5 r=new DefaultEx5();
r.dis ();
}  
}  

Output:

outer default classs

Example 6

instance of default constructor outside the package

instance of default constructor

package.java.


public class R
{  
 String chat.
R (String chat)  
 {  
this. chat=chat;
 }  
public void dis ()  
{  
    System.out.println(chat);
}  


}  
//DefaultEx6.java  
package com. jtp.
import com.java.R;


public class DefaultEx6 {  
public static void main (String [] args) {  
Rr=new R ("Try to create the instance of default constructor outside the package");
r.dis ();
}  
}  

Example 7:

Illustarting whether the given method is default method is overridden to sub-class implementing private access modifier using private keyword.

class G
{  
    void mit ()  
    {  
        System.out.println("Try it");
    }  
}  
//DefaultEx7.java  
class DefaultEx1 extends A {  
    void mit ()  
{  
    System.out.println("provides overridden method");
}  
public static void main (String [] args) {  
    DefaultEx7 p=new DefaultEx7();
g.mit();  


}  
}  

Output:

provides overridden method

Example 8:

Illustarting whether the given method is default method is overridden to sub-class implementing private access modifier using private keyword.

class A  
{  
    void man ()  
    {  
        System.out.println("Do it");
    }  
}  


class DefaultEx8 extends A {  
    private void man ()  
{  
    System.out.println("overridden method");
}  
public static void main (String [] args) {  
    DefaultEx8 r=new DefaultEx8();
r.man ();


}  
}  

Output:

error

Related Topics

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

How to set path in Java

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated Development...

5 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.