3rd Cs
3rd Cs
3rd Cs
1. Using packagename.* :-
If you use package.* then all the classes and interfaces of this package will be
accessible but not sub packages.
The import keyword is used to make the classes and interfaces of another
package accessible to the current package.
E.g.
package pack;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
2. Using packagename.classname :-
If you import packagename.classname then only declared class of this package
will be accessible.
Eg.
import pack.A;
class B
{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
(d) How to add new class to a package? Explain with an
example. 4M
Ans:
Create a package :
simply include a package command as the first statement in a Java source file.
Include classes:
Any classes declared within that file will belong to the specified package.
The package statement defines a name space in which classes are stored.
If you omit the package statement, the class names are put into the default package, which has no
name.
Syntax:
package pkg; //Here, pkg is the name of the package
Example:
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();
Winter 2017
Eg:
interface MyInterface
int strength=60;
void method1();
class MyBaseClass
String str;
MyBaseClass(String str)
this.str = str;
{
System.out.println("Class: "+str);
float total;
super(str);
total = t;
System.out.println("Avg is "+avg);
c.display();
c.method1();
built-in
user defined.
Creation of user defined package:
To create a package a physical folder by the name of the package should be created in the
computer.
Example:
we have to create a package myPack, so we create a folder
d:\myPack
The first line in the java program should be package <name>; followed by imports and the
program logic.
package myPack; {
{ }
import myPack.*;
(d) Enlist any four built in packages in java API with atleast two
class name from each package.
Ans.:
Inbuilt packages in java:
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
classes :
Thread, String
2. java.util:-
– language utility classes such as vectors, hash tables, random numbers, date etc
classes :
Date,Collection,Vector
3. java.io :-
– input/output support classes. They provide facilities for the input and output of data
classes :
FileReader, FileWriter
4. java.awt :-
– set of classes for implementing graphical user interface. They include classes for windows,
buttons, lists, menus and so on
classes :
Button,Label
5. java.net:-
– classes for networking. They include classes for communicating with local computers as well as
with internet servers
classes :
Socket,URL
Summer 2017
(b) What is package? State how to create and access user defined
package in Java. (Note: Code snippet can be used for describing)
Ans.:
Package is a name space that organizes a set of related classes and interfaces. Conceptually, it is
similar to the different folders in a computer. It also provides access protection and removes name
collision.
Example:
we have to create a package myPack, so we create a folder d:\myPack. The java program is to be
written and saved in the folder myPack.
To add a program to the package, the first line in the java program should be package <name>;
followed by imports and the program logic.
package myPack;
import java.util;
//code
import mypack.*;
(b) What is meant by interface? State its need and write syntax and
features of interface.
Ans.:
1) Interface is the mechanism by which multiple inheritance is possible in java. It is a
reference type in Java.
2) An interface has all the methods undefined.
3) For a java class to inherit the properties of an interface, the interface should be implemented
by the child class using the keyword “implements”.
4) All the methods of the interface should be defined in the child class.
Example:
interface MyInterface
int strength=60;
void method1();
void method2();
int total;
MyClass(int t)
total = t;
System.out.println("Avg is "+avg);
c.method1();
Need:
1) A java class can only have one super class. Therefore for achieving multiple
inheritance, that is in order for a java class to get the properties of two parents,
interface is used.
2) Interface defines a set of common behaviours.
3) The classes implement the interface, agree to these behaviours and provide their own
implementation to the behaviours.
Syntax:
interface InterfaceName
Features:
(d) List any four built-in packages from Java API along with their use.
Ans.
1. java.lang:
Contains Language support classes which are used by Java compiler during compilation of
program
2. java.util:
Contains language utility classes such as vectors, hash tables, random numbers, date etc.
3. java.io:
Contains I/O support classes which provide facility for input and output of data.
4. java.awt:
Contains a set of classes for implementing graphical user interface.
5. java.aplet:
Contains classes for creating and implementing applets.
6. java.sql:
Contains classes for database connectivity.
7. java.net:
Contains classes for networking.