Wednesday, 18 February 2015

Core Java ---- 4

public class Score {
       int  totalWorldcupCenturies=32;
       void cal()
       {
              System.out.println(totalWorldcupCenturies);
       }
      

}


class IndiaScore extends Score
{
       int totalWorldcupCenturies=12;
       void cal()
       {
              System.out.println("total world bats man made centuries "super.totalWorldcupCenturies);
       }
       public static void main(String[] args) {
              IndiaScore d=new IndiaScore();
              d.cal();
              System.out.println("total  Indians man made centuries " +d.totalWorldcupCenturies);
       }
      
}






public class Score {
      
       public Score() {
              super();
              // TODO Auto-generated constructor stub
       }
       public Score(int totalWorldcupCenturies) {
              super();
              this.totalWorldcupCenturies = totalWorldcupCenturies;
       }
       int  totalWorldcupCenturies=32;
       void cal()
       {
              System.out.println(totalWorldcupCenturies);
       }
      

}


class IndiaScore extends Score


{
      
       public IndiaScore() {
              super(12);
       }
       int totalWorldcupCenturies=12;
       void cal()
       {
              System.out.println("total world bats man made centuries "super.totalWorldcupCenturies);
       }
       public static void main(String[] args) {
              IndiaScore d=new IndiaScore();
              d.cal();
              System.out.println("total  Indians man made centuries " +d.totalWorldcupCenturies);
       }
      
}


// Instance Initializer Block



public class Pulsar {
       int speed;
       {
             
              speed=1200;
              System.out.println(speed);
       }
       public Pulsar() {
        System.out.println("construtor");
       }
public static void main(String[] args) {
       Pulsar p=new Pulsar();
      
}     

}


public class M {

       public M() {

System.out.println("parent class Constructor");
       }
       {
              System.out.println("parent instance iniliazer");
       }
      

}
 class B extends M {

       public B() {

System.out.println(" Child class Constructor");
       }
       {
              System.out.println(" child instance iniliazer");
       }
      
public static void main(String[] args) {
       B b1= new B();
}
}


O/P:--
parent instance iniliazer
parent class Constructor
 child instance iniliazer
 Child class Constructor

Final as variable:-

public class Pulsar {
final  int speed=9000;
       {
             
              speed=1200;
              System.out.println(speed);
       }
       public Pulsar() {
        System.out.println("construtor");
       }
public static void main(String[] args) {
       Pulsar p=new Pulsar();
      
}     

}

Final as Methd:--

public class R {
        final 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();
       }
      
      
}

Final as class : ---

public final  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();
       }
      
      
}

// constructor as final

public class M {

       public final  M() {

System.out.println("parent class Constructor");
       }
       {
              System.out.println("parent instance iniliazer");
       }
      

}
 class B extends M {

       public B() {

System.out.println(" Child class Constructor");
       }
       {
              System.out.println(" child instance iniliazer");
       }
      
public static void main(String[] args) {
       B b1= new B();
}
}



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



}

//  Upcasting
class MLA extends Politician
{

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

// static Binding


public class Calculation {
       void sum(int a, int b)
       {
              System.out.println(a+b);
       }
      
       void sum(int a, int b,int c)
       {
              System.out.println(a+b+c);
       }
       public static void main(String[] args) {
              Calculation c=new Calculation();
              c.sum(12, 80);
              c.sum(12, 123, 89);
       }
}


Dynamic Binding :-


public class Score {
      
       public Score() {
              super();
              // TODO Auto-generated constructor stub
       }
       public Score(int totalWorldcupCenturies) {
              super();
              this.totalWorldcupCenturies = totalWorldcupCenturies;
       }
       int  totalWorldcupCenturies=32;
       void cal()
       {
              System.out.println(totalWorldcupCenturies);
       }
      

}


class IndiaScore extends Score


{
      
       public IndiaScore() {
              super(12);
       }
       int totalWorldcupCenturies=12;
       void cal()
       {
              System.out.println("total world bats man made centuries "super.totalWorldcupCenturies);
       }
       public static void main(String[] args) {
               Score d=new IndiaScore();   // Dynamic Polymorpism// Dynamic Binding
              d.cal();
              System.out.println("total  Indians man made centuries " +d.totalWorldcupCenturies);
       }
      
}



// Instance of operator in  java


public class Template2 {
       String type;
       public static void main(String[] args) {
             
              Template2 t=new Template2();
              if (t instanceof Template2)
              {
               System.out.println("true");
              }
              else
              {
                     System.out.println("false");
              }
             
       }

}


// Abstract  Program

public  abstract class Bike {
       int speed;
       static int y=12;
       final int k=89;
      
       public Bike(int speed) {
             
              this.speed = speed;
       }
       abstract void ride();
       void m1()
      
       {
              System.out.println("hi");
       }
       abstract void m2();
      

}
class  Honda extends Bike
{

      

       public Honda(int speed) {
              super(speed);
              // TODO Auto-generated constructor stub
       }

       @Override
       void ride() {
              System.out.println("  Honda riding" + speed);
             
       }

       @Override
       void m2() {
              System.out.println("implementing m2");
             
       }
       public static void main(String[] args) {
              Bike b=new Honda(123);
              b.ride();
              b.m1();
              b.m2();
       }
      
}


//Interface Programs 1


public interface Printable {
       void print ();
      

}


class  Testing implements Printable
{
       public static void main(String[] args) {
              Testing t=new Testing();
              t.print();
             
       }

       @Override
       public void print() {
              System.out.println("hi i am printing");
             
       }
}



// Multiple Ineritance in interfaces
public interface Printable {
       void print ();
      

}

 interface  Showble {
       void show ();
      

}

class  Testing implements Printable ,Showble
{
       public static void main(String[] args) {
              Testing t=new Testing();
              t.print();
             
       }


       @Override

No comments:

Post a Comment