Document From Kathawde Komal
Document From Kathawde Komal
Document From Kathawde Komal
A class that is declared with abstract keyword is known as abstract class in java. It can have
abstract and non-abstract methods (method with body).Abstraction is a process of hiding the
implementation details and showing only functionality to the user. Another way, it shows only
important things to the user and hides the internal details.A method must be always redefined
in a subclass of an abstract class, as abstract class does not contain method body. Thus abstract
makes overriding compulsory.Class containing abstract method must be declared as
abstract.You can not declare abstract constructor, and so, objects of abstract class cannot be
instantiated.
Syntax :
abstract method1(…);
method2(….);
Example :
abstract class A
void show()
}}
class B extends A
{
void disp()
}}
class test
B b = new B();
b.disp();
b.show();
}}
2] What is interface? How it is different from class? With suitable program explain the use of
interface.
Interface:
Exampl
e:
interface sports
{
int sport_wt=5;
class test
int roll_no;
String name;
int m1,m2;
roll_no=r;
name=nm;
m1=m11;
m2=m12;
}}
super (r,nm,m11,m12);
System.out.println("Roll no : "+roll_no);
System.out.println("Name : "+name);
System.out.println("sub1 : "+m1);
System.out.println("sub2 : "+m2);
System.out.println("sport_wt : "+sport_wt);
int t=m1+m2+sport_wt;
System.out.println("total : "+t);
r.disp();
}}
Output :
D:\>java result
Roll no : 101
Name : abc
sub1 : 75
sub2 : 75
sport_wt : 5
total : 155
3] What is final variable and methods? How it is different from abstract method?
4]
void gross_sal();
class Employee
String name;
float basic_sal;
Employee(String n, float b)
name=n;
basic_sal=b;
void display()
System.out.println("Name of Employee="+name);
float hra;
super(n,b);
hra=h;
void disp()
display();
System.out.println("HRA of Employee="+hra);
double gross_sal=basic_sal+ta+hra;
System.out.println("TA of Employee="+ta);
System.out.println("DA of Employee="+da);
class Empdetail
s.disp();
s.gross_sal();
}}
Package: Packages are used for grouping a variety of classes & interfaces together. This
grouping is done according to functionality. Packages acts as containers of classes. Packages are
stored in hierarchical manner & are explicitly imported into new class definitions. Following
benefits are achieved by organizing classes into packages:
1. The classes contained in the packages of other programs can be easily reused.
2. In packages, classes can be unique compared with classes in other packages. That is two
classes in two different packages can have same name. They may be referred by their fully
qualified name, comprising package name & class name.3. Packages provide way to hide classes
thus preventing other programs or packages from accessing classes that are meant for internal
use only.4. Packages also provide a way for separating “design” from “coding”. First we can
design classes & decide their relationship & then we can implement Java code needed for
methods. It is possible to change implementation of any method without affecting rest of the
design.Java packages are therefore classified into two types. First category is known as Java API
packages & second is known as User defined packages.Creating Packages: First declare name of
package using package keyword followed by package name. This must be first statement in a
java source file. Here is an example:
…..
…..
Here the package name is firstPackage. The class FirstClass is now considered a part of this
package. This listing would be saved as file called FirstClass.java & located in a director named
firstPackage. When source file is complied, Java will create a .class file & store it in same
directoryclass must be located in a directory that has same name as the package, & this
directory should be a subdirectory of the directory where classes that will import package are
located. Creating package involves following steps:
1. Declare the package at beginning of a file using the form package packagename;
2. Define the class that is to be put in the package & declare it public
3. Create a subdirectory under directory where main source files are stored
Case is significant & therefore subdirectory name must match package name exactly. Java
supports the concept of package hierarchy. This is done by specifying multiple names in a
package statement, separated by dots. Example: package firstPackage.secondPackage;This
approach allows us to group related classes into a package & then group related packages into
larger package. To store this package in subdirectory named firstPackage/secondPackage.A java
package file can have more than one class definition. in such cases only one of the classes may
be declared public & that class name with .java extension is the source file name. When a
source file with more than one class definition is complied, java creates independent .class files
for these classes.
6] Write the effect of access specifiers public, private and protected in package.
Visibility restriction must be considered while using packages and inheritance in program
visibility restrictions are imposed by various access protection modifiers. packages acts as
container for classes and other package and classes acts as container for data and methods.
Data members and methods can be declared with the access protection modifiers such as
private, protected and public. Following table shows the access protections
7] Explain with example how to achieve multiple inheritance with interface.
Multiple inheritances: It is a type of inheritance where a derived class may have more than one
parent class. It is not possible in case of java as you cannot have two classes at the parent level
Instead there can be one class and one interface at parent level to achieve multiple
interface.Interface is similar to classes but can contain on final variables and abstract method.
Interfaces can be implemented to a derived class.
{
r.disp();
}}
8] What is package? State any four system packages along with their use? How to add class to a
user defined packages?
Package: Java provides a mechanism for partitioning the class namespace into more
manageable parts. This mechanism is the „package‟. The package is both naming and visibility
controlled mechanism. System packages with their use:
1. java.lang - language support classes. These are classes that java compiler itself uses and
therefore they are automatically imported. They include classes for primitive types,strings, math
functions, threads and exceptions.
2. java.util – language utility classes such as vectors, hash tables, random numbers, date etc.
3. java.io – input/output support classes. They provide facilities for the input and output of data
6. java.applet – classes for creating and implementing applets. To add a class to user defined
package :
2) Any classes declared within that file will belong to the specified package.
3) The package statement defines a name space in which classes are stored.
4) Example :
package MyPack;
9] State the use of final keyword w. r. t. a method and the variable with suitable example
All variable and methods can be overridden by default in subclass. In order to prevent this, the
final modifier is used. Final modifier can be used with variable, method or class.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.Eg of declaring final variable: final
int size = 100;final method: making a method final ensures that the functionality defined in this
method will never be altered in any way, ie a final method cannot be overridden.
//implementation
class person
String name;
int age;
name=n;
age=a;
}
void display()
System.out.println("name--->"+name);
System.out.println("age--->"+age);
String emp_designation;
float emp_salary;
emp_designation=d;
emp_salary=s;
void emp_dis()
System.out.println("emp_designation-->"+emp_designation);
System.out.println("emp_salary-->"+emp_salary);
}}
class single_demo
e.accept("ramesh",35);
e.display();
e.accept_emp("lecturer",35000.78f);
e.emp_dis();
}}
Java provides a mechanism for partitioning the class namespace into more manageable
parts called package (i.e package are container for a classes). The package is both naming
and visibility controlled mechanism. Package can be created by including package as the
first statement in java source code. Any classes declared within that file will belong to the
specified package.
package pkg;
eg : package mypack;
Packages are mirrored by directories. Java uses file system directories to store packages.
The class files of any classes which are declared in a package must be stored in a
directory which has same name as package name. The directory must match with the
package name exactly. A hierarchy can be created by separating package name and sub
pkg1\pkg2\pkg3.
Syntax:
following the package statement (if it exists) and before any class definitions.
Syntax:
import pkg1[.pkg2].(classname|*);
Example:
package1:
package package1;
int l= 5;
int b = 7;
int h = 8;
System.out.println("Volume is:"+(l*b*h)); }
Source file:
import package1.Box;
class VolumeDemo
b.display();
12] State the use of ‘super’ and ‘final’ keyword w.r.t inheritance with example.
hen you will want to create a superclass that keeps the details of its implementation to
itself (that is, that keeps its data members private). In this case, there would be no way for
a subclass to directly access or initialize these variables on its own. Whenever a subclass
needs to refer to its immediate super class, it can do so by use of the keyword super. As
constructer can not be inherited, but derived class can called base class constructer using
super ()
The second is used to access a member of the super class that has been hidden by a
member of a subclass.
A subclass can call a constructor method defined by its super class by use of the
A Second Use for superThe second form of super acts somewhat like this, except that it always
refers to the super class of the subclass in which it is used. This usage has the following general
form:
super.memberHere, member can be either a method or an instance variable. This second form
of super
Example:
class A
int i;
class B extends A
{
B(int a, int b)
super.i = a; // i in A
i = b; // i in B
void show()
class UseSuper
subOb.show();
Final keywords
The keyword final has three uses. First, it can be used to create the equivalent of a named
While method overriding is one of Java‟s most powerful features, there will be times
When you will want to prevent it from occurring. To disallow a method from being
overridden, specify final as a modifier at the start of its declaration. Methods declared
class A
class B extends A
void meth()
System.out.println("Illegal!");
As base class declared method as a final , derived class can not override the definition of base
class methods.
13]What is meant by an interface? State its need and write syntax and features of an
Defining an Interface:
Interface is also known as kind of a class. So interface also contains methods and
variables but with major difference the interface consist of only abstract method
(i.e.methods are not defined,these are declared only ) and final fields(shared constants).
This means that interface do not specify any code to implement those methods and data
fields contains only constants. Therefore, it is the responsibility of the class that
Syntax:
….
Features:
1. Variable of an interface are explicitly declared final and static (as constant) meaning
that the implementing the class cannot change them they must be initialize with a
constant value all the variable are implicitly public of the interface, itself, is declared
as a public
2. Method declaration contains only a list of methods without anybody statement and
ends with a semicolon the method are, essentially, abstract methods there can be
default implementation of any method specified within an interface each class that
Need:
Example:
interface sports
int sport_wt=5;
class Test
int roll_no;
String name;
int m1,m2;
roll_no=r;
name=nm;
m1=m11;
m2=m12;
}}
super (r,nm,m11,m12);
{
System.out.println("Roll no : "+roll_no);
System.out.println("Name : "+name);
System.out.println("sub1 : "+m1);
System.out.println("sub2 : "+m2);
System.out.println("sport_wt : "+sport_wt);
int t=m1+m2+sport_wt;
System.out.println("total : "+t);
r.disp();
}}
Method Overloading means to define different methods with the same name but different
parameters lists and different definitions. It is used when objects are required to perform
similar task but using different input parameters that may vary either in number or type of
arguments. Overloaded methods may have different return types. It is a way of achieving
polymorphism in java.
Example :
class Sample
{
return i + j ;
return s1 + s2;
return d1 + d2;
class AddOperation
System.out.println(sObj.addition(1,2));
System.out.println(sObj.addition("Hello ","World"));
System.out.println(sObj.addition(1.5,2.2));
}}
15] State any four system packages along with their use.
1. java.lang - language support classes. These are classes that java compiler itself uses
and therefore they are automatically imported. They include classes for primitive
2. java.util – language utility classes such as vectors, hash tables, random numbers, date
etc.
3. java.io – input/output support classes. They provide facilities for the input and
output of data
4. java.awt – set of classes for implementing graphical user interface. They include
5. java.net – classes for networking. They include classes for communicating with
16]Write syntax of defining interface .Write any major two differences between interface and a
class.
17]Describe with example how to achieve multiple inheritance with suitable program.
void display()
}
}
{ double HRA;
super(n,b);
HRA=h;
void disp_sal()
{ display();
class EmpDetails
s.disp_sal();
s.gross_sal();
This class can then be inherited by other, more specific classes, each adding those things that
are
unique to it. In the terminology of Java, a class that is inherited is called a superclass. The class
that
superclass.
. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword
super.
Superhas two general forms. The first calls the super class constructor. The second is used to
access a member of the superclass that has been hidden by a member of a subclass.
Super is used to call overridden method of base class or overridden data or evoked the
overridden
Class Box
Box()
{
void show()
//definition of show
BoxWeight()
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines
the
this keyword. this can be used inside any method to refer to the current object. That is, this is
always a reference to the object on which the method was invoked. You can use this anywhere
a
reference to an object of the current class‟ type is permitted. To better understand what this
refers
to, consider the following version of Box( ): // A redundant use of this. Box(double w, double h,
instance variable. This is why width, height, and depth were not used as the names of the
parameters to the Box( ) constructor inside the Box class. If they had been, then width would
have
this.width = width;
this.height = height;
this.depth = depth;
final method: making a method final ensures that the functionality defined in this method will
//implementation
EXAMPLE
class A
}
class B extends A
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.
20]Design a package containing a class which defines a method to find area of rectangle. Import
it in java application to calculate area of a rectangle
package Area;
doublelength,bredth;
length=l;
bredth=b;
return(length*bredth);
}
import Area.Rectangle;
class RectArea
double area=r.rect(10,5);
int roll_no;
{
this.name = name;
this.roll_no = roll_no;
this.m1 = m1;
this.m2 = m2;
interface exam {
double per;
super(n,r,m1,m2);
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
void display()
per_cal();
}
public static void main(String args[])
InputStreamReader(System.in));
try
the student");
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
r.display();
} catch(Exception e)
System.out.println("Exception caught"+e);
}}}
proper program.
in java is that new classes can be created that are built upon existing
classes.
multiple inheritance.
happens, the class which implements the interface must define all the
interfaces.
Eg:
import java.io.*;
class Student
String name;
int roll_no;
this.name = name;
this.roll_no = roll_no;
this.m1 = m1;
this.m2 = m2;
interface exam
{
public void per_cal();
double per;
super(n,r,m1,m2);
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
void display()
per_cal();
InputStreamReader(System.in));
try
{
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
r.display();
catch(Exception e)
System.out.println("Exception caught"+e);
Interface:
OR
package mypack;
import mypack.test;
This makesthe code much more elegant and less repetitive. Single
SingleLevelInheritanceChild(2);
cube.volume();
cube.area();
collision.
in the computer.
folder d:\myPack
To add a program to the package, the first line in the java program
logic.
package myPack;
import java.util;
//code
our program. Once we have done the import we can create the
object of the class from the package and thus through the object we
import mypack.*;
26]What is meant by interface? State its need and write syntax and
features of interface
Example:
interface MyInterface{
int strength=60;
void method1();
void method2();
int total;
MyClass(int t) {
total = t;
}
public void method1() {
System.out.println("Avg is "+avg);
c.method1();
Need:
A java class can only have one super class. Therefore for achieving
behaviours.
Syntax:
interface InterfaceName {
}
Features:
final and static. All the methods of the interface are implicitly
for the subclass to define all the methods of an interface. If all the
abstract class.
cannot be overridden.
e.g.
class test
System.out.println(“In superclass”);
2. Prevent inheritance:
e.g.
void disp()
System.out.println(“In superclass”);
Variable declared final, it is constant which will not and can not
change.
int ta=1000;
int da=4000;
class employee
String name="Abc";
int basic_sal=8000;
int hra;
int total=0;
salary(int h)
hra=h;
}
public void gross_sal()
total=basic_sal+ta+da+hra;
void disp_sal()
gross_sal();
System.out.println("Name :"+name);
s.disp_sal();
unique within its class. It can inherit the general attributes from its
subclass of Bird. Parrot inherits a lot many features of the class Bird
class Bird {
System.out.println(sum);
System.out.println(sum);
System.out.println("Class: "+str);
float total;
super(str);
total = t;
}
public void method1() {
System.out.println("Avg is "+avg);
c.display();
c.method1();
import java.io.*;
class Person {
intAadharno;
String name;
String Panno;
this.Aadharno = Aadharno;
this.name = name;
this.Panno = Panno;
this.Aadharno = Aadharno;
this.name = name;
Panno = "Not Applicable";
void display() {
System.out.println("Aadharno is :"+Aadharno);
System.out.println("Panno is :"+Panno);
BufferedReaderbr = new
BufferedReader(newInputStreamReader(System.in));
int a;
String n, pno;
try {
a = Integer.parseInt(br.readLine());
System.out.println("Enter name");
n = br.readLine();
System.out.println("Enter panno");
pno = br.readLine();
p = new Person(a,n,pno);
a = Integer.parseInt(br.readLine());
System.out.println("Enter name");
n = br.readLine();
System.out.println("Enter panno");
pno = br.readLine();
p1 = new Person(a,n,pno);
a = Integer.parseInt(br.readLine());
System.out.println("Enter name");
n = br.readLine();
p2 = new Person(a,n);
a = Integer.parseInt(br.readLine());
System.out.println("Enter name");
n = br.readLine();
p3 = new Person(a,n);
a = Integer.parseInt(br.readLine());
System.out.println("Enter name");
n = br.readLine();
System.out.println("Enter panno");
pno = br.readLine();
p4 = new Person(a,n,pno);
p.display();
p1.display();
p2.display();
p3.display();
p4.display();
} catch(Exception e) {
System.out.println("Exception caught"+e);
}
}
collision.
d:\myPack
package myPack;
importjava.util.*;
System.out.println("Inside package");
our program. Once we have done the import we can create the object
of the class from the package and thus through the object we can
import myPack.*;
c.myMethod();
If subclass (child class) has the same method as declared in the parent
Example:
class Vehicle{
void run()
{
obj.run();
34]Enlist any four built in packages in java API with atleast two
classes :Date,Collection,Vector
classes :Button,Label
servers
classes :Socket,URL
Using inheritance, you can create a general class that defines traits
other, more specific classes, each adding those things that are unique
Superhas two general forms. The first calls the super class
Class Box
Box()
void show()
//definition of show
BoxWeight()
Sometimes a method will need to refer to the object that invoked it.
To allow this, Java defines the this keyword. this can be used inside
any method to refer to the current object. That is, this is always a
reference to the object on which the method was invoked. You can
d; }
when a local variable has the same name as an instance variable, the
local variable hides the instance variable. This is why width, height,
and depth were not used as the names of the parameters to the Box( )
constructor inside the Box class. If they had been, then width would
width.
this.width = width;
this.height = height;
this.depth = depth;
}
Ans: Definition: Java does not support multiple inheritances with only classes.
Syntax:
Variables declaration;
Methods declaration;
Features:
multiple inheritance.
37]What is package in Java? Write a program to create a package andimport the package in
another class.
Ans: Package: Java provides a mechanism for partitioning the class namespace
into more manageable parts called package (i.e package are container for a
source code. Any classes declared within that file will belong to the specified
package.
Syntax:
package pkg;
Program:
package1:
package package1;
int l= 5;
int b = 7;
int h = 8;
System.out.println("Volume is:"+(l*b*h));
Source file:
import package1.Box;
class VolumeDemo
b.display();
Ans: Super Keyword: The super keyword in Java is a reference variable which is
used to refer immediate parent class object. Whenever you create the instance
Example:
class A
int i;
A(int a, iont b)
i=a+b;
void add()
class B extends A
int j;
super(a,b);
j=a+b+c;
void add()
{
super.add();
“final”.
Example:
Final Variable: The final variable can be assigned only once. The value of a
though a sub class can call the final method of parent class without any issues
Example:
class XYZ
void demo()
{
System.out.println("ABC Class Method");
obj.demo();
The above program would throw a compilation error, however we can use the
class XYZ
obj.demo();
Output:
XYZ Class Method
final class: We cannot extend a final class. Consider the below example:
void demo()
System.out.println("My Method");
obj.demo();
Output:
same name, type, & parameter list as a method in superclass then the method
called from within a subclass it will always refer to the version of method
defined by subclass. The version of method from superclass will be hidden.
Example:
class overridetest
int x,y;
overridetest(int a,int b)
x=a;
y=b;
void display()
System.out.println("x="+x);
System.out.println("y="+y);
int z;
super(a,b);
z=c;
System.out.println("z="+z);
t.display();
int sports_weightage=5;
void calc_total();
class person
String name;
String category;
person(String nm,String c)
name=nm;
category=c;
}
int marks1,marks2;
super(n,c);
marks1=m1;
marks2=m2;
int total;
if (category.equals("sportsman"))
total=marks1+marks2+sports_weightage;
else
total=marks1+marks2;
System.out.println("Name="+name);
System.out.println("Category ="+category);
System.out.println("Marks1="+marks1);
System.out.println("Marks2="+marks2);
System.out.println("Total="+total);
s1.calc_total();
s2.calc_total();
An Interface can extend another interface similarly to the way that a class can
extend another class. The extends keyboard is used to extends an interface and
Syntax:
Body of class2
Example:
Interace A
Int code=11;
String name=”Computer”;
Interface B extends A
{
Void display();
Ans Java provides a mechanism for partitioning the class namespace into more
manageable parts called package (i.e. package are container for a classes). The
created by including package as the first statement in java source code. Any
classes declared within that file will belong to the specified package.
Syntax:
package pkg;
eg : package mypack;
Java uses file system directories to store packages. The class files of any classes
which are declared in a package must be stored in a directory which has same
name as package name. The directory must match with the package name
structure as pkg1\pkg2\pkg3.
following the package statement (if it exists) and before any class definitions.
Syntax:
import pkg1[.pkg2].(classname|*);
Example:
package1:
package package1;
int l= 5;
int b = 7;
int h = 8;
System.out.println("Volume is:"+(l*b*h));
Source file:
import package1.Box;
class VolumeDemo
Example:
class A {
void callme() {
class B extends A {
// override callme()
void callme() {
class C extends A {
// override callme()
void callme() {
class Dispatch {
r = a; // r refers to an A object
r = b; // r refers to a B object
r = c; // r refers to a C object
properties and behaviors of a parent object. The idea behind inheritance is that
we can create new classes that are built upon existing classes. When we
inherit from an existing class, we can reuse methods and fields of the parent
class.
syntax -
A class which is inherited is called a parent or superclass, and the new class is
Types of inheritance
1)Single inheritance
2)Multilevel inheritance
3)Multiple inheritance
4)Hierarchical inheritance
image below, the class A serves as a base class for the derived class B.
For example
class one
int v1=10;
void method1()
void method2()
System.out.println("v1="+v1);
}
}
class singleinherit
t.method1();
t.method2();
Output:
we are in method 1
v1=10;
we are in method 2
45]
import java.io.*;
interface sport
int sport_wt=5 ;
class student
String name;
int roll_no,marks;
try
name=d.readLine();
roll_no=Integer.parseInt(d.readLine());
marks=Integer.parseInt(d.readLine());
catch(Exception e)
System.out.println(" ");
System.out.println("name="+name);
System.out.println("roll no="+roll_no);
System.out.println("marks1="+marks);
int total;
super.getdata();
total=sport_wt+super.marks;
System.out.println("total marks="+total);
r.get_total();
r.display();
Output:
abc
200
name=abc
roll no=11
marks1=200
total marks=205
example.
1. Built in packages
Built in packages are part of java API. Some of the built in packages
in java are:
Eg of classes/interfaces:
OutputStream
operations.
GUI.
Eg: Applet,AppletContext,AudioClip
47]
System.out.println("Customer name is "+name);
try {
String b,a,n;
int no;
InputStreamReader(System.in));
b = br.readLine();
System.out.println("Enter address");
a = br.readLine();
n = br.readLine();
no = Integer.parseInt(br.readLine());
p.input(b,a,no,n);
p.display();
} catch(Exception e) {
System.out.println("Exception caught"+e);}