Introduction to Multithreading /Chapter 1 / Multi Threading

Multi Threading

java multithreading


We can define java multithreading in the following two ways:-



  • By extending Thread class

  • By implementing Runable interface


1. By extending Thread class:-


example:-
class MyThread extends Thread
{
public void run ()//This for run method will be executed by child thread
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
}
class ThreadDemo
{
public static void main(String [] args)
{
MyThread t =new MyThread();

t.start();// starting of thread

for(int i=0;i<10;i++)
{
System.out.println("Main Thread");//This for Loop will be executed by main thread
}
}
}

run method:-


run method present is thread class and it has an empty implementation. child thread is responsible for executing run method. So we have to override the thread class run method in our java program. If we don't override run method then it's super class means thread class run method will be called and it has empty implementation so no output will be produced.

Java Customized Exceptions / Chapter 11 / Exception Handling

Java Customized Exceptions


Sometimes to fulfill the programming requirement we can define our own exceptions such type of exceptions are called customized exceptions.

Let's take an example of customized exceptions.
import java.util.*;
class TooYoungException extends RuntimeException
{
TooYoungException (String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException (String s)
{
super(s);
}
}
class CustomizeException
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
if(age>60)
{
throw new TooOldException("Your age is already crossed the marriage age...
try In next birth... ");
}
else if(age<18)
{
throw new TooYoungException("Please wait some time...
You will get your best match soon... ");
}
else
{
System.out.println("Congrats!!!You are eligible for marriage..
you will get details of your match by email!!");
}
}
}

Output is:-

java throws keyword /Chapter 10 / Exception Handling

java throws

Java throws keyword


We can use throws keyword to delegate the responsibility of exception handling to the caller. It may be another method or JVM. After then caller method is responsible to handle that exception.

example:-
class Test
{
public static void main(String [] args) throws InterruptedException
{
Thread.sleep(5000);
}
}


java throw keyword /Chapter 9 /Exception Handling

java throw

java throw keyword:-


throw keyword used to create our own exception object explicitly and we can handover this (our own created) exception object to JVM manually.


example:-java throw

The main objective of throw keyword is to hand over our created exception object to JVM manually.


Let's take two example where the output will be same.

Some Possible Combinations of try catch finally /Chapter 8 / Exception Handling

try catch finally


Some possible combinations of try catch finally


try 
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(ArithmeticException e)
{

}

The above combination is invalid.And we will get Compile Time Error.

Difference Between final,finally,finalize /Chapter 7 / Exception Handling

Difference Between final, finally, finalize


final is a modifier applicable for method, classes & variables.


1.final method:-


Whatever method parent class has by default available to the child class through inheritance.

If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".


But,

If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.


2. final class:-

If a class declared as final we can't extend the functionality of that class so that we can not create the child class for that class. That's why inheritance is impossible for final classes.


try with multiple catch blocks / Chapter 6 / Exception Handling

try with multiple catch blocks


A way of handling an exception is varied from exception to exception hence, for every exception type, it is highly recommended to take separate catch block.

Let's take two examples of try catch.

Bad Or Worst Programming Practice
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

Best and Recommended Programming Practice

Method To Print Exception Information /Chapter 5 / Exception Handling

Method To Print Exception Information:-


The Throwable class defines the following methods to print Exception Information.Methods to print exception information

java try catch / Chapter 4 / Exception Handling

java try catch


java try catch block is highly recommended to handle exceptions.

The code which may rise an exception is called risky code and we have to define that code inside try block and corresponding handling code we have to define inside catch block.
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

let's take an example of a java program without try catch

Checked and Unchecked Exceptions /Chapter 3/ Exception Handling

Checked Exceptions


Checked Exceptions


The exceptions which are checked by the compiler for smooth execution of the program at runtime are called, checked exceptions.

example:-

FileNotFoundException,ServletException etc.

In our java program, if there is a possibility of rising checked exception then mandatory we should handle that checked exception (either by try catch or throws keyword) otherwise, we will get compile time error.

Java Exception Hierarchy / Chapter 2/ Exception Handling

java exception hierarchyJava Exception Hierarchy



  • The Throwable class is the root class for java exception hierarchy.

  • The Throwable class defines two child classes.



  1. Exception

  2. Error


1.Exception:-


Most of the time exceptions are occurs by our java program and these are recoverable.

for example: -


In our java program, there is a requirement of reading data from a remote file which is locating at NewYork (USA). At runtime, if NewYork file is not available our program should not be terminated abnormally and we will get RunTime exception saying"FileNotFoundException". To solve this problem we have to provide some local file to continue rest of the program normally.

Introduction of Exception Handling /Chapter -1 / Exception Handling

java exception

Java Exception:-


An unexpected unwanted event that disturbs normal flow of the program, is called an exception.

The purpose of exception handling:-


The main objective of exception handling is "graceful termination " of the program. It is highly recommended to handle the exception in java program.

Explanation:-


Exception Handling doesn't mean repairing an exception. We have to provide an alternative way so that the rest of the program will execute normally, is the concept of exception handling.


for example: -

Difference between interface and abstract class / Chapter 14 / OOPs Concept

Difference between interface and abstract class

Difference between interface and abstract class



  1. In the interface, if we don't know about implementation and just we have requirement specification then we should go for an interfaceBut In the abstract class, If we are talking about partial implementation but not complete then we should go for abstract class.

  2. In the interface, every method is always public and abstract whether we are declaring or not. But  In the abstract class, every method need not be public and abstract. we can take concrete method also.

Java Interface /Chapter -13 /OOPs Concept


Java Interface



  • Any service requirement specification (SRS) is considered as an "interface".

  • Inside "interface", every method is always abstract whether we are declaring or not hence, the interface is considered as 100% pure abstract class.

  • Any contract between client and service provider is considered as an "interface".


Interface declaration and implementation



  1. For each and every method present in the interface we have to provide implementation otherwise, we have to declare the class as abstract then next level child class is responsible for providing the implementation.

  2. Every method present in an interface is always public and abstract we are declaring or not hence, whenever we are implementing an interface's for every method compulsory we should declare as public otherwise we will get Compile-Time Error.


Constructor in Java /Chapter -12 /OOPs Concept

java constructorjava constructor


Whenever we are creating an object some part of the java program(code) will be executed automatically to perform initialization of the object, this part of the program is called "Constructor".
Hence the main objective of the constructor is to perform initialization of an object. 

example 1.

class Employee

{

String name;

int eid;

Employee (String name, int eid)

{

this.name = name;

this.eid = eid;

System.out.println(name+"-----"+eid);

}

public static void main(String args[ ])

{

Employee e1 = new Employee("Dhoni",101);

Employee e2 = new Employee("Virat",102);

}

}

Output of the above program is:-

In how many ways we can create Object in JAVA?/Chapter-11/OOP's Concept

In how many ways we can create Object in JAVA?In five ways we can create Object in java.




  1. By using the new operator:-In how many ways we can create Object in JAVA?




  2. By using newInstance( ) method:- In how many ways we can create Object in JAVA?




  3. By using the clone( ) method:- 

Java- Object Type Casting / Chapter - 10 / OOP's Concept

java Object Type Casting

Now that we have learned that,


We can use parent reference to hold child object.


example:-     Object  o   =  new String ("javabychetan");


We can use interface reference to hold child object implemented class object.


example:-     Runnable      r   =  new Thread ( );


let's understand the syntax of "How to do Type-Casting".

Java Object Type Casting

Before Doing type casting we have to apply or check all these three rules otherwise, we won't able to perform type casting.


Java- Polymorphism / Chapter - 9 / OOP's Concept

Java-Polymorphism

Polymorphism


One into many forms is the concept of polymorphism.


example 1: - method name is same but we can apply for different types arguments(Overloading).

public void m1(int)                  (Overloading)

public void m1(long)               (Overloading)

public void m1(float)                (Overloading)
example 2: - method signature is same but in parent class one type of implementation and in the child class another type of implementation(Overriding).

Java- Difference Between Method Overloading & Method Overriding / Chapter - 8 / OOP's Concept

Method Overloading,Method Overriding Difference between Method Overloading and Method Overriding 



  1. Method Names: - In Overloading method name should be same, And Even In Overriding method name should be same.

  2. Argument Type: - In Overloading, argument type must be different (at least the order of arguments), But in the Overriding argument type must be same.

  3. Method Signature: - In Overloading, method signature must be different, But in the Overriding method signature must be same.

  4. Return Type: - In Overloading, there is no restriction on the return type, But in Overriding return type must be same until 1.4 v, But from 1.5v onwards co-variant return type is allowed.

Java- Method Overriding / Chapter - 7 / OOP's Concept

Method OverridingMethod Overriding in Java


1.In java, whatever method parent class has that method by default available to the child class through inheritance. If the child class is not satisfied with parent class method implementation then child class is allowed to redefine that method based on its requirement, this process is known as "Overriding".


2.The parent class method which is overridden is called overridden method and the child class method which is overriding is called the overriding method.


3.In overriding method resolution is always takes care by JVM based on the runtime object hence overriding is also known as runtime polymorphism or dynamic polymorphism.


Java-Method Overloading / Chapter - 6 / OOP's Concept

Method Overloading
Method Overloading:-


Two methods are said to be overloaded method if and only if, both methods having the same name but different arguments types.
In the old language like C method overloading concept is not available hence, we can't declare multiple methods with same name but different arguments types.
But in Java, we can declare multiple methods with same name but different arguments types.Such type of methods is called the overloaded method. This overloading feature of java reduces the complexity of programming.

Java- Overloading / Chapter - 6 / OOP's Concept

Overloading:-

  1. Two methods are said to be overloaded method if and only if ,both methods having same name but different arguments types.
  2. In old language like C method overloading concept is not available hence ,we can't declare multiple methods with same name but different arguments types.
  3. But in Java , we can declare multiple methods with same name but different arguments types.Such type of methods are called overloaded method. This overloading feature of java reduces the complexity of programming.                                                                                                                                        example:

    class Test
    {
             public void m1( )
             {
                 System.out.println( "no - arguments m1() method");
             }
               public void m1( int   x)
              {
                 System.out.println( "integer - arguments m1() method");
              }
              public void m1( double y)
              {
                 System.out.println( "double - arguments  m1() method");
               }
           public static void main( String [ ] args)
            {
                      Test    t   =   new    Test( );
                       t.m1( );
                       t.m1(20 );
                       t.m1(20.5);
              }
    }

    Output is:

Java- Method Signature / Chapter -5 / OOP's Concept



   Java- Method SignatureJava Method Signature



    1. In Java method signature consists of, methods names followed by arguments types.

Method Signature Syntax

     2. Return Type is not part of the method signature in java.
     3.The compiler will use method signature to resolve method calls.

example:-

Java- Method Signature / Chapter -5 / OOP's Concept

1. In Java method signature consists of, methods names followed by arguments types.


     2. Return Type is not part of method signature in java.
    3.Compiler will use method signature to resolve method calls.

example:-
class Test
{
        public void m1(int i)
       {
             System.out.println(int  type arguments");
         }   
        public void m1(String s)
       {
             System.out.println(String type  arguments");
         }   
        public static void main(String [ ] args )
        {
               Test   t    =new  Test( );
               t.m1(10);
               t.m1("javabychetan");
        }
}

Output

Java Inheritence / Chapter -4 / OOP's Concept


Java Inheritance

Inheritance



  1. It is also known as Is-A relationship.

  2. The main advantage of inheritance is code reuse-ability.

  3. By using extends keyword we can implement Is-A relationship.


example:-

class P

{

     public void m1()

    {

        System.out.println("Parent");

     }

}

class C extends P

{

   public void m2( )

   {

       System.out.println("Child");

    }

Class Test

{

     public static void main(String [ ] args)

    {

            C c =new C ( );

            c.m1( );

            c.m2( );

      }

}

 Output is:  


Java- Inheritence / Chapter -4 / OOP's Concept



Inheritance:-

  1. It is also known as Is-A relationship.
  2. The main advantage of inheritance is code reuse-ability.
  3. By using extends keyword we can implement Is-A relationship.
example:-
class P
{
     public void m1()
    {
        System.out.println("Parent");
     }
}
class C extends P
{
   public void m2( )
   {
       System.out.println("Child");
    }
Class Test
{
     public static void main(String [ ] args)
    {
            C c =new C ( );
            c.m1( );
            c.m2( );
      }
}
 Output is:  Parent
                   Child
            
Conclusion:-
  • Whatever method parent class has by default available to the child class and hence on the child reference we can call Both Parent & Child class methods.

Java- Encapsulation / Chapter -3 / OOP's Concept



Java Encapsulation

Java Encapsulation



  1. The process of binding the data and corresponding method into a single unit is nothing but "encapsulation".                  

  2. If any component follows data hiding and abstraction such type of component is said to be encapsulated component.


Java- Encapsulation Java- Encapsulation

The main advantage of encapsulation are:-



Java- Encapsulation / Chapter -3 / OOP's Concept

  1. The process of binding the data and corresponding method into a single unit is nothing but "encapsulation".                  
  2. If any component follows data hiding and abstraction such type of component is said to be encapsulated component.

Java- Abstraction / Chapter -2 / OOP's Concept



Abstraction

Abstraction:-


Hiding internal implementation and just highlighting the set of services what we are offering, is known as "Abstraction". 
example:-
Through Bank ATM GUI screen bank people are highlighting the set of services what they are offering without highlighting internal implementation.
Abstraction


The main advantage of Abstraction are:-




Java- Abstraction / Chapter -2 / OOP's Concept

Abstraction:-

Hiding internal implementation and just highlighting the set of services what we are offering , is known as "Abstraction".
example:-
Through Bank ATM GUI screen bank people are highlighting the set of services what they are offering without highlighting internal implementation.



The main advantage of  Abstraction are:-

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com