Java Training in Chennai – Basics – Learning – Part – IV

Java Training in Chennai – Basics – Learning – Part – IV 

This blog explains about Java Training in Chennai – Basics – Learning – Part – IV. Some of them are discussed below .

     We have already about some programming in the previous blogs , Java Training in Chennai – Basics – Learning – Part – I , II & III

_______________________________________________________________________

                Chapter 7: Inheritance

Question 1:

class Base {

    public void show() {

       System.out.println(“Base::show() called”);

    }

}

 

class Derived extends Base {

    public void show() {

       System.out.println(“Derived::show() called”);

    }

}

 

public class Main {

    public static void main(String[] args) {

        Base b = new Derived();;

        b.show();

    }

}

 

    final public void show() {

       System.out.println(“Base::show() called”);

    }

}

 

Question 2:

class Base {

    final public void show() {

       System.out.println(“Base::show() called”);

    }

}

 

class Derived extends Base {

    public void show() {

       System.out.println(“Derived::show() called”);

    }

}

 

class Main {

    public static void main(String[] args) {

        Base b = new Derived();;

        b.show();

    }

}

Question 3:

class Base {

    public static void show() {

       System.out.println(“Base::show() called”);

    }

}

 

class Derived extends Base {

    public static void show() {

       System.out.println(“Derived::show() called”);

    }

}

 

class Main {

    public static void main(String[] args) {

        Base b = new Derived();;

        b.show();

    }

}

Question 4:

class A

{

    int  b = 50;

}

 class B extends A

{

    int b = 20;

}

 

public class MainClass

{

    public static void main(String[] args)

    {

        A a = new B();

        System.out.println(a.b);

    }

}

Question 5:

class Base{

        public Base(){

                System.out.print(“Base”);

        }

}

public class Derived extends Base{

        public Derived(){

                this(“Examveda”);

                System.out.print(“Derived”);

        }

        public Derived(String s){

                System.out.print(s);

        }

        public static void main(String[] args){

                new Derived();

        }

}

 

Question 6:

abstract class C1{

                public C1(){

                                System.out.print(1);

                }

}

class C2 extends C1{

                public C2(){

                                System.out.print(2);

                }

}

class C3 extends C2{

                public C3(){

                                System.out.println(3);

                }

}

public class Test{

                public static void main(String[] a){

                                new C3();

                }

}

 

 

public class Test

{

    public static void main(String[] args)

    {

      int i = 100;      

      long l = i;           

      float f = l;           

      System.out.println(“Int value “+i);

      System.out.println(“Long value “+l);

      System.out.println(“Float value “+f);

    }

}

 

public class Test

{

    public static void main(String[] args)

    {

      double d = 100.04; 

      long l = (long)d;   

      int i = (int)l;    

      System.out.println(“Double value “+d);

      System.out.println(“Long value “+l);

      System.out.println(“Int value “+i);

}

}

 

class IntToByteConversion

{

    public static void main(String arg[])

    {

        int a = 350;

        byte b;

        b = (byte) a;

        System.out.println(“b = ” + b );

    }

}

class DatatypeCasting

{

    public static void main(String arg[])

    {

        byte b;

        int i = 81;

        double d = 323.142;

        float f = 72.38f;

        char c = ‘A’;

        c = (char) i;

        System.out.println(“i = ” + i + ” c = ” + c);

        i = (int) d; // LINE A

        System.out.println(“d = ” + d + ” i = ” + i); // LINE B     

        i = (int) f; // LINE C

        System.out.println(“f = ” + f + ” i = ” + i); // LINE D

        b = (byte) d;

        System.out.println(“d = ” + d + ” b = ” + b);

    }

}

Verify the Output

class Vehicle

{

    int maxSpeed = 120;

}

 

/* sub class Car extending vehicle */

class Car extends Vehicle

{

    int maxSpeed = 180;

    void display()

    {

        /* print maxSpeed of base class (vehicle) */

        System.out.println(“Maximum Speed: ” + super.maxSpeed);

    }

}

 

/* Driver program to test */

class Test

{

    public static void main(String[] args)

    {

        Car small = new Car();

        small.display();

    }

}

What is the output.

/* Base class Person */

class Person

{

    void message()

    {

        System.out.println(“This is person class”);

    }

}

 

/* Subclass Student */

class Student extends Person

{

    void message()

    {

        System.out.println(“This is student class”);

    }

    // Note that display() is only in Student class

    void display()

    {

        // will invoke or call current class message() method

        message();

        // will invoke or call parent class message() method

        super.message();

    }

}

 

/* Driver program to test */

class Test

{

    public static void main(String args[])

    {

        Student s = new Student();

        // calling display() of Student

        s.display();

    }

}

Check the output

/* superclass Person */

class Person

{

    Person()

    {

        System.out.println(“Person class Constructor”);

    }

}

 

/* subclass Student extending the Person class */

class Student extends Person

{

    Student()

    {

        // invoke or call parent class constructor

        super();

        System.out.println(“Student class Constructor”);

    }

}

 

/* Driver program to test*/

class Test

{

    public static void main(String[] args)

    {

        Student s = new Student();

    }

}

 

Verify the output for the below classes:

public Class SuperDemo{

int a,b;

}

 

public Class Subdemo extends SuperDemo{

int a,b;

void disply(){

 

System.out.println(a);

System.out.println(b);

super.a=10;

super.b=20;

System.out.println(super.a);

System.out.println(super.b);

}

 

public static void main (String args[]) {

 Subdemo obj= new Subdemo();

obj.a=1;

obj.b=2;

obj.disply();

}

}

 

Verify the output for the below coding:

public Class SuperDemo{

int a,b;

public void show() {

System.out.println(a);

System.out.println(b);

}

}

 

public Class Subdemo extends SuperDemo{

int a,b;

void disply(){

System.out.println(a);

System.out.println(b);

super.a=10;

super.b=20;

 super.show();

}

 

public static void main (String args[]) {

 Subdemo obj= new Subdemo();

obj.a=1;

obj.b=2;

obj.disply();

}

}

Verify the output:

public Class SuperDemo{

int a,b;

SuperDemo(int x, int y){

 a=x;

b=y

System.out.println(“Super class constructor called “);

}

}

 

public Class Subdemo extends SuperDemo{

int a,b;

SubDemo(int x, int y){

super(10,20);

 a=x;

b=y

System.out.println(“Sub class constructor called “);

}

 

public static void main (String args[]) {

 Subdemo obj= new Subdemo(1,2);

}

}

Verify the output:

public class ThisDemo {

    int a, b;

 

 ThisDemo(int a, int b){

      a=a;

      b=b;

 }

    public static void main(String[] args){

        ThisDemo obj= new ThisDemo(1,2);

        System.out.println(obj.a);

        System.out.println(obj.b);

}

 

public class ThisDemo {

    int a, b;

 ThisDemo(int a, int b){

      this.a=a;

       this.b=b;

 }

    public static void main(String[] args){

        ThisDemo obj= new ThisDemo(1,2);

        System.out.println(obj.a);

        System.out.println(obj.b);

}

}

 

public class ThisDemo {

    int a, b;

 

ThisDemo(){

System.out.println(“Default constructor called”);

}

 

 ThisDemo(int a, int b){

       this();

        this.a=a;

       this.b=b;

 }

 

    public static void main(String[] args){

        ThisDemo obj= new ThisDemo(1,2);

        System.out.println(obj.a);

        System.out.println(obj.b);

}

}

 

public class Test{

    int a, b;

Test(int a, int b){     

       this.a=a;

       this.b=b;

 }

 

void show(){

System.out.println(“Show() method called”);

}

 

void print(){

    this.show();

    System.out.println(a);

    System.out.println(b);

 }

    public static void main(String[] args){

        Test obj= new Test(1,2);

        obj.print()

}

}

 

Guess the output:

public class InheritanceTest

{

  public static void main(String[] args)

  {

    Parent c = new Child();

    c.doSomething();

  }

}

 

class Parent

{

  public void doSomething()

  {

    System.err.println(“Parent called”);

  }

}

 

class Child extends Parent

{

  public void doSomething()

  {

    System.err.println(“Child called”);

  }

}

What is the output?

public class InheritanceTest

{

  public static void main(String[] args)

  {

    Parent c = new Child();

  }

}

 

class Parent

{

  public Parent()

  {

    System.err.println(“Parent called”);

  }

}

 

class Child extends Parent

{

  public Child()

  {

    System.err.println(“Child called”);

  }

}

______________________________________________________________________

Chapter 8: Interfaces and Abstract Classes

Predict the output of the following program.

abstract class demo

{

    public int a;

    demo()

    {

        a = 10;

    }

    abstract public void set();

    abstract final public void get();

}

 

class Test extends demo

{

    public void set(int a)

    {

        this.a = a;

    }

    final public void get()

    {

        System.out.println(“a = ” + a);

    }

 

    public static void main(String[] args)

    {

        Test obj = new Test();

        obj.set(20);

        obj.get();

    }

}

 

What is the output of this program?

    interface calculate {

        void cal(int item);

    }

    class display implements calculate {

        int x;

        public void cal(int item) {

            x = item * item;           

        }

    }

    class interfaces {

        public static void main(String args[]) {

            display arr = new display;

            arr.x = 0;     

            arr.cal(2);

            System.out.print(arr.x);

        }

    }

What is the output of this program?

interface calculate {

            int VAR = 0;

            void cal(int item);

        }

        class display implements calculate {

            int x;

          public  void cal(int item) {

                if (item<2)

                    x = VAR;

                else

                    x = item * item;           

            }

        }

 class interfaces {

            public static void main(String args[]) {

                display[] arr=new display[3];

               for(int i=0;i<3;i++)

               arr[i]=new display();

               arr[0].cal(0);   

               arr[1].cal(1);

               arr[2].cal(2);

               System.out.print(arr[0].x+” ” + arr[1].x + ” ” + arr[2].x);

            }

        }

 

What should be done to compile the code successfully?

interface IBeingZeroStudent

{

                double getPercentageMarks();

                String getName();

                int getAge();

}

 

class BeingZeroStudent implements IBeingZeroStudent

{

                public String getName()

                {

                                return “BeingZeroStudent”;

                }             

                public int getAge()

                {

                                return 20;

                }             

}

_________________________________________________________________

References:

                https://payilagam.com/blogs/java-training-in-chennai-basics-learning-part-i/

                https://payilagam.com/blogs/java-training-in-chennai-basics-learning-part-ii/

                https://payilagam.com/blogs/java-training-in-chennai-basics-learning-part-iii/

COURTESY

                            www.proprofs.com, meritcampus.com, www.geeksforgeeks.org,       

                            www.f5java.com, dzone.com, www.examveda.com, beingzero.in/

                            scjptest.comwww.gocertify.com, web.cs.iastate.edu,          

                             www.sanfoundry.com, https://codingpuzzles.com