Friday, 9 October 2015

Method overloading example in java (dynamic polymorphism)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
obj.show();

}

}


public class Second {
int i,k;
float j;
void show(){
System.out.println("No argument method");
}
void show(int k){
this.k=k;
System.out.println("The value of k is " +k);
}
void show(int i,float j){
this.j=j;
this.i=i;
System.out.println("The value of i,j is " +i +j);
}
}

output:-
No argument method

Dynamic overriding example in java (dynamic polymorphism)


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
Third  obj1 = new Third();
obj.show();
obj1.show();
}
}



public class Second {

void show(){
System.out.println("Parent");

}

}

class Third extends Second{
void show(){


System.out.println("Child");

}


}

Output:-
Parent
Child

Thursday, 8 October 2015

"Super" keyword usage example in Java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Third obj=new Third();
}

}



public class Second {
int i;
Second(int i){
this.i=i;
System.out.println("This is super class param constructor");
}
}



class Third extends Second{
Third(){
super(20);
System.out.println("The value of super calss variable i is " +super.i);
}
}

output:-
This is super class param constructor
The value of super calss variable i is 20

"This" keyword usage in Java


public class Acceptt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Third obj = new Third(10,20);
  obj.show();
  }

}



class Second {

 void show(){
 
  System.out.println("I am Parent");
 }

}


class Third extends Second{
 int i,j;

 void show(){

  System.out.println("I am child");
 }
 Third(){
  System.out.println("Third constructor");
 }

 Third(int i,int j){
  this();
  this.i=i;
  this.j=j;
  System.out.println("The values are "+i  +j);
  }

 void display(){
  this.show();
 }

}


output:-
Third constructor
The values are 1020
I am child

Java Inheritance example


public class Inheritance {

public static void main(String[] args) {
// TODO Auto-generated method stub
Second obj = new Second();
First obj1 = new First();
obj.setage(10);
obj1.setage(20);
System.out.println("The value is " +obj.i);
System.out.println("The value is " +obj1.i);
}

}


public class First {
int i;
String S;
void setage(int i){
this.i=i;
}
void setname (String S){
this.S=S;
}
int getage(){
return i;
}
String getname(){
return S;
}
}

class Second extends First{
int j;
void setrollno(int i){
j=i;
}
}

output:-
Test one
Test Two

Java Program Example to demonstrtae First parent constructor is called followed by chiled class constructor

/**
 *
 */

/**
 *
 *
 */
public class Second {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
First obj = new First();
}

}


public class Demo {
Demo(){
System.out.println("Test one");
}
}

class First extends Demo{
First(){
System.out.println("Test Two");
}
}

Output:-
Test one
Test Two

Java Inheritance example Program


public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub

SubClass obj = new SubClass();

obj.display();

}

}




public class First {
protected String str;
First() {
str = "Java ";
}
}
 
class SubClass extends First {
SubClass() {
str = str.concat("Is My World ...!!!");
}
void display()
{
System.out.println(str);
}
}

output:-
Java Is My World ...!!!