×

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

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.