22/01/25 ---------- Q)What is the output of the following program? class Student{ int rollno; String name; Student(int rno,String nm){ rollno=rno; name=nm; } void displayStudent(){ System.out.println("ROLLNO:"+rollno); System.out.println("NAME:"+name); } }// class StudentApplication{ public static void main(String a[]){ Student s=new Student(); s.displayStudent(); } }//Student Ans:- The above program causes compilation error. Reason:- For Student class, zero argument constructor not available. But in the syntax of Student object creation, we have used zero argument constructor. Q)What is constructor overloading? =>Having multiple constructors within the same class with different parameters is nothing but constructor overloading. Q)Example Program on Constructor Overloading. //BooksApplication.java class Book{ String title; String author; int pages; double price; Book(){ title="Java Complete Reference"; author="Herbert Schildt"; pages=800; price=650; }//non parameterised constructor Book(String t,String a,int p,double pr){ title=t; author=a; pages=p; price=pr; } void displayBookDetails(){ System.out.println("TITLE:"+title); System.out.println("AUTHOR:"+author); System.out.println("NO OF PAGES:"+pages); System.out.println("PRICE Rs."+price); } } class ConstructorOverloadingApplication{ public static void main(String[] args){ Book b1=new Book(); System.out.println("First book details....."); b1.displayBookDetails(); Book b2=new Book(); System.out.println("Second book details....."); b2.displayBookDetails(); Book b3=new Book("Let us C","Yaswant Kanetakar",300,260); System.out.println("Third book details....."); b3.displayBookDetails(); } } Q)What is the output of the following program? class Student{ int rollno; String name; Student(int rollno,String name){ rollno=rollno; name=name; } void displayStudent(){ System.out.println("ROLLNO:"+rollno); System.out.println("NAME:"+name); } } class StudentApplication{ public static void main(String a[]){ Student s=new Student(101,"Rama"); s.displayStudent(); } } Ans:- ROLLNO:0 NAME:null =>Reason for the unexpected output is parameter names and instance variable names are the same. =>Whenever instance variable name and parameter name are the same, preference will be given to local variable only. Instance variable is not reffered. =>To overcome the problem, we use "this" keyword. Student(int rollno,String name){ this.rollno=rollno; this.name=name; } =>"this" is an implicit reference to the current object. Q)What is constructor chaining? =>Within the constructor of a class calling another constructor of the same class is nothing but constructor chaining. =>It is done using "this" keyword. class Employee{ int empno; String name; Employee(){ System.out.println("Employee appointed"); } Employee(int empno,String name){ this();//zero argument constructor is called this.empno=empno; this.name=name; } void displayEmployee(){ System.out.println("EMPNO:"+empno); System.out.println("NAME:"+name); } public static void main(String[] args) { Employee e=new Employee(101,"Rama"); e.displayEmployee(); } } Q)Develop a Java application in which, two Distance objects(with the fields feet &inches ) are added and resultant distance is displayed. class Distance{ int feet; int inches; Distance(int feet,int inches){ this.feet=feet; this.inches=inches; } void displayDistance(){ System.out.println(feet+" feet "+inches+" inches"); } void addTwoDistances(Distance d1,Distance d2){ int i=d1.inches+d2.inches; int f=d1.feet+d2.feet; if(i>=12){ feet++; i=i-12; } Distance d=new Distance(f,i); d.displayDistance(); } } class DistanceApplication{ public static void main(String[] args) { Distance d1=new Distance(10,9); System.out.print("First distance is "); d1.displayDistance(); Distance d2=new Distance(9,10); System.out.print("Second distance is "); d2.displayDistance(); System.out.print("Resultant distance is "); d1.addTwoDistances(d1,d2); } }