Unit 3 Java Programming Question Answer
Unit 3 Java Programming Question Answer
UNIT 3
SubjectName:Java Programming ModelAnswer SubjectCode:22412
2 State the use of final keyword with respect to inheritance. (Winter 22) 4M
Ans Final keyword : The keyword final has three uses. First, it can be used to create
the equivalent of a named constant.( in interface or class we use final as shared Use of final
constant or constant.) keyword-2
Other two uses of final apply to inheritance M
Using final to Prevent Overriding While method overriding is one of Java’s most Program-2
powerful features, M
To disallow a method from being overridden, specify final as a modifier at the
start of its declaration. Methods declared as final cannot be overridden.
The following fragment illustrates final:
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void meth()
{ // ERROR! Can't override.
System.out.println("Illegal!");
}
}
As base class declared method as a final , derived class can not override the
definition of base class methods.
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page1
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
3 Develop a program to create a class ‘Book’ 4M
having data members author, title
and price. Derive a class 'Booklnfo' having data
member 'stock position’ and
method to initialize and display the information
for three objects. (Winter 22)
Ans class Book { String author, title, publisher; Book(String a, String t, String p)
{ author = a; title = t; publisher = p; } } class BookInfo extends Book { float
price; int stock_position; BookInfo(String a, String t, String p, float amt, int s)
{ super(a, t, p); price = amt; stock_position = s; } void show()
{ System.out.println("Book Details:"); System.out.println("Title: " + title);
System.out.println("Author: " + author); System.out.println("Publisher: " +
publisher); System.out.println("Price: " + price); System.out.println("Stock
Available: " + stock_position); } } class Exp6_1 { public static void main(String[]
args) { BookInfo ob1 = new BookInfo("Herbert Schildt", "Complete Reference",
"ABC Publication", 359.50F,10); BookInfo ob2 = new BookInfo("Ulman",
"system programming", "XYZ Publication", 359.50F, 20);
BookInfo ob3 = new BookInfo("Pressman", "Software Engg", "Pearson
Publication", 879.50F, 15); ob1.show();
ob2.show(); ob3.show(); } }
OUTPUT Book Details: Title: Complete Reference Author: Herbert Schildt
Publisher: ABC Publication Price: 2359.5 Stock Available: 10 Book Details:
Title: system programming Author: Ulman Publisher: XYZ Publication Price:
359.5 Stock Available: 20 Book Details: Title: Software Engg Author: Pressman
Publisher: Pearson Publication Price: 879.5 Stock Available: 15
4 Write a program to create a class 'salary with data members empid', ‘name' 6M
and ‘basicsalary'. Write an interface 'Allowance’ which stores rates of
calculation for da as 90% of basic salary, hra as 10% of basic salary and pf
as 8.33% of basic salary. Include a method to calculate net salary and display
it.
(Winter 22)
Ans interface allowance 6 M for
{ correct
double da=0.9*basicsalary; program
double hra=0.1*basicsalary;
double pf=0.0833*basicsalary;
void netSalary();
}
class Salary
{
int empid;
String name;
float basicsalary;
Salary(int i, String n, float b)
{
empid=I;
name=n;
basicsalary =b;
}
void display()
{
System.out.println("Empid of Emplyee="+empid);
System.out.println("Name of Employee="+name);
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page2
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
System.out.println("Basic Salary of Employee="+ basicsalary);
}
}
class net_salary extends salary implements allowance
{
float ta;
net_salary(int i, String n, float b, float t)
{
super(i,n,b);
ta=t;
}
void disp()
{
display();
System.out.println("da of Employee="+da);
}
public void netsalary()
{
double net_sal=basicsalary+ta+hra+da;
System.out.println("netSalary of Employee="+net_sal);
}
}
class Empdetail
{
public static void main(String args[])
{
net_salary s=new net_salary(11, “abcd”, 50000);
s.disp();
s.netsalary();
}
}
5 List any four Java API packages 2M
Ans 1.java.lang 1/2 Marks
2.java.util for
3.java.io one
4.java.awt Package
5.java.net
6.ava.applet
6 Differentiate between class and interfaces.(Winter 19) 4M
Ans 1M for
each
point
(Winter
19)(Summer 23 for 4 m only variables and methods are different)
Ans interface Salary (Interface:
{ 1M,
double Basic Salary=10000.0; Employee
void Basic Sal(); class:
} 2M,
class Employee Gross_Sal
{ ary
String Name; class: 3M)
int age;
Employee(String n, int b)
{
Name=n;
age=b;
}
void Display()
{
System.out.println("Name of Employee
:"+Name);
System.out.println("Age of Employee :"+age);
}
}
class Gross_Salary extends Employee implements Salary
{
double HRA,TA,DA;
Gross_Salary(String n, int b, double h,double t,double d)
{
super(n,b);
HRA=h;
TA=t;
DA=d;
}
public void Basic_Sal()
{
System.out.println("Basic Salary
:"+Basic_Salary);
import java.io.*;
// A simple interface
interface In1 {
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Output
Geek
10
13 List out different ways to access package from another package 2M
There are three ways to access the package from outside the package.
1) import package.*;
2) import package.classname;
14 Write a program to show the Hierachical inheritance 4M
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page8
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
meowing...
eating...