Variable and TypeCasting  Related Questions

  1. What are the default value of these variable?
    Object reference, byte, short,int, boolean,char
    Object reference null (not referencing any object)
    byte, short, int, long 0
    float, double 0.0
    boolean false
    char '\u0000'
  2.  what is upcasting and downcasting in java ?
    Upcasting means An object of sub class can be refferered by object of super class
    like
    int x=9;
    float y=x;
    int x value will automatically asign to y value and compile perfectly.
    downcasting:
    Downcasting means opposite is not true, mean object of superclass  can not  reffered to subclass, we need to do typecast.
        int x=9;
        byte b=x;
    It will give compile time error. We have to write like this
    byte b=(byte)x;
  3. What will be the output for following program?
    public class TypeCast {
        public static void main(String [] args) {
            long l = 129L;
            byte b = (byte)l;
            System.out.println("The byte is " + b);
            }
    }
    Ans The byte is -127
    byte range is -128 to 127. After 127 it'll calculate  form -128
    These are two classes
    public class A  {
        public void testA(){
            System.out.println("class A");
        }

    }
    public class B extends A{
    public void testB(){
        System.out.println("Class B");
    }
    }
    What will be output for following questions
  4. What is out put of this one
    public class Test {
        public static void main(String args[]){
            A a=new A();
            B b= (B) a;
            b.testA();
        
        }

    }
    Ans run time error. A can not cast to B. Super class object can not cast to sub class.
  5. In the above example can we write like this
    public class Test {
        public static void main(String args[]){
            B b= new B();
            A a =(A)b;
            a.testA();
    }
    }
    Ans yes out put will "Class A"
  6. In the above example can I write like this
    public class Test {
        public static void main(String args[]){
             A a =new B();
            a.testA();
                }}
    yes, Super class reference can point to subclass Object.
  7. In the above example can I write like this
    public class Test {
        public static void main(String args[]){
                   
            B b = new A();
            b.testB();
           
        }

    }
    no
  8. In the above example can I write like this?

    public class Test {
        public static void main(String args[]){
                   
            A a= new B();
            a.testB();
           
        }

    }
    no
  9. Can I write like this?
    public class Test {
        public static void main(String args[]){
                   
            B b= (B) new A();
            b.testB();
           
        }

    }
    no, run time error
  10. There are three classes A,B,C like this
    public class A  {
        public void testA(){
            System.out.println("class A");
        }

    }
    public class B extends A{
    public void testB(){
        System.out.println("Class B");
    }
    }
    public class C extends B{
    public void tectC(){
        System.out.println("class c");
    }
    }
    In this blelow class which type cast is wrong and which one is currect?
    public class Test {
        public static void main(String args[]){
                   
            A a = new A();
            B b= new B();
            C c=new C();
            c=(C)a;
            c=a;
            c=(C)b;
            c=b
            b=(B)a;
            b=a;
            b=(C)c;
            b=c;
            a=(C)c;
            a=c;
            a=(B)b;
            a=b;
               
        }

    }

    Ans
    public class Test {
        public static void main(String args[]){

            A a = new A();
            B b= new B();
            C c=new C();
            c=(C)a;//w
            c=a;//w
            c=(C)b;//w
            c=b;w
            b=(B)a;//w
            b=a;//w
            b=(C)c;//c
            b=c;//c
            a=(C)c;//c
            a=c;//c
            a=(B)b;//c
            a=b; //c
            c.testA();

        }

    }
  11. There are four classes A,B,C,D. B extends A and D extends C. Like this
    public class A  {
        public void testA(){
            System.out.println("class A");
        }

    }
    public class B extends A{
    public void testB(){
        System.out.println("Class B");
    }
    }

    public class C {
    public void tectC(){
        System.out.println("class c");
    }
    }

    public class D extends C {
        public void testD(){
            System.out.println("test D");
        }

    }

    Can we typecast like below?
    public class Test {
    public static void main(String ar[]){
    A a=new A();
    B b = new B();
    C c =new C();
    D d =new D();
    Object o=(A)a;
    c=(C)o;
    c.tectC();
    }
    }
    no, its run time error. Two sibling can't be casted