Sunday, 12 April 2015

Abstract class and Interface


Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.
But there are many differences between abstract class and interface that are given below.
Abstract classInterface
1) Abstract class can have abstract and non-abstractmethods.Interface can have only abstract methods.
2) Abstract class doesn't support multiple inheritance.Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.Interface has only static and final variables.
4) Abstract class can have static methods, main method and constructor.Interface can't have static methods, main method or constructor.
5) Abstract class can provide the implementation of interface.Interface can't provide the implementation of abstract class.
6) The abstract keyword is used to declare abstract class.The interface keyword is used to declare interface.
7) Example:
public class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

Example of abstract class and interface in Java

Let's see a simple example where we are using interface and abstract class both.
  1. //Creating interface that has 4 methods  
  2. interface A{  
  3. void a();//bydefault, public and abstract  
  4. void b();  
  5. void c();  
  6. void d();  
  7. }  
  8.   
  9. //Creating abstract class that provides the implementation of one method of A interface  
  10. abstract class B implements A{  
  11. public void c(){System.out.println("I am C");}  
  12. }  
  13.   
  14. //Creating subclass of abstract class, now we need to provide the implementation of rest of the methods  
  15. class M extends B{  
  16. public void a(){System.out.println("I am a");}  
  17. public void b(){System.out.println("I am b");}  
  18. public void d(){System.out.println("I am d");}  
  19. }  
  20.   
  21. //Creating a test class that calls the methods of A interface  
  22. class Test5{  
  23. public static void main(String args[]){  
  24. A a=new M();  
  25. a.a();  
  26. a.b();  
  27. a.c();  
  28. a.d();  
  29. }}  
Output:
       I am a
       I am b
       I am c
       I am d

Multithreading in Java


Multithreading in java is a process of executing multiple threads simultaneously.
Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.
But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.
Java Multithreading is mostly used in games, animation etc.

Advantage of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform multiple operations at same time.
2) You can perform many operations together so it saves time.
3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.

Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:
  • Process-based Multitasking(Multiprocessing)
  • Thread-based Multitasking(Multithreading)

1) Process-based Multitasking (Multiprocessing)

  • Each process have its own address in memory i.e. each process allocates separate memory area.
  • Process is heavyweight.
  • Cost of communication between the process is high.
  • Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.

2) Thread-based Multitasking (Multithreading)

  • Threads share the same address space.
  • Thread is lightweight.
  • Cost of communication between the thread is low.

Note: At least one process is required for each thread.

What is Thread in java

A thread is a lightweight sub process, a smallest unit of processing. It is a separate path of execution.
Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area.
what is thread in java
As shown in the above figure, thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.

Note: At a time one thread is executed only.

Do You Know
  • How to perform two tasks by two threads ?
  • How to perform multithreading by annonymous class ?
  • What is the Thread Schedular and what is the difference between preemptive scheduling and time slicing ?
  • What happens if we start a thread twice ?
  • What happens if we call the run() method instead of start() method ?
  • What is the purpose of join method ?
  • Why JVM terminates the daemon thread if there is no user threads remaining ?
  • What is the shutdown hook?
  • What is garbage collection ?
  • What is the purpose of finalize() method ?
  • What does gc() method ?
  • What is synchronization and why use synchronization ?
  • What is the difference between synchronized method and synchronized block ?
  • What are the two ways to perform static synchronization ?
  • What is deadlock and when it can occur ?
  • What is interthread-communication or cooperation ?
What we will learn in Multithreading
  • Multithreading
  • Life Cycle of a Thread
  • Two ways to create a Thread
  • How to perform multiple tasks by multiple threads
  • Thread Schedular
  • Sleeping a thread
  • Can we start a thread twice ?
  • What happens if we call the run() method instead of start() method ?
  • Joining a thread
  • Naming a thread
  • Priority of a thread
  • Daemon Thread
  • ShutdownHook
  • Garbage collection
  • Synchronization with synchronized method
  • Synchronized block
  • Static synchronization
  • Deadlock
  • Inter-thread communication