Sunday, January 22, 2012

Variable Length Arguments Example - Java


package com.net4java.examples;

public class VarArgsExample {

 public static int addNumbers( int... numbers ) {
  int total = 0; 
  for ( int number : numbers ){
    total += number; 
  }
     return total;
 }
 
 public static void main( String args[] ) {
  int num1 = 10,num2 = 20,num3 = 40;
  
  System.out.printf( "Total:" + addNumbers(num1,num2) );
  System.out.printf( "\nTotal:" + addNumbers(num1,num2,num3) );
}

}


Object:

An object is a software construct that encapsulates data, along with the ability to use or modify and inspect that data, into a software entity.

Class:
  • A class is a blueprint for objects
  • A class is a collection of data and methods that operate on that data.
  • It defines a type of object according to the data the object can hold and the operations the object can perform.

Object Class:
The Object class is the highest superclass (ie. root class) of Java. All other classes are subclasses (children or descendants) of the Object class. The Object class includes methods such as:
clone()             equals()            copy(Object src)
finalize()          getClass()        hashCode()
notify()            notifyAll()      toString()
wait()

Class Access:

  • Default Access: Has no modifiers preceding it in the declaration, can be accessed by the classes in the same package.
  • Public Access: All classes in the Java Universe (JU) have access to a public class. Don't forget, though, that if a public class you're trying to use is in a different package from the class you're writing, you'll still need to import the public class.

Non Access Class Modifiers:

  • final:  When a class declared as final, it can not be sub classed. We need to make a class final only if you need an absolute guarantee that none of the methods in that class will ever be overridden.

  • abstract: An abstract class can never be instantiated. Its sole purpose, mission in life, is to be extended (sub classed).


Interface
When we create an interface, we are defining a contract for what a class can do, without saying anything about how the class will do it.

  • All interface methods are implicitly public and abstract.
  • All variables defined in an interface must be public, static, and final(constants)
  • Interface methods must not be static.
  • Because interface methods are abstract, they cannot be marked final,strictfp, or native.
  • An interface can extend one or more other interfaces.
  • An interface cannot extend anything but another interface.
  • An interface cannot implement another interface or class.


Access Modifiers:

Access modifiers determines the visibility and the scope of the Java elements.

A class can use two of the four access control levels (default or public), members can use all four:
public: Highest level of accessibility. When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member (assuming the class itself is visible).

protected & default: The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.

private: Members marked private can't be accessed by code in any class other than the
class in which the private member was declared

Non Access Member Modifiers:

Final Methods:
The final keyword prevents a method from being overridden in a subclass. 

Final Arguments:
Can not be modified within the method, means can not reassign a new value to the variable.

Final Variables:
Declaring a variable with the final keyword makes it impossible to reinitialize that
variable once it has been initialized with an explicit value

A reference variable marked final can't ever be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed. In other words, a final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object.




Abstract Methods

An abstract method is a method that's been declared (as abstract) but not implemented
The first concrete subclass of an abstract class must implement all abstract methods of the superclass.

It is illegal to have even a single abstract method in a class that is not explicitly declared abstract

Synchronized Methods
The synchronized keyword indicates that a method can be accessed by only one thread at a time.