Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
181 views

Unit 3 Java Programming Question Answer

The document discusses object-oriented programming concepts like inheritance, polymorphism, abstraction and encapsulation in Java. It provides examples of using final keyword with classes and methods. It also discusses creating packages and user-defined packages in Java with examples.

Uploaded by

pranavchavan3103
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views

Unit 3 Java Programming Question Answer

The document discusses object-oriented programming concepts like inheritance, polymorphism, abstraction and encapsulation in Java. It provides examples of using final keyword with classes and methods. It also discusses creating packages and user-defined packages in Java with examples.

Uploaded by

pranavchavan3103
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

UNIT 3
SubjectName:Java Programming ModelAnswer SubjectCode:22412

1 List the types of inheritance which is supported by java. 2M


(Summer 19)(Winter 22)
Ans Any two
1 M each

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

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page3


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

7 Describe final variable and final method. (Winter 19) 4M


Ans Final method: making a method final ensures that the 2M for
functionality defined in this method will never be altered in any definition,
way, ie a final method cannot be overridden. 2M for
Syntax: example
final void findAverage()
{
//implementation
}
Example of declaring a final method:
class A
{ final void show()
{
System.out.println(“in show of A”);
}
}
class B extends A
{
void show() // can not override because it is declared with final
{
System.out.println(“in show of B”);
}}
Final variable: the value of a final variable cannot be changed.
Final variable behaves like class variables and they do not take
any space on individual objects of the class.
Example of declaring final variable: final int size = 100;
8 Define package. How to create user defined package? 6M
Explain with example(Winter 19)(Summer 23 for 4m)
Ans Java provides a mechanism for partitioning the class namespace (Definition
into more manageable parts. This mechanism is the package. The of
package is both naming and visibility controlled mechanism. package -
Package can be created by including package as the first statement 1M,
in java source code. Any classes declared within that file will

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page4


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
belong to the specified package. Package defines a namespace in which classes
are stored. Package
The syntax for defining a package is: creation
package pkg; - 2M
Here, pkg is the name of the package Example -
eg : package 3M
mypack; (Note Any
Packages are mirrored by directories. Java uses file system other
directories to store packages. The class files of any classes which example
are declared in a package must be stored in a directory which has can be
same name as package name. The directory must match with the considered
package name exactly. A hierarchy can be created by separating )
package name and sub package name by a period(.) as
pkg1.pkg2.pkg3; which requires a directory structure as
pkg1\pkg2\pkg3.
Syntax:
To access package In a Java source file, import statements
occur immediately following the package statement (if
it exists) and before any class definitions.
Syntax:
import pkg1[.pkg2].(classname|*);
Example:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h));
}
}
Source file:
import package1.Box;
class volume
{
public static void main(String args[])
{
Box b=new Box();
b.display();
}
}
9 Implement the following inheritance 6M

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page5


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

(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);

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page6


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
}
void Total_Sal()
{
Display();
Basic_Sal();
double Total_Sal=Basic_Salary + TA + DA +
HRA;
System.out.println("Total Salary :"+Total_Sal);
}
}
class EmpDetails
{ public static void main(String args[])
{ Gross_Salary s=new
Gross_Salary("Sachin",20,1000,2000,7000);
s.Total_Sal();
}
}
10 State use of finalize( ) method with its syntax.(Summer 19) 2M
Ans Use of finalize( ): Use 1M
Sometimes an object will need to perform some action when it is destroyed. Eg. If Syntax
an object holding some non java resources such as 1M
file handle or window character font, then before the object is
garbage collected these resources should be freed. To handle such
situations java provide a mechanism called finalization. In
finalization, specific actions that are to be done when an object is
garbage collected can be defined. To add finalizer to a class define
the finalize() method. The java run-time calls this method whenever it
is about to recycle an object.
Syntax:
protected void finalize() {
}
11 Give syntax to create a package and accessing package in java(Summer 23) 2m
First create a folder and give name of package to it .
e.g.c:\mypack
for accessing package in java
import mypack.*;
to compile and run program
-d c:\mypack className.java
To run
Java className
12 Describe interface in Java with suitable example. 4M
The interface in Java is a mechanism to achieve abstraction. There can be only
abstract methods in the Java interface, not the method body. It is used to achieve
abstraction and multiple inheritances in Java using Interface.
Syntax for Java Interfaces
interface {
// declare constant fields
// declare methods that abstract
// by default.
}

import java.io.*;

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page7


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

// A simple interface
interface In1 {

// public, static and final


final int a = 10;

// public and abstract


void display();
}

// A class that implements the interface.


class TestClass implements In1 {

// Implementing the capabilities of


// interface.
public void display(){
System.out.println("Geek");
}

// 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...

14 Explain dynamic method dispatch in Java with suitable example. .(Summer 4M


19)6m & 4 m
Ans Dynamic method dispatch is the mechanism by which a call to an Explanatio
overridden method is resolved at run time, rather than compile time. n 2M
 When an overridden method is called through a superclass
reference, Java determines which version (superclass/subclasses) of
that method is to be executed based upon the type of the object being
referred to at the time the call occurs. Thus, this determination is
made at run time.
 At run-time, it depends on the type of the object being referred to
(not the type of the reference variable) that determines which version
of an overridden method will be executed
 A superclass reference variable can refer to a subclass object. This
is also known as upcasting. Java uses this fact to resolve calls to
overridden methods at run time.
Therefore, if a superclass contains a method that is overridden by a
subclass, then when different types of objects are referred to through
a superclass reference variable, different versions of the method are
executed. Here is an example that illustrates dynamic method
dispatch:
// A Java program to illustrate Dynamic Method
// Dispatch using hierarchical inheritance
class A
{
void m1() Example
{ 2M
System.out.println("Inside A's m1 method");
}
}
class B extends A
{
// overriding m1()
void m1(){
System.out.println("Inside B's m1 method");
}
}
class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}
// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page9
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
// obtain a reference of type A
A ref;
// ref refers to an A object
ref = a;
// calling A's version of m1()
ref.m1();
// now ref refers to a B object
ref = b;
// calling B's version of m1()
ref.m1();
// now ref refers to a C object
ref = c;
// calling C's version of m1()
ref.m1();
}
}

Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page1


0
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT2

Prof.GulnajSayyad ShreeRamchandraCollegeofEngineering Page11


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT2

Prof.GulnajSayyad ShreeRamchandraCollegeofEngineering Page12


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT2

Prof.GulnajSayyad ShreeRamchandraCollegeofEngineering Page13

You might also like