X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

X

Formal parameters :

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input passed.

final modifier :

final is the modifier applicable for classes, methods and variables. If a method is declared as final then we are not allowed to override that method in the child class.

Example :

class Algo { public void m1() { System.out.println("Algo"); } public final void m2() { System.out.println("Valley"); } } class Example extends Algo { public void m2() { System.out.println("Example"); } public static void main(String [ ] args) { Algo a = new Algo(); a.m1(); } }

Output:

Compile time error : m2() in Example cannot override m2() in Algo

If a class is declared as final then we can't create child class.

Example :

final class Algo { public void m1() { System.out.println("Algo"); } } class Example extends Algo { public static void main(String [ ] args) { Algo a = new Algo(); a.m1(); } }

Output:

Compile time error : cannot inherit from final Algo

Every methods present inside a final class are always final by default. But every variables present in final class are not need to be final.

In general for instance and static variables it is not required to perform initialization explicitly, JVM will always provide default values. But for the local variable JVM won't provide any default values and it is compulsory that we should provide initialization before using that variable.

class Algo
{
 public static void main(String [ ] args)
 {
  int x;
  System.out.println("Algo");
 }
} // Output : Algo

class Algo
{
 public static void main(String [ ] args)
 {
  int x;
  System.out.println("Algo");
 }
} // Compile time error : variable x might not have been initialized

For the normal instance variables it is not required to perform initialization explicitly, JVM will provide default values. If the instance variable declared as final then it is compulsory that we should perform initialization whether we are using it or not otherwise we will get compile time error.

class Algo
{
 final int x;
} // Compile time error : variable x might not have been initialized

For the final instance variables we should perform initialization before constructor completion.

class Algo
{
 final int x = 10;
} // At the time of declaration

class Algo
{
 final int x;
 {
  x = 10;
 } // instance block
} // inside instance block

class Algo
{
 final int x;
 Algo()
 {
  x = 10;
 }
} // inside constructor

Other than these if we are performing initialization then we will get compile time error.

class Algo
{
 final int x;
 public void m1()
 {
  x = 10;
 }
} // Compile time error : cannot assign a value to final variable x

For the normal static variables it is not required to perform initialization explicitly, JVM will always perform default values. But for final static variables we should perform initialization explicitly otherwise we will get compile time error.

class Algo
{
 static int x;
} // valid

class Algo
{
 static final int x;
} // Compile time error : variable x might not have been initialized

For the final static variables we should perform initialization before class loading completion.

class Algo
{
 final static int x = 10;
} // At the time of declaration

class Algo
{
 final static int x;
 static
 {
  x = 10;
 } // static block
} // inside static block

Other than these if we are performing initialization then we will get compile time error.

class Algo
{
 final static int x;
 {
  x = 10;
 } // instance block
} // inside instance block
// Compile time error :  cannot assign a value to final variable x

class Algo
{
final static int x;
 Algo()
 {
  x = 10;
 }
} // inside constructor
// Compile time error :  cannot assign a value to final variable x

class Algo
{
 final static int x;
 public void m1()
 {
  x = 10;
 }
} // Compile time error : cannot assign a value to final variable x

For the local variables declared as final then JVM won't provide any default values so, it is compulsory that we should initialize it before using those variables.

class Algo
{
 public static void main(String [ ] args)
 {
  final int x;
  System.out.println("Algo");
 }
} // Output : Algo

class Algo
{
 public static void main(String [ ] args)
 {
  final int x;
  System.out.println("Algo");
 }
} // Compile time error : variable x might not have been initialized

The only applicable modifier for local variable is final (and default member is possible). If we are using any other modifier then we will get compile time error.

class Algo
{
 public static void main(String [ ] args)
 {
  public int x = 10;
  System.out.println(x);// Compile time error : illegal start of expression
  protected int x = 10;
  System.out.println(x);// Compile time error : illegal start of expression
  private int x = 10;
  System.out.println(x);// Compile time error : illegal start of expression
  static int x = 10;
  System.out.println(x);// Compile time error : illegal start of expression
  final int x = 10;
  System.out.println(x);// Output : 10
 }
} 

Formal parameters of a method can be access as local variables and hence can be declared as final. If we declare a formal parameter as final within the method then we can't change it's value otherwise we will get compile time error.

class Algo
{
 public static void m1(final int x , int y)
 {
  x = 11;
  y = 21;  
 } 
 public static void main(String [ ] args)
 {
  m1(10 , 20);
 }
}// Compile time error : final parameter x may not be assigned

The main advantage of final keyword is that we can achieve security as no one is allowed to change it's implementation. But the main disadvantage of final keyword is that we are missing key benefits of OOP's Inheritance and Polymorphism(overriding).


static modifier :

static is a modifier applicable for variables and methods but not for classes (but inner classes can be declared as static). If the value of a variable is varied from object to object then we should go for instance variable. In the case of instance variable, for every object a separate copy of variable will be created. If the value of variable is same for all objects then we should use static variables. In such case only one copy of variable will be created at class level.

class Algo
{
 int x = 10;
 static int y = 20;
 public static void main(String [ ] args)
 {
  Algo a = new Algo();
  a.x = 11;
  a.y = 21;
  Algo a1 = new Algo();
  System.out.println(a1.x + " " + a1.y);
 }
}// Output : 10 21
X

implementation :

The process of putting a decision or plan into effect; execution.

static members can be accessed from both instance and static access where as instance members can be accessed only from instance context directly i.e. from static context we can't access instance members directly otherwise we will get compile time error.

For static methods, implementation is compulsory whereas for abstract methods, implementation should not be available. Hence, abstract - static combination is illegal for methods.

For static methods, overloading concept is applicable. Hence, within the same class we can declare two main methods with different argument.

class Algo
{
 public static void main(String [ ] args)
 {
  System.out.println("Algo");
 }
 public static void main(int [ ] args)
 {
  System.out.println("Valley");
 }
} // Output : Algo 

JVM always call main() method with String argument. The other main() method can be called explicitly just like a normal method call.

Inheritance concept is applicable for static methods including main() method hence while executing child class if the child class doesn't contain main() method then the parent class main() method will be executed.

class Algo extends Example
{

} 
class Example 
{
 public static void main(String [ ] args)
 {
  System.out.println("Algo");
 }
}
// save as : Example.java
// compile as : javac Example.java
// run as : java Example => Output : Algo 
// run as : java Algo => Output : Algo 

It seems that overriding concept is applicable for static methods but it is not overriding, it is method hiding.

class Algo extends Example
{
 public static void main(String [ ] args)
 {
  System.out.println("Algo");
 }
} 
class Example 
{
 public static void main(String [ ] args)
 {
  System.out.println("Algo");
 }
}
// save as : Example.java
// compile as : javac Example.java
// run as : java Example => Output : Algo 
// run as : java Algo => Output : Algo 

abstract modifier :

abstract is the modifier applicable for classes and methods but not for variables.


abstract method :

Even though we don't know about implementation but still we can declare a method with abstract modifier i.e. abstract methods can have only declared but not implemented. Hence, every abstract method declaration should be end with terminator(;). Child classes are responsible to provide implementation for parent class abstract methods.

abstract class Algo
{
 public abstract void m1();
}
class Example1 extends Algo
{
 public void m1()
 {
   System.out.println("Algo");
 }
}
class Example2 extends Algo
{
 public void m1()
 {
   System.out.println("Valley");
 }
}

The modifiers which are not allowed with abstract methods are final, static, synchronized, native, strictfp and private.


abstract class :

For any Java class if we don't want instantiation then we have to declare that class as abstract i.e. for abstract classes, instantiation (creation of object) is not possible.

Example :

abstract class Algo { } class Example extends Algo { public static void main(String [ ] args) { Algo a = new Algo(); } }

Output:

Compile time error : Algo is abstract; cannot be instantiated

strictfp modifier :

strictfp is a modifier applicable for methods and classes but not for variables.

If a method is declared as strictfp then all floating point calculations in that method has to follow IEEE 754 standard so that we will get platform independent results.

strictfp method always talk about implementation where as abstract method never talk about implementation. Hence, abstract - strictfp method combination is illegal.

X

concrete :

It refers to something existing in a material or physical form i.e not abstract.

If a class is declared as strictfp then every concrete method in that class has to follow IEEE 754 standard so that we will get platform independent results.

abstract - strictfp combination is legal for classes but illegal for methods.

abstract strictfp class Algo
{

} // valid

public abstract strictfp void m1(); // invalid

native modifier :

native is the modifier applicable only for methods but not for variables and classes.

The native methods are implemented in some other languages like C & C++. Hence, native methods also known as foreign methods.

X

legacy :

It refers to something denoting or relating to software or hardware that has been substituted but is difficult to replace because of its wide use.

The main objectives of native keyword are to improve performance of the system and use already existing legacy Non-Java code.

Pseudo code : 

class Algo
{
 static
 {
  System.loadLibrary("Native Library");
 }
 public native void m1();
}
class Example
{ 
 public static void main(String [ ] args)
 {
  Algo a = new Algo();
  a.m1();
 }
}

For native methods, implementation is already available in other languages and we are not responsible to provide implementation. Hence, native method declaration should end with terminator (;).

class Algo
{
 public native void m1()
 {
 
 }
} // Compile time error : native methods cannot have a body

For native methods, implementation should be available in some other languages where as for abstract methods, implementation should not be available. Hence, abstract - native combination is illegal for methods.

native methods cannot be declared with strictfp modifier because there is no guarantee that the old language follows IEEE 754 standard. Hence, native - strictfp combination is illegal for methods.

The main disadvantage of native keyword is that it breaks platform independent nature of Java because we are depending on result of platform dependent languages.


synchronized modifier :

synchronized is the modifier applicable for methods and blocks. We can't declare classes and variables with this keyword. If a method or block is declared as synchronized then at a time only one thread is allowed to operate on the given object.

X

inconsistency :

It refers to the fact or state of not staying the same throughout.

X

thread :

A thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.

X

constraint :

It refers to limitation or restriction.

X

complexity :

It refers to the state or quality of being intricate or complicated.

X

outdated :

It refers to something no longer produced or used i.e. out of date.

The main advantage of synchronized keyword is that it can resolve data inconsistency problems. But the main disadvantage of synchronized keyword is that it increases waiting time of the thread and effects performance of the system. Hence, if there is no specific requirement then it is never recommended to use synchronized keyword.


transient modifier :

transient is the modifier applicable only for variables and we can't apply it for methods and classes. At the time of serialization, if we don't want to save the value of a particular variable to meet security constraint then we should use transient keyword. At the time of serialization, JVM ignores the original value of transient variable and default value will be serialized.


volatile modifier :

volatile is the modifier applicable only for variables but not for methods and classes. If the value of a variable keeps on changing then such type of variable need to be declared with volatile modifier. If a variable declared as volatile then for every thread a separate local copy will be created.

Every intermediate modification performed by the thread will take place in local copy instead of master copy. Once the value get finalized then just before terminating the thread, the master copy value get updated with local value.

volatile variable means that the value keeps on changing whereas final means that the value never changes. Hence, final - volatile combination is illegal for variables.

The main advantage of volatile keyword is that it resolves data inconsistency problems. But the main disadvantage of volatile keyword is that it creates and maintain a separate copy for every thread which increases the complexity of the program and effect performance of system. Hence, it is not recommended to use and it is almost outdated.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.