Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

3rd Cs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

SUMMER– 18

(d) Explain how interface is used to achieve multiple Inheritance


in Java. 4M
Ans:
1. Multiple inheritances is not possible in java. Multiple inheritance happens when
a class is derived from two or more parent classes.
2. Java classes cannot extend more than one parent classes , instead it uses the
concept of interface to implement the multiple inheritance.
3. It contains final variables and the methods in an interface are abstract. A sub
class implements the interface.
4. When such implementation happens, the class which implements the interface
must define all the methods of the interface.
5. A class can implement any number of interfaces.
6. Example of multiple inheritance :
(b) Which are the ways to access package from another
package? Explain with example. 4M
Ans:
There are two ways to access the package from another package.
1. import package.*;
2. import package.classname;

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

public class class_name

//class body goes here

 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 VolumeDemo

public static void main(String args[])

Box b=new Box();

b.display();

Winter 2017

(b) What is the multiple inheritance? Write a java program to


implement multiple inheritance.
Ans.
 Multiple inheritance:
It is a feature in which a class inherits characteristics and features from more than one super class
or parent class.
Java cannot have more than one super class. Therefore interface is used to support multiple
inheritance in java. Interface specifies what a class must do but not how it is done.

Eg:

interface MyInterface

int strength=60;

void method1();

class MyBaseClass

String str;

MyBaseClass(String str)

this.str = str;

public void display()

{
System.out.println("Class: "+str);

public class MyClass extends MyBaseClass implements MyInterface

float total;

MyClass(String str, float t)

super(str);

total = t;

public void method1()

float avg = total/strength;

System.out.println("Avg is "+avg);

public static void main(String a[])

MyClass c = new MyClass("Fifth Sem",1300.0f);

c.display();

c.method1();

(b) What is package? How do we create it? Give the example to


create and to access package.
Ans.
Package is a name space that organizes a set of related classes and interfaces. It also provides
access protection and removes name collision.

Packages can be categorized into two: -

 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 java program is to be written and saved in the folder myPack.

The first line in the java program should be package <name>; followed by imports and the
program logic.

package myPack; {

import java.util.*; System.out.println("Inside package");

public class Myclass }

{ }

public void myMethod()

 Access user defined package:


To access a user defined package, we need to import the package in 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 access the instance methods.

import myPack.*;

public class MyClassExample{

public static void main(String a[]) {

Myclass c= new Myclass();


c.myMethod();

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

 Packages can be categorized into two:-


1) built-in
2) user defined.
 Creation of user defined package:
To create a package a physical folder by the name should be created in the computer.

 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;

public class Myclass


{

//code

 Access user defined package:


To access a user defined package, we need to import the package in 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 access the instance methods.

import mypack.*;

public class MyClassExample

public static void main(String a[])

Myclass c= new Myclass();

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

public class MyClass implements MyInterface

int total;

MyClass(int t)

total = t;

public void method1()

int avg = total/strength;

System.out.println("Avg is "+avg);

public void method2()

public static void main(String a[])

MyClass c = new MyClass(3600);

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

int var1 = value;

int var2 = value;

public return_type methodname1(parameter_list) ;

public return_type methodname2(parameter_list) ;

 Features:

1) Interface is defined using the keyword “interface”. Interface is implicitly abstract.


2) All the variables in the interface are by default final and static. All the methods of the
interface are implicitly public and are undefined (or implicitly abstract).
3) It is compulsory for the subclass to define all the methods of an interface.
4) If all the methods are not defined then the subclass should be declared as an abstract class.

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

You might also like