Welcome to Java

public class Sample 
{	
  public static void main(String args[])
  {
    System.out.println("Welcome Back to Java");
  }
}

Assigning Variables

public class Sample 
{	
  public String strName;
	
  public static void main(String args[])
  {
    Sample1 objSample1 =  new Sample1();
    objSample1.strName = "MugilVannan";
    System.out.println(objSample1.strName);
  }
}

A Class with default access can be seen by class in same package.If classA and classB are in different package and classA has default access classA wont be visible to class B

Class Sample1.java has a class sample3 with default access applied to it.

Sample1.java

package pkgSampler;

public class Sample1 
{
	public static void main(String[] args) 
	{
	  SampleA objSampleA =  new SampleA();
	  System.out.println(objSampleB.strName);
	}
}

class SampleA
{
	public String strName;
	
	SampleA()
	{
	  this.strName = "Mugil";	
	}
}

Some Other Class in Same Package Accessing Variable with in Class with Default Access
SampleB.java

package pkgSampler;

public class SampleB 
{
  public static void main(String[] args) 
  {
	SampleA objSampleA =  new SampleA();
	System.out.println(objSampleA.strName);
  }
}

The Methods in Class SampleA with default access in one package will only be visible to SampleB when it is Declared as Public.Otherwise it will compilation error.

Package1 with Class SampleA

 package Package1;
 class SampleA{}

Package2 with Class SampleB

 package Package2;
 import Package1.SampleA;
 class SampleB extends SampleA{}

Classes Declared as Final cannot be inherited.Nor its methods and variable can be overridden or extended.

 package Pack1;
 public final class SampleA
 {
  public void Sampler(){}
 }

Now ClassB tries to access classA

 package Pack2;
 import Pack1;
 public class SamplerNew extends Sampler{}

The above code will result in error

Abstract Classes
1.Abstract classes cant be instantiated.It could be only extended.

2.Methods marked as abstract should end with semicolon at its end.

eg:

  abstract class absFood
  {
     public abstract void SouthIndianThali();
     public abstract void Chinese(); 
  } 

3.Even if Single method is Abstract the whole class must be Abstract.You can put non abstract method in abstract class.

4.A Class can’t be abstract and Final

Interface
1.Interface is like contract which says what a class can do without telling how the class is going to do that.
2.Interface are like abstract class but abstract class can have both abstract and non abstract methods whereas interface can have only abstract methods.

3.Other Rules for Interfaces are
a.Interface Methods Must not be Final
b.Interface Methods Must not be Static
c.Variables in Interface should be public, Static and Final.
d.Methods in Interface are Implicitly Public and Abstract
e.Interface can only extend other interface
f.Interface cannot implement another interface.

4.Public modifier is required if you want teh interface to have public rather than default access.

5.All Interfaces methods are Implicitly public and abstract

e.g

public interface bounceable
{
   void Running();
   void setRunningSpeed(int Speed);
}

The Interface methods above are public and abstract as it used to be by default.The following applies the same for the below method.

void runnable();                      //Public 
public void runnable();               //Abstract     
abstract void runnable();             //Public 
abstract public void runnable();    
public abstract void runnable();    

The following interface method declaration wont compile

final void runnable()         //Interface shld nt be final
static void runnable()        //Interface define instance Methods
protected void runnable()     //Interface methods are public  
private void runnable()

Interface constants are public static final.But there is no need to declare them.

Assigning Constant in Interface

  interface Run
  {
    int BAR = 42;          //Public final and Static by Default
    void go(); 
  }

class Marathon implements Run
{
  public void go()
  {
   BAR = 27;             //Not Allowed Interface 
                         //Constant value should not be changed
  }
}

Access Modifiers
A class can use only two(public, default) of four modifiers.The four modifiers are
1.Public
2.protected
3.default
4.static

Class Zoo

class Zoo
{
  public string coolMethod()
  { 
     return "I am Cool";
  }
}

Class Animal

 
class Animal extends coolMethod
{
  public void coolAnimalList()
  {
    //The below code works because of inheritance
    System.out.println(this.coolMethod());

    //The below code works because of Instance Objects
    coolMethod objcoolMethod = new coolMethod();
    System.out.println(objcoolMethod.coolMethod());
  }
}

Note
Always look for Access level of Class.If the class itself is not accessible no wonder the methods would be.

Protected vs Default
Protected and default access control levels are almost identical.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 subclass in different package.


Class declared as protected in one package can be accessed only through inheritance in another class in other package

Default Access
Default members are visible to subclasses only if those subclasses are in same package as the superclass.

Note : Access modifiers can’t be applied to local variables

Local Variable can take only one modifier – final

Final Methods
Final methods prevents methods being overridden in a subclass and is often used to enforce the API functionality of method

Abstract Methods
An abstract method is a method that’s been declared but not implemented.The method contains no functional code.When a method is marked abstract the subclasses should provide the implementation.

If you declare a abstract method in class then the class should also be abstract.

There are three ways you could tell whether the method is abstract or not.

  • The method is not marked abstract
  • The method declaration includes curly braces
  • The method provides actual implementation code.

The First Concrete method of an abstract class must implement all abstract methods of the superclass

Concrete means Nonabstract class

eg.

public abstract class Vehicle
{
 public abstract goUpHill();
}

public abstract class car extends Vehicle
{
  public abstract goUpHill();

  public void dpThings()
  {
    ...
    ...
  }
}

//First Concrete class since the above two classes are abstract
public class Mini extends Car
{  
  public void goUpHill()
  {
     
  }
}

A Abstract Method can Never be marked as both abstract and Final or both abstract and Private

Abstract Modifier can never be combined with Static modifier

Abstract means the super class does not know anything about how the subclasses should behave in that method where as final means the super class knows every things about the subclasses.

Constructors

Every Class has a Constructor, although if you don’t create one explicitly, the compiler will build one for you.

1.Constructor can’t ever, ever have return type.
2.Constructors can’t be marked static, they can’t be marked final or abstract.

Primitives
Eight Types – int, float, long, char, boolean, double, short, byte

For the Integers Small to Big Would be
byte, short, int, long, and doubles, floats

Instance Variables can be any of four access levels, final and transient

Instance Variables can’t be abstract, synchronized, strictfp, native and static

Local Variables
Local Variables are variables declared within a method.Local Variables life begins and Ends within a method.

Local variables are always on the stack and not on the heap

Local Variables cannot be public, transient, volatile, abstract, or static but local variables are final.

Shadowing:Declaring a local variable with the same name as instance variable

How to Declare a Array in Java
e.g
int[] key; //Recommended
int key []; //Legal but less readable

It is not legal to add size of array in your declaration

int[5] names; //The code wont compile

Final Object Reference
An object marked as final can never be reassigned to different object.The data within the object can be modified but the reference variable cannot be changed.

There are no final objects but only final references.

1.Final Class Cannot be Subclassed
2.Final Method Cannot be Overridden
3.Final Variable Cannot be assigned a new value.

Transient Variable Skip the variable when you attempt to serialize it.

Volatile Variable Tells the JVM that a thread accessing the variable must always reconcile its private copy of the variable with the master copy in memory.

Static methods and members
Static members exists even before you ever make a new instance of a class and there will be only one copy of static member regardless of number of instances of that class.In other words all instances of the class share the same value for the given static variable.

Classes, Constructors, Interfaces, Method, inner class methods and instances variables, local variables cannot be Static.

Leave a reply