Java Default Keyword
- The Default keyword in java programming language is used as access modifier.
- 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.
- The default values will print even if the default keyword is not written in some code.
- Default value is an optimal keyword or non mandatory keyword.
- Default keyword is also known as the default access modifier.
- 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