Java Notes For Java Beginner

To execute the java program in multitasking way, this concept is called as multi-threading.
      Multi-threading is used to overcome the idle time of CPU.

What is the procedure to create thread? And how it is execute?.
Inherit the java.lang.Thread class in user defined class. so our class becomes thread class. override the public void run() method thread class in our class. Create the object of our class. This is the new born stage of thread. Active this object using start() method of thread class. It will implicitly calls the overrides run() method.

Note : the program execution and time sharing to the threads are totally depends upon local operating system and the processor.

class A extends Thread
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
}
class B extends Thread
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 System.out.println("j is "+j);
 }
 
 public static void main(String args[])
 {
 A a1=new A();
 B b1=new B();
 
 a1.start();
 b1.start();
 
 for(int k=1;k<=50;k++)
 System.out.println("k is "+k)
 }
}
Another procedure to create and execute the Threads.       Implements the java.lang.Runable interface into user defined class, override it's only one method public void run() in our class. Still this is not the thread object, because our class is not extending the thread class.       So create the separate object of java.lang.Thread class and assign the user defined object of this thread object call the start() method using thread class object.       It will be implicitly calls the override run() method of respective object classes.
class AA extends object implements runnable
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
}
class BB implements runnable
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 System.out.println("j is "+j);
 }
 
 public static void main(String args[])
 {
 AA a1=new AA();
 BB b1=new BB();
 
 Thread f1=new Thread(a1);
 Thread f2=new Thread(b1);
 
 t1.start();
 t2.start();
 }
}

Thread controlling methods:

Methods which are used to manage the execution controlling of threads at running position.

1) sleep()

This method is used to interrupt the running thread for the given the interval in milliseconds. this interrupted thread will completing the time interval. This method throws interval. This method throws Interrupted Exception so we have to handle it at calling time.
class A extends Thread
{
 public void run()
 {
 for(int j=1;j<=50;j++);
 {
  try{
   if(j==15)
   thread.sleep(500);
  }
  catch(Exception e)
  {}
 System.out.println("j is "+j);
 }
 }
}
class B extends Thread
{
 public void run()
 {
 for(int i=1;i<=50;i++);
 System.out.println("i is "+i);
 }
 
 public static void main(String args[])
 {
 A a1=new A();
 B b1=new B();
 
 a1.start();
 b1.start();
 }
}

2) setPriority()

This method is used to set the execution of threads. It is just a request to local operating system. There are three type of priority:
  1. Minimum priority => 1
  2. Normal priority => 5
  3. Maximum priority => 10
A a1=new A();
B b1=new B();
C c1=new C();

a1.setPriority(10);
b1.setPriority(7);
c1.setPriority(5);

a1.start();
b1.start();
c1.start();

3) join()

This method is used to join or connect or connect one thread to other, so that the other thread will complete it's execution and normal goes into dead stage.
What is the procedure to kill any thread ?.
Use stop() method to kill the thread abnormally, but it is deprecated from java library. and it's replacement method join().
Write a program to join one thread to another using join() method.
class D extends Thread
{
 public D extends Thread
 {
  for(int i=1;i<=5000;i++)
  System.out.println("i is "+i);
 }
}
class E extends Thread
{
 D d1;
 E(D d1) //pass the reference from
 {
  this.d1=d1;
 }
 public void run()
 {
 for(int j=1;j<=50;j++)
  {
   try{
    if(j==15)
    d1.join();
   }
   catch(Exception e);
   {}
   
   System.out.println("j is "+j);
  }
 }
 
 public static void main(String args[])
 {
  D d1=new D();
  E e1=new E()d1; //pass reference to constructor
  
  d1.start();
  e1.start();
 }
}

Synchronization :

To execute the thread based program in process way, this concept is called as synchronization.
Keyword "synchronized" is used to define such methods.
class Test
{
 Synchronized public void fun1(String args[])
 {
  System.out.println(" [ Hello ");
  try
  {
   Thread.sleep(1000);
  }
  catch(Exception e1)
  {}
  System.out.println(" [ "+msg);
  try
  {
   Thread.sleep(1000);
  }
  catch(Exception e)
  {}
  System.out.println(" ] world ] ");
 }
}
class AB extends Thread
{
 Test t1;
 AB(Test t1)
 {
  this.t1=t1;
 }
 
 public void run()
 {
  t1.fun1("java");
 }
}
class BC extends Thread
{
 Test t1;
 BC(Test t1)
 {
  this.t1=t1;
 }
 
 public void run()
 {
  t1.fun1("C++");
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  
  AB a1=new AB(t1);
  BC b1=new BC(t1);
  
  a1.start();
  b1.start();
 }
}
Java Notes For Java Beginner
java packageThe set of built in classes stored under certain name and used as a library in java application, is called as package.
The super most package of JAVA hierarchy is "java".

java.lang package :

This package include all the basic classes which are required every java application.

Examples :
  • object
  • string
  • System
  • Exception
  • Thread
  • etc...

Note : This package is by default allocated to every java program.

java.io package :

This package are used to handle the input and output operation between editor-console, client-server and database handling program.
Examples :
  • DataInoutStream
  • BufferedReader
  • fileOutputStream
  • fileReader
  • etc...

java.net package :

This package contains all the network related classes and interfaces. They are used to handle the networking in client-server architecture.

Examples :
  • Socket
  • ServerSocket
  • INetAddress
  • DatagramPackage
  • etc...

java.sql package :

This package have data handling classes and interface.

Examples :
  • class:
    • DriverManager
  • Interfaces:
  • Connection
  • Statement
  • ResultSet
  • etc...

java.awt package :

The classes of this package are used to represent the graphical output of java application.

Examples :
  • Button
  • Lable
  • Textfield
  • frame
  • Panel
  • Checkbox
  • etc...



java.awt.event package:

The interfaces of this package are used to handle the events on graphical components.

Examples :
  • ActionListener
  • TextListener
  • WindowsListener
  • MouseListener
  • etc...


keyword "import" is used to include any of the above package in java application.

Example:

import java.io.*;
This statement will include all the classes and interfaces of this package ("*" mean all the classes)
import java.fileReader;
This statement will include any fileReader class in program other classes are not included.
import java.awt.*;
This statement will inlcude only awt classes.
It cannot import the event package. If we want to import event package element then we have to write.
import java.awt.event.*;

How to create and use it in another application ?
Declare the keyword 'package' in the heading selection of package class.
Declare the fully fully qualified path of this class that where to store it in memory (Hard disk).
Declare all the elements as public in and with class. Save and compile this application in working folder.
Then create the folder as mention with package keyword. Cut the .class file of this class and paste it into the created package folder. This is the package creation process.
Create another java application in working folder. Include the package using import keyword and fully qualified path of package in new application, Now we can with it's elements.
//Package Code
package Abc.Outer.Inner.Pack1;
public class A //public class important
{
 public void fun1()
 {
  System.out.println("Hello package class method");
 }
}

Working :

my working folder
D:\xyz
save and compile

Create folders as mention with package keyword in working folder.
D:\xyz\Abc\Outer\Inner\pack1
cute and paste the A.class file into pack1 folder.

Now file location will be
D:\xyz\Abc\Outer\Inner\pack1\A.class

Create another java application in which this package class should be imported.

//Program code
class B
{
 public void fun2()
 {
  System.out.println("Hello fun2");
 }
 public static void main(String args[])
 {
  A a1=new A();
  a1.fun1();
  B b1=new B();
  a1.fun2();
 }
}


Another process to create package and store the .class file into it.

Use the -d attribute with javac command to create the directories. It creates the specified packages or directories with package keyword.

The "."
Directories should be created in working folders. And then the .class file of package class is stored in specified folder.

JAVAC -d . A.JAVA
-d for directors
. for working folder

Java Notes For Java Beginner
Inner classes

Inner classes :

To define one class into another, this process is called as inner classes.

There are four type of inner classes
1) Member inner class.
2) Static inner class.
3) Local inner class.
4) Anonymous inner class.


Member inner class :

A class which is defined as a member of some outer class is called as member inner class.

Write a program to define the member in inner class.
class outer
{
 int x=10;
 void fun1()
 {
  System.out.println("Hello outer class method");
 }
 class inner
 {
  int y=20;
  void fun2()
  {
   System.out.println("Hello inner class method");
  }
 } //inner class end
 
 public static void main(String args[])
 {
  outer a1=new outer();
  System.out.println("a1.x");
  a1.fun1();
  
  //a1.fun2();error
  //Inner il=new Inner();error
  
  outer.Inner i2=new Outer().new Inner();
  System.out.println(i2.y);
  
  //il.fun1();error
 }
} //outer class end

Static inner class :

Inner classes can be static we can instantiate this class directly or by the outer class name.
Write a program to define the static inner class.
class outer
{
 static int x=10;
 void fun1()
 {
  System.out.println("Hello outer class method");
 }
 static class inner
 {
  int y=20;
  void fun2()
  {
   System.out.println("Hello inner class method");
  }
 } //inner class end
 
 public static void main(String args[])
 {
  outer a1=new outer();
  System.out.println("X is "+x);
  System.out.println("X is "+a1.x);
  a1.fun1();
  
  Inner i=new Inner();
  System.out.println("Y is "+i.y);
  i.fun2();
 // OR
  outer.Inner il=new outer.inner();
  System.out.println("Y is "+il.y);
  il.fun2();
 }
} //outer class end

Local inner class :

A class which is defined in method body of outer class, this is called as local inner class. The scope and accessibility of this class is limited up to the method in which it is defined.
Write a program to define the local inner class.
class outer1
{
 int y=20;
 void fun1()
 {
  System.out.println("Enter into method");
  
  class local
  {
   int x=50;
   void fun2()
   {
    System.out.println("Hello local class method");
   }
  };
  
 local L1=new local();
 System.out.println("X is "+L1.x);
 L1.fun2();
 System.out.println("Exit from method");
 } //fun1

 public static void main(String args[])
 {
  outer1 a1=new outer1();
  System.out.println("Y is "+a1.y);
  a1.fun1();
 }
} //outer class end

Anonymous inner class :

A local inner class, which does not have name and implements the outside interface, is called as anonymous inner class.
Write a program to define the anonymous inner class.
interface xyz
{
 public void fun1();
 public void fun2();
}
class test
{
 public xyz getxyz1()
 {
  xyz x1=new xyz()
  {
   public void fun1()
   {
    System.out.println("Hello first class fun1");
   }
   public void fun2()
   {
    System.out.println("Hello first class fun2");
   }
  };
 return(x1);
 } //getxyz1
 
 public xyz getxyz2()
 {
  xyz x2= new xyz()
  {
   public void fun1()
   {
    System.out.println("Hello second class fun1");
   }
   public void fun2()
   {
    System.out.println("Hello second class fun2");
   }
  };
 return(x2);
 } //getxyz2
 public static void main(String args[])
 {
  Test t1=new Test();
  xyz x3=t1.getxyz1();
  xyz x4=t1.getxyz2();
  
  x3.fun1();
  x3.fun2();
  
  x4.fun1();
  x4.fun2();
 }
}


class AB
{
 {
  System.out.println("Non static block");
 }
 static
 {
  System.out.println("static block");
 }
 
 AB()
 {
  System.out.println("Constructor block");
 }
 
 fun1()
 {
  System.out.println("Method block");
 }
 
 public static void main(String args[])
 {
  System.out.println("Main block");
  
  AB a1=new AB();
  a1.fun1();
  
  AB a2=new AB();
 }
}

static block
Main block
Constructor block
Method block

Non static block
constructor block
Java Notes For Java Beginner
Abstract And Final Class

Abstract class

  • A partially implemented and partially unimplemented class, is called as abstract class.
  • Definition : A class which have at least one abstract method, is called as abstract class explicitly.
  • Keyword abstract is used to declare this class explicitly, Abstract class cannot be instantiated because it may have abstract method.

Write a program to define abstract class execution.
abstract class Abs1
{
 public void fun1()
 {
  System.out.println("Hello abstract class method");
 }
 abstract public void fun2();
}

class Abs extends Abs1
{
 public void fun2()
 {
  System.out.println("Hello fun2 sub class method");
 }
 
 public static void main(String args[])
 {
  Abs a1 = new Abs();
   a1.fun1();
   a1.fun2();
  Abs1 a2 = new Abs();
   a1.fun1();
   a1.fun2();
 }
}



Abstract class can created in 3 ways:
1) Declare abstract method in class.
2) Declare keyword 'abstract' with class.
3) Do not implement the interface completely.

Final class:

  • A class which can Inherit others but cannot be inherited by other is called final class.
  • Keyword "final" is used to declare final class explicitly.
  • final class is the last class of inheritance chain.
Write a program implement final class execution.
final class fin extends object
{
 public void fun1()
 {
  System.out.println("Hello final class method");
 }
}
class fun1
{
 public void fun2()
 {
  System.out.println("Hello sub class method");
 }
 public static void main(String args[])
 {
  fin1 f1=new fin1();
  //f1.fun1();
  f1.fun2();
  fun f2=new fun();
  f2.fun();
 }
}


Difference between abstract and final class.
1) Abstract class cannot be instantiated.
2) Abstract class should be inherited in some other class.
3) Abstract class may have abstract methods.
4) Super class reference and sub class object concept is followed by abstract.
5) Abstract keyword is used to declare this class
1) The object creation process of final class is compulsory.
2) final class cannot be inherited.
3) final class doesnot have abstract method.
4) this concept is not followed by final class.

5) final keyword is used to declare this class.


Note : The class can be either abstract or final.


abstract
final
static
  • class
  • variables
  • methods
  • constructor
Java Notes For Java Beginner

We can create the reference of super class and assign the sub class object to it. The execution behavior of this reference variable is depends upon the super class. But the execution is like basic interface.

Super class reference & sub class object
class A
|
♦-- Fun1, Fun2, FunX
|

class B
|
♦-- Fun2, FunB, FunN
|

class C
|
♦-- Fun1, FunX, Fun3
|

class D
|
♦-- FunN, FunB, Fun3
|

D d1 = new D();
d1.fun1(); //C
d1.fun2(); //B
d1.funN(); //D

C c1 = new C();
c1.funN();
c1.fun1();
c1.fun2();

A a1 = new D();
a1.fun1(); //C
a1.fun2(); //B

//a1.funN(); error
a1.funX(); //C

B b1=new C();
b1.funX(); //C
b1.funb(); //B

D d1=new A(); //Note possible
The sub class reference and super class object cannot is not possible because this object doesn't fulfill the context requirement of sub class refernece.


Write a program of dynamic polymorphism.

class AA
{
 void AA()
 {
  System.out.println("Hello class AA method");
 }
}
class BB extends AA
{
 void BB()
 {
  System.out.println("Hello class BB method");
 }
}
class CC extends BB
{
 void fun1()
 {
  System.out.println("Hello sub class CC method");
 }
 
 public static void main(String args[])
 {
  int n=args.lenth;
  
  if(n>0)
  {
   AA.a1;
   int a=Integer.parseInt(args[0]);
   switch(a);
   {
    case 1:{
       d=new AA();
       break;
        }
    case 2:{
       a1=new BB();
       break;
        }
    case 3:{
       a1=new CC();
       break;
        }
    default:{
       System.out.println("Invalid Arguments");
       a=new AA();
       break;
      }
   }
  a1.fun();
  }
  else
  {
   System.out.println("Please enter arguments");
  }
 }
}
Java Notes For Java Beginner

Interface

Interface in javaA special type of class, which is used to declare only abstract methods is called as interface.

Keyword "interface" is used to define this class.

Keyword "implemets" is used to inherit the interface into classes.

interface xyz
{
 public void fun1();
 public void fun2();
}
class A implements xyz
{
 public void fun1()
 {
  System.out.println("Hello Function 1");
 }
 public void fun2()
 {
  System.out.println("Hello Function 2");
 }
 public void fun3()
 {
  System.out.println("Hello Function 3");
 }
 
 public static void main(String args[])
 {
  A a1=new A();
  a1.fun1();
  a1.fun2();
  a1.fun3();
 }
}


Note : One class can implements more than one interface.

Example :
Class A implements xyz, pqr, abc.
It look like multiple inheritance but it is not actually.

IMP Note :
By default all the elements declared in interface are in public.



Final keyword :

Keyword "final" is used to create constant in Java program. The values of these variables are never changed at runtime.

Example :
final int x=3;
x=5; // error
x=3; // error
x++; // error

IMP Note :
By default the interface variables are public, static and final.

interface ABC
{
 public final static int x=10;
 int y=20;
}
class B implements ABC
{
 public static void main(String args[])
 {
  B b1=new B();
  
  System.out.println("X is "+b1.x);
  System.out.println("Y is "+b1.y);
  
  // b1.X=50;  error
  // b1.Y=100; error
  
  System.out.println("X is "+ABC.x);
  System.out.println("Y is "+ABC.y);
 }
}

Java Notes For Java Beginner

Inheritance:

The concept of reutilazation is called as inheritance.
Inheritance is used to eliminate the reduandancy of program.


Note : Keyword "extends" is used to inherit one class into another class.

The class which gives it's properties to another class is called as super class or base class.
The class which takes properties from other class is called as sub-class or derived class.

Syntax:
class B extends A
{
-----
-----
}

Types of inheritance

1] Single inheritance :

The one to one communication between super and sub class, this concept is called as single inheritance.

/*Example of simple inheritance */
class A
{
 void fun1()
 {
  System.out.println("Hello this is Super class method");
 }
}
class B extends A
{
 void fun2()
 {
  System.out.println("Hello this is Sub class method");
 }
 
 public static void main(String args[])
 {
  A a1=new A();
  B b1=new B();
  
  a1.fun1();
  b1.fun2();
  b1.fun1();
 }
}


2] Multilevel inheritance :

To inherit more than one classes in chain format with a finite termination, this concept is called as multilevel inheritance.

Tip [Rule] message : One java class can extends only one java class.
According to this rule, Java doesn't follow multiple, Multi-path and Hybrid inheritance.

Note : Class object is the super must built in class of java hierarchy. Every built in and user defined classes should inherit the object class. This class provides the context creation, constructor calling, data overloading and execution properties to the object creation statement.


JAVA Does not Follow The Cyclic Inheritance.

Constructor are executed in the manner they are defined in inheritance


Write a program to define the constructor execution inheritance.

class A extends object
{
 A()
 {
  System.out.println("Class A Constructor");
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B Constructor");
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C Constructor");
 }
}
class D extends C
{
 D()
 {
  System.out.println("Class D Constructor");
 }
 
 public static void main(String args[])
 {
 D d1=new D();
 C c1=new C();
 B b1=new B();
 A a1=new A();
 }
}



Super


Note : Kayword 'super' is used to call the super class parameterized constructor from sub class constructor.

"Super" should be the first statement of constructor body.

Write a program to call the super class default and parameterized constructor from sub class.

class A
{
 A()
 {
  System.out.println("Class A constructor");
 }
 A(int a)
 {
  System.out.println("A is "+a);
 }
}
class B extends A
{
 B()
 {
  System.out.println("Class B constructor");
 }
 B(int b)
 {
  System.out.println("B is "+b);
 }
}
class C extends B
{
 C()
 {
  System.out.println("Class C constructor");
 }
 C(int c)
 {
  System.out.println("C is "+c);
 }
 
 public static void main(String args[])
 {
 C a1=new C();
 }
}


Java program execution criteria :


JVM loads all the static data of class into memory. At last it loads the main method. But executes the main() method first to all main() method creates objects.
Object's call the constructor creates context for remaining non-static data of class. It loads this data into newly created context and assign the control to the object.

Method overriding :


To define the same name method with same signature and same arguments in super class as well as in sub class, this concept is called as method overloading.

Note : Keyword "super" is used to call the super class overrided method from sub class method.

Write a program to define methods overriding.

class P
{
 void fun1()
 {
  System.out.println("Hello super class method");
 }
}
class Q extends P
{
 void fun1()
 {
  Super.fun1();
  System.out.println("Hello Sub class method");
 }
 public static void main(String args[])
 {
  Q q1=new Q();
  q1.fun1();
 }
}