Wednesday, 18 February 2015

Core Java --3

//Constructor Over Loading
public class Template {
      
       int tid;
       String tname;
       String creator;


       public Template(int tid, String tname, String creator) {
             
              this.tid = tid;
              this.tname = tname;
              this.creator = creator;
              System.out.println(tid);
              System.out.println(creator);
              System.out.println(tname);
       }
       public static void main(String[] args) {
              Template t1=new Template(12,"SMS");
              Template t2=new Template(13, "Email" , "Sravan");
       }
       public Template(int tid, String tname) {
              super();
              this.tid = tid;
              this.tname = tname;
             
              System.out.println(tid);
              System.out.println(tname);
       }

}
//copy Constructor
class Student6{  
1.        int id;  
2.        String name;  
3.        Student6(int i,String n){  
4.        id = i;  
5.        name = n;  
6.        }  
7.          
8.        Student6(Student6 s){  
9.        id = s.id;  
10.     name =s.name;  
11.     }  
12.     void display(){System.out.println(id+" "+name);}  
13.    
14.     public static void main(String args[]){  
15.     Student6 s1 = new Student6(111,"Karan");  
16.     Student6 s2 = new Student6(s1);  
17.     s1.display();  
18.     s2.display();  
19.    }  
20. }  



//static Varibles

public class Keyword {

       int kid;
static        String kname;
public static void main(String[] args) {
       System.out.println(kname);
      
}
      
}





// static Demo


public class Keyword {

       int kid;
       int index;
public Keyword(int kid, int index) {
              super();
              this.kid = kid;
              this.index = index;
       }
static        String kname;
public static void main(String[] args) {
       Keyword k=new Keyword(121, 1);
       Keyword k1=new Keyword(191, 1);
       System.out.println(kname);
       System.out.println(k.kid);
       System.out.println(k.kname);
       System.out.println(k.index);
      
       System.out.println(k1.kid);
       System.out.println(k1.kname);
       System.out.println(k1.index);
      
}
      
}
o/p:--



null
121
null
1
191
null
1



// static metod
public class Recursion {
static int fact (int num)
{
       if(num==1)
       {
              return 1;
             
       }
       else
             
       {
              return num *=fact(num-1);
       }
}
public static void main(String[] args) {
        int d=fact (5);
        System.out.println(d);
      
}
}


// static block and  static Method
public class Demoo12 {
       static int k;
       static
       {
              k=12;
              System.out.println(k);
              System.out.println("static block");
       }
       public static void main(String[] args) {
              System.out.println("main ");
       }

}


//static Block Demo
public class Demoo12 {
       static int k;
       static
       {
              k=12;
              System.out.println(k);
              System.out.println("static block");
             
              int a=12;
              int b=13;
              int c=a+b;
        System.out.println(c);
        System.exit(0);
       }



}


// Program On static



public class Demo13 {
       int id;
static        String name;
      
       public static void main(String[] args) {
              Demo13 d =null;
              System.out.println(d.name);
              System.out.println(d.id);
             
             
       }

}



// this key word

public class Template {
      
       int tid;
       String tname;
       String creator;


       public Template(int tid, String tname, String creator) {
             
              this.tid = tid;
              this.tname = tname;
              this.creator = creator;
              System.out.println(tid);
              System.out.println(creator);
              System.out.println(tname);
       }
       public static void main(String[] args) {
              Template t1=new Template(12,"SMS");
              Template t2=new Template(13, "Email" , "Sravan");
       }
       public Template(int tid, String tname) {
              super();
              this.tid = tid;
              this.tname = tname;
             
              System.out.println(tid);
              System.out.println(tname);
       }

}








//  this() to invoke  the constructor
                                                                                                                                                                                                                                                                                                                                                                                              
public class Template {
      
       int tid;
       String tname;
       String creator;


       public Template(int tid, String tname, String creator) {
             

    this(tid, tname);
              this.creator = creator;
              System.out.println(tid);
              System.out.println(creator);
              System.out.println(tname);
       }
       public static void main(String[] args) {
              Template t1=new Template(12,"SMS");
              Template t2=new Template(13, "Email" , "Sravan");
       }
       public Template(int tid, String tname) {
              super();
              this.tid = tid;
              this.tname = tname;
             
              System.out.println(tid);
              System.out.println(tname);
       }

}


// this() to call  the method
public class Template {
      
       int tid;
       String tname;
       String creator;


       public Template(int tid, String tname, String creator) {
             

    this(tid, tname);
              this.creator = creator;
              System.out.println(tid);
              System.out.println(creator);
              System.out.println(tname);
       }
       public static void main(String[] args) {
              Template t1=new Template(12,"SMS");
              Template t2=new Template(13, "Email" , "Sravan");
       }
      
       void m1()
       {
              System.out.println("m1 method");
       }
       public Template(int tid, String tname) {
       this.m1();
              this.tid = tid;
              this.tname = tname;
             
              System.out.println(tid);
              System.out.println(tname);
       }

}

O/P: ---



//  this retrns an  obect of a class


public class Demo1 {
       Demo1 get()
       {
              return this;
       }
       void m1()
       {
              System.out.println("in m1 method ");
       }
       public static void main(String[] args) {
              new Demo1().get().m1();
       }
      
      

}




//this as Method argument
public class Demo15 {

       Demo15 d;

       void  Demo(Demo15 d) {
              System.out.println("inside Demo");
      
       }
      
       void m1()
       {
              Demo(this);
       }
      
}



// this Program

public class D {
       void m1()
      
       {
              System.out.println(this);
       }
       public static void main(String[] args) {
              System.out.println("hi");
              D d=new D();
              System.out.println(d);
             
       }

}

O/p:--
hi
D@1a46e30
D@1a46e30

// Ineritance

public class Politician {
       int  governmentsal=100000;
       String services;
      

}
class MLA extends Politician
{

       int corruptio=600000;
       public static void main(String[] args) {
              MLA mla=new MLA();
              System.out.println(mla.corruptio);
System.out.println(mla.governmentsal);         
       }
}







// Method Overiding
public class BattingStyle {
       void  playingstyle()
       {
              System.out.println(" All Batsmens are not aggressive ");
       }
      
      

}
class IndianPlayersStyle extends BattingStyle
{
       void  playingstyle()
       {
              System.out.println("indian players are aggeresive");
       }
       public static void main(String[] args) {
              IndianPlayersStyle ind=new IndianPlayersStyle();
              ind.playingstyle();
       }
}



// Covarient return Type



public class R {
       R get ()
       {
              return this;
             
       }
      

}
class T extends R
{
       T get()
       {
              return this;
       }
      
       void m1()
       {
              System.out.println("hi");
       }
       public static void main(String[] args) {
              T t=new T();
              t. get().m1();
       }
      
      

}

No comments:

Post a Comment