Friday, February 12, 2016

StringExample1

package StringInJava;

public class StringExample1 {

/**
Q.what is string

1. String is a class.
2. String represents set of characters.
3. String is immutable i.e. it cannot be changed , It is final

-----------------------------------------------------------
Q.How to create String object

     There are two ways to create String object:
     1. By string literal
     2. By new keyword
------------------------------------------------------------    
     String literal is created by using double quotes
      e.g String t = "Bhanu"; ( In constant pool)
      e.g String t1 = "Bhanu";
     To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
------------------------------------------------------------    
     By new keyword
      String t1 = new String("Bhanu"); ( In non constant pool, i.e heap area)

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

}

}

Monday, February 1, 2016

MethodOverRiddingExample7

package methodOverridding;

public class MethodOverRiddingExample7 extends MethodOverRiddingExample6{

public int add(){
System.out.println(" I am from child class");
return 5;
}

public boolean add1(int a){
System.out.println(" I am from child add1");

}

/**
* for method overridding both child class and parent class method return type, method name
* and method argument should be same
*/

}

MethodOverRiddingExample6

package methodOverridding;

public class MethodOverRiddingExample6 {

public void add(){
System.out.println(" I am from parent class");
}

public void add1(int a){
System.out.println(" I am from parent add1");
}

}

MethodOverRiddingExample5

package methodOverridding;

public class MethodOverRiddingExample5 extends MethodOverRiddingExample4{

public void sum(int a, int b) {
int c = a + b;
System.out.println("sum is from child class with two argument:-" + c);
}

public void sum(int a, int b, int c) {
int d = a + b + c;
System.out.println("sum is from child class with three argument:-" + d);
}


void test4(){
System.out.println(" i am from child class");
}

   public static void main(String[] args) {

  MethodOverRiddingExample4 obj = new MethodOverRiddingExample5();
  /**
   * if you will make reference of parent class, then we can access only parent
   * class members through reference. at compile time
   */
 
  /**
* At run time method will be called based on the class for which we have
* created object, if method is not available in child class then it will
* check in parent class.
*/
 
  obj.sum(4, 5);
  obj.sum(4, 5, 6);
 
  obj.mutiply(5, 6);
 
 
 
  MethodOverRiddingExample5 obj1 = new MethodOverRiddingExample5();
 
  obj1.sum(2, 3);
  obj1.sum(2, 3, 7);
  obj.mutiply(7, 9);
  obj1.test4();
 
  /**
   * if you will make reference of child class, then we can access  parent
   * class members as well child class members at compile time
   */
 
 
   }
}

MethodOverRiddingExample4

package methodOverridding;

public class MethodOverRiddingExample4 {

public void sum(int a, int b){
int c = a+b;
System.out.println("sum is from parent with two argument:-"+c);
}

public void sum(int a, int b, int c){
int d = a+b+c;
System.out.println("sum is from parent with three argument:-"+d);
}

public void mutiply(int a, int b){
int d = a*b;
System.out.println("multiply is from parent:-"+d);
}

}

MethodOverridingExample3

package methodOverridding;

public class MethodOverridingExample3 extends MethodOverRiddingExampe1{

/*public void test1(){
System.out.println("I am from child  class");
}*/

public static void main(String[] args) {

MethodOverRiddingExampe1 obj = new MethodOverridingExample3();

obj.test1();

/**
* At run time method will be called based on the class for which we have
* created object.
*/
}

}

MethodOverRiddingExample2

package methodOverridding;

public class MethodOverRiddingExample2 extends MethodOverRiddingExampe1{

public void test1(){
System.out.println("I am from child  class");
}

public static void main(String[] args) {

MethodOverRiddingExample2 obj = new MethodOverRiddingExample2();

obj.test1();

/**
* At run time method will be called based on the class for which we have
* created object.
*/


}

}