Friday 11 December 2015

Inheritance In Easy Way

What is Inheritance

Lets Discuss Today,What inheritance really is...

Inheritance means inheriting properties from parent like A son inherites properties of his father....

But In Java programming ......

A class inherites properties of its parent class

So we can say "Inheritance is process in which one object acquires all the properties and behaviours of its parent class"

Inheritance represents IS-A relationship..

Note -static members can not be inheritaed in child class because static member always stick with its class.All the static member gets memory at loading time And Inheritance is a runtime phenomina.So it can not inheritaed ..

extends and implements  are  keywords in java which are used to inherit a class and interface respectivly

There Are Two Main Reason Why We Use Inheritance In Java

1- For Method Overriding (Runtime polymorphism)
2- For Code reusability

Type of Inheritance 

1-Single Inheritance  

2-multilevel Inheritance

3-Hierarchical Inheritance








4-multiple Inheritance (It is not supported by java)

5-Hybrid Inheritance



Q-> Why Java does not support multiple Inheritance ?

Ans- Multiple Inheritance means One class is  inheriting(extending) two class.So It may  be possible that Both classes have same variable .Thus at the time of accessing that variable ,there will be ambiguity to call variable

Method Overriding 

Overriding means when we rewrite a method again for its improvement  
If subclass (child class) has the same method as declared in the parent class, it is known as 
Method Overriding"

Rule 1-

Methods must have the same name And same parameter
Rule-2

Access Modifier of child class method must be same OR wider than parent class method

1-Single Inheritance

class A{
public void operation(int a,int b){
System.out.println(a+b);
}
}
public class B extends A{

public void operation(int a,int b){
System.out.println(a*b);
}
public static void main(String arg[]){

A a=new B();
a.operation(3,4);
}
}

output is

12

multilevel inheritance

class A{
public void operation(int a,int b){
System.out.println(a+b);
}
}
class B extends A{

public void operation(int a,int b){
System.out.println(a*b);
}
}
public class C extends B{
public void operation(int a,int b){
System.out.println("This is class C ");
System.out.println(a-b);
}
public static void main(String arg[]){
C c=new C();
c.operation(4,3);
}

output is

1

Wednesday 9 December 2015

Difference Between Interface And Abstract class


What is Abstraction

Abstraction is a process of hiding the Complexity and showing only the functionality.

For Example

The Internal details of mobile Or TV remote is hide and we have only the buttons to operate the TV or Mobile
So we know what buttons do but do not know how it does ...

Advantages

We can increase security
------------------------------------------------------------------------------------------------------------

What is abstrct class

1) A class that is declared as abstract is known as abstract class.
2) A Abstract class can not be instantiated Because constractors are not inherites in subclass.

Example Of abstract class




In Following example, Company is the abstract class that contains only one abstract method empSal. 
It implementation is provided by the Employee class



abstract class Company{
abstract void empSal(); 



class Employee extends Company{ 
void empSal(){System.out.println("high salary");} 
public static void main(String args[]){ 
Company c = new Employee(); 
c.empSal(); 





Output is:
high salary

What is Interface....

An interface is a specification of method prototype 


It is blueprint of class .

The interface is a mechanism to achieve the 100% Abstraction in java


Interface is written when all features are implemented differently in different object 


Interface fields are public, static and final by default


methods are public and abstract.

------------------------------------------------------------------------------------------------------------


abstract class Vs interface

                                                               1
If we do not know anything about implementation just we have required specification Then go with interface

If we  are talking about implementation but not completly(partial implementation) THEN we should go for abstract class
                                                                2
Methoda are public and abstract means 100% anstraction

Methods are abstract and concrete both
                                                               3
We can not declare interface method as private ,protected,final static nativ syncronized

There is no restriction abstract class method modifier
                                                              4
Variables are public static and final

No need to be public ,static and final in abstract class
                                                             5
Inside interface we can not declare instance and static blocks otherwise we will get complie time error

We can declare instance and static block inside abstract class 

Anatomy Of Multithreading In Java

What is MultiThreading 

Multithreading is a way by which one single program can perform two or more task simultaneously 
Multi means more than one 
Thread means smallest unit of processing Or Seperate path of execution 
So Multithreading means one or more than one thread is/are executing simultaneously.....

How can we create a thread in java


There is two ways of creating a thread in java..

1- Using Thread classs

2 -Using runnable interface

So we will do each one by one
_________________________________________________________________________________

1-Using Thread class

Using This method ,we have to extends the thread class.....And we know when we extending any class ,our class has become of parent type class..
So Lets create a Thread..

--------------------------------------------------------------------------------------------------------------------------
public class City extends Thread{

public void run(){
System.out.println("my current city is delhi");
                             }
public static void main(String arg[]){

City c=new City();
c.start();
}
}

--------------------------------------------------------------------------------------------------------------------------
So Lets Discuss Each line
In 1st line we are making a class(City) and extends Thread class.So our class also become Thread
In 2nd line we are writing a method called run and write something in body of this method.
run() method is Present in runnable interface and Thread class has implemented that run method So
Here we are overriding that run method

In main Method ,we are creating an object of City class and calling the run() method...............
Lets See What is happening behind the scene....
When we create object of City class ,The Default constractor of City class is called and Inside the of constractor JVM  placed the super  keyword and call the paarent class's(Thread) constractor
Thus Our City class object Became Thread Type Object...

strat() is method of thread class.Itstarts the Thread means it put thread in Runnable State
_________________________________________________________________________________

_________________________________________________________________________________

2-Using runnable interface

Using runnable interface,we can create a thread....
For this we have to implement the runnable interface..runnable interface has only one method called run().
Lets start.....
-------------------------------------------------------------------------------------------------------------------------
public class City implements runnable{

public void run(){

System.out.println("Delhi is  The capital of india");
                           }
public static void main(String arg[]){
Thread t=new Thread();
City ct=new City(t);
ct.start();

}

--------------------------------------------------------------------------------------------------------------------------
Here we are creating a object of Threat class and pass it as parameter in our City class Because Thread class has a contractor which takes parameter of Thread Type




Lifecycle of a thread

Lifecycle of a thread have five states 

New

It is very first state when object of  class is created

Runnable

It is the second state When we invoked the start() method

Non-Runnable(Blocked)

It is the third state.When we call methods like sleep(),wait(),suspend() on running thread

Running

When Thread class executing its run method

Terminated

when run() method exits means execution of run method got over