I hope this is the right time to say something about Java, it is a higher level language, java platform lies above the software platform(OS), a special type of virtual machine needed to execute java code, it is named as JVM (Java Virtual Machine). This JVM only understands bytecode, where to get such byte code..?

Software development in Java follows steps as below,

1) Install required JDK (Java Development Kit)

2) Set CLASSPATH

3) Start programming,

4) Execution,

a) convert the source code into bytecodes (understandable form of JVM),

$ javac src.java 

b) using interpreter provide bytecode into JVM, and get the output

$ java src 

I am using linux debian, I told you all what I followed.. I welcome your comments..

Now I will share some thing about Java,

Polymorphism in java

Polymorphism – Same thing with different forms…

        a) Static Polymorphism

It is compile time polymorphism,

Happens by method overloading,

// DemoPolymorphism.java

 public class DemoPolymorphism{

public int add(int a,int b){

return a+b;

}

public int add(int a,double b){

return a+(int)b;

}

public int add(double a,int b){

return (int)a+b;

}

public int add(double a,double b){

return (int)(a+b);

}

}

// methodDemo.java

public class methodDemo{

public static void main(String[] args){

DemoPolymorphism aa = new DemoPolymorphism();

System.out.println(aa.add(4,4));

System.out.println(aa.add(4.3,3));

System.out.println(aa.add(3,4.4));

System.out.println(aa.add(3.3,4.5));

}

}

        b) Dynamic Polymorphism

It is Runtime polymorphism

Happens by method overriding

// DynamicDemo.java

class Demo{

public void move(){

System.out.println("A Vehicle can move...");

}

}

class child extends Demo{

public void move(){

System.out.println("Jack and jill went upto hill");

}

}

public class dynamicDemo{

public static void main(String[] args){

Demo aa = new child();    // reference type is Demo and object being referenced to child class

aa.move();     // prints "Jack and jill went upto hill"

Demo bb = new Demo();  // reference type is Demo and object being referenced to Demo class

bb.move();   // prints "A Vehicle can move..."

}

}

Inheritance

I hope every new comer to java might struggle under standing this important concepts of java.

I hope nothing to say more about Inheritance, just see yourself you inherit your parents, like wise a class can inherit another class, results as parent and child class relationship.

public class inheritanceDemo{

public static void main(String[] args){

Child obj1 = new Child();

obj1.display();

obj1.printIt();

Parent obj2 = new Child();

obj2.printIt();

//obj2.display();

}

}

class Parent{

public void printIt(){

System.out.println("Myself Parent Class");

}

}

class Child extends Parent{

public void display(){

System.out.println("Myself Child Class");

}

}

What if I add this line

obj2.display();

just below

obj2.printIt();

.

It will throw error, since obj2 of type Parent is not aware of method ‘display()’ specific to class Child. To overcome this, we need cast object as below,

Child obj3 = (Child)obj2;

obj3.display();

And finally I can make my code lovable,

public class inheritanceDemo{

public static void main(String[] args){

Child obj1 = new Child();

obj1.display();

obj1.printIt();

Parent obj2 = new Child();

obj2.printIt();

Child obj3 = (Child)obj2;

obj3.display();

}

}

class Parent{

public void printIt(){

System.out.println("Myself Parent Class");

}

}

class Child extends Parent{

public void display(){

System.out.println("Myself Child Class");

}

}

Compile and see the output…

Myself Child Class

Myself Parent Class

Myself Parent Class

Myself Child Class

Constractor Chaining

//    classA.java

public class classA{

public classA(){

System.out.println("Jack and jill went upto hill");

}

}

//     classB.java

public class classB extends classA{

public classB(){

System.out.println("To fetch a pail of water");

}

}

//    demo.java

public class demo{

public static void main(String[] args){

new classB();        // equivalent to classB bb = new classB;

}

}

And the output is

Jack and jill went upto hill

To fetch a pail of water

Interface

Interface holds abstract classes, which can be implemented by any class for usage,

//        interfaceClass.java

public interface interfaceClass{

public void hello();
}

//        mainDemo.java

public class mainDemo{
 public static void main(String[] args){
 interfaceClass i = new dummy();

i.hello();
// i.anotherMethod(); it is not possible since type of 'i' instance(object) is interface it cannot read other methods of class(which implements interfaceClass)
 dummy j = new dummy(); // it is possible
 j.anotherMethod();
 }
}
class dummy implements interfaceClass{
 public void hello(){
 System.out.println("jack and jill went upto hill");
 }

public void anotherMethod(){
 System.out.println("I am extra method at dummy class");
 }
}

Local and Anonymous Class demo

//        helloWorldClass.java

public class helloWorldClass{

//     interface
 interface helloInterface{
 public void greet();
 public void greetSomeOne(String someOne);
 }

//     method

public void display(){

//     local class
 class helloLocalClass implements helloInterface{
 String someOne = "world";

public void greet(){
 System.out.println("Hello LocalClass greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 }

//    Creating instance of interface with local class object

helloInterface obj1 = new helloLocalClass();

//    Anonymous class declaration

helloInterface obj2 = new helloInterface(){
 String someOne = "world";

public void greet(){
 System.out.println("French Greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 };

//    Another Anonymous class

helloInterface obj3 = new helloInterface(){
 String someOne = "world";

public void greet(){
 System.out.println("German Greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 };

 obj1.greet();
 obj2.greetSomeOne("Audhil");
 obj3.greet();
 }

//    main Method to show demo

public static void main(String[] args){
 helloWorldClass Obj = new helloWorldClass();

Obj.display();
 }
}

I welcome your comments here…

Enjoy…!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s