0% found this document useful (0 votes)
85 views12 pages

Oops Unit 2

1. The document discusses different types of inheritance in Java including single inheritance, multilevel inheritance, multiple inheritance through interfaces, and hierarchical inheritance. 2. Single inheritance allows a subclass to inherit from one parent class, while multilevel inheritance extends the inheritance relationship across multiple generations of classes. 3. Java does not support multiple inheritance of classes but does allow classes to inherit from multiple interfaces, and hierarchical inheritance involves inheriting a single superclass with multiple subclasses.

Uploaded by

Anusree K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views12 pages

Oops Unit 2

1. The document discusses different types of inheritance in Java including single inheritance, multilevel inheritance, multiple inheritance through interfaces, and hierarchical inheritance. 2. Single inheritance allows a subclass to inherit from one parent class, while multilevel inheritance extends the inheritance relationship across multiple generations of classes. 3. Java does not support multiple inheritance of classes but does allow classes to inherit from multiple interfaces, and hierarchical inheritance involves inheriting a single superclass with multiple subclasses.

Uploaded by

Anusree K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2-2 B (MCA-Sem-2) Inheritance, Interfaces and Packages

PART-1

2 UNIT
Inheritance, Interfaces
and Packges
Inheritance : Super Classes, Sub Classes, Protected Members,
Constructors in Sub Classes, Object Class,
Abstract Classes and Methods.

Questions-Answers

Long Answer Type and Medium Answer Type Questions

CONTENTS Que 2.1. Explain the concept of inheritance in Java.

2-2B to 2-9B Answer


Part-l : Inheritance: Super Classes, Sub 1 Inheritance in Java is a mechanism in which one object acquires all the
Classes, Protected Members,
properties and behaviors of parent object.
Constructors in Sub Classes, Object
Class, Abstract Classes and Methods
2 The idea behind inheritance in Java is that you can create new classes
that are built upon existing classes.
2-1OB to 2-15B
Part-2 : Interfaces: Defining an Interface, .... 3 When you inherit from an existing class, you can reuse methods and
Implementing Interface, Differences fields of parent class, and you can add new methods and fields also.
Between Classes and Interfaces 4 InJava, a class can inherit from another class using the 'extends' keyword.
and Extending Interfaces,
5 The class that is being inherited from is called the superclass, and the
Object Cloning, Inner Classes class that inherits from it is called the subclass.
Packages: Defining Package, 2-15B to2-19B
Part-3 : 6 The benefits of inheritance include code reusability, flexibility, and
CLASSPATH Setting for Packages, modularity.
Making JAR Files for Library Terms used in Inheritance:
Packages, Import and Static
Import Naming Convention for
l Class : Aclass is a group of objects which have common properties. It is
Packages, Networking java.net a template or blueprint from which objects are created.
Package 2 Subclass/Child Class : Subclass is a class which inherits the other
class. It is also called a derived class, extended class, or child class.
Superclass/Parent Class :Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
4. Reusability :It is a mechanism which facilitates to reuse the fields and
methods of the existing class when we create a new class. We can use
the same fields and methods already defined in previous class.
Que 2.2. What are the different forms of inheritance in Java ?
Explain.
2-1 B (MCA-Sem-2)
2-3 B (MCA-Sem-2) 2-4 B (MCA-Sem-2)
Object Oriented Programming Inheritance, Interfaces and Packages
OR Fig. 2.2.3 is a diagram illustrating muitilevel inheritance:
of inheritance in Java with
Explain in detail about various types
neat diagram. Class A

Answer
Following are the differeut forms of inheritance in Java: Class B
1. Single Inheritance :
from only one
i. In single inheritance, a class is allowed to inherit only. Class C
class, i.e., one sub class is inherited by one base class
ii Fig. 2.2.1l is a diagram illustrating single inheritance : Fig. 223.
V.
In Fig. 2.2.3,Class Binherits the members or methods of Class A.
Class A Vi. Class C inherits the members of Class B.
vü. Therefore, Class A is the parent class of Class B and Class B is the
parent of Class C.
Class B viü. Hence, Class C implicitly inherits the properties of Class A along
with Class B.
Fig. 2.2.1. 4 Hierarchical Inheritance :
ii. In Fig. 2.2.1, Class A is a parent class and Class B is a child class. In this type of inheritance, more than one sub class is inherited
Class B inherits all the properties of the Class A. from a single base class, i.e, more than one derived class is
Multiple Inheritance (through interfaces) : from a single base class.
created
i. In multiple inheritance a class can inherit from more than one This creates a bierarchy of classes with multiple subclasses at the
classes, i.e., one sub class is inherited from more than one base same level.
classes.
. Java does not support multiple inheritance of classes, but it does
iüi. Each subelass inherits properties and methods from the same
superclass.
support nmultiple inheritance through interfaces.
An interface can be implemented by multiple classes, allowing them iv. Fig. 2.2.4 is a diagram illustrating hierarchical
inheritance :
toinherit properties and methods from the same interface.
Class A
iv. Fig.2.2.2 is a diagram illustrating multiple inheritance:
Class A Class B
Class B Class C

Class C Fig. 2.2.4.


V
In Fig. 2.2.4, Class B and Class C inherit a
Fig. 2.2.2. Class A. single parent class named
5.
Multilevel Inheritance : Hybrid (Virtual) Inheritance:
i. In this type of inheritance, a derived class is created from another Hybrid inheritance is implemented by combining more
derived class. type of inheritance. than one
ii. This creates a more complex hierarchy of
levels of inheritance. classes with
multiple
Hybrid nheritance is the combination of both single and
inheritance. multiple
ii. Each subelass inherits properties and methods ii. Hybrid inheritance is also not
superclass and all its ancestor classes. from its direct through interface we can achievedirectly
this. supported in Java; only
2-5 B (MCA-Sem-2) 26B (MCA-Sem-2)
Object Oriented Programming Inheritance, Interfaces and Packages
hybrid inheritance: 6 To access a protected mnember from a
iv. Fig. 2.2.5 is a diagram illustrating keyword. subclass, we can use the super
7. Protected members are useful when we want to expose
Class A some
functionality to subclasses and other classes within the same package,
Single while still keeping certain details hidden from the rest of the
Inheritance program.
Class B Que 24. Explain about 'super keyword in inheritance.
Hierarchical
Inheritance Answer
Class C Class D Hybrid 1 In object-oriented programming, the 'super keyword is used to refer to
Inheritance the immediate parent class of a subclass, also known as the
superclass.
Fig. 2.2.5. 2 It is commonly used in inheritance relationships to call methods and
the properties constructors from the superclass.
In Fig. 2.2.5, Class A is a super class. Class B inherits 3 Following are some common use cases of the super keyword :
of Class A.
inheritance. Calling the superclass constructor:Whena subclass is created,
vi. Since Class B and Class A represents single its constructor can call the constructor of the supercass using the
vii. Further, Class B is inherited by Class C and Class D. Thus, Class B 'super(y method. This allows the subeiass to inherit the properties
becomes the parent class for Class Cand Class D.
and behavior of the superclass.
classes represent the hierarchical inheritance. Combinedly,
vii. These ii. Accessing superclass methods : A subelass can call a method
it denotes the hybrid inheritance. defined in its superclass using the 'super keyword. This is useful
Que 2.3. Discuss protected members in Java. when the subclass needs to add functionality to a methoddeñned in
the superclass.
Answver iii. Accessing superclass variables:A subelass can access âvariable
defined in its superelass using the 'super keyword. This is useful
1 In Java, protected access modifier is used to declare a member (variable when the subclass needs to modify the value of avariable defined
or method) of a.class that is accessible within the class, its subclasses, in the superclass.
and other classes in the same package.
2 Protected members are less restrictive than private members, which Que 2.5.Eplain how a constructor is called in subclasS and
can only be accessed within the lass they are declared in. superclass.
3 On the other hand, public members are more permissive and can be
accessed from anywhere in the program. Answer
4. Following is an example of a class with a protected member: 1 In Java, when a subclass is created, is constructor is called after the
package com.example; constructor of the superclass.
public class MyClass ( 2. This is because a subclass inherits all of the members of its superclass,
protected int myProtectedInt; including its constructors.
constructor of its
public void doSomething() { 3. When a subclass constructor is called, it first calls the
subelass to initialize
Il code here superclass using the super keyword, which allows the
the inherited members of the superclass.
other necessary
4. Then, it initializes its own members and performs any
5 In this example, the 'myProtectedInt' member variable is declared as operations.
how constructors are called in a
protected, which means it can be accessed by other classes in the 5. Here is an examnple that illustrates
"com.example' package as wellas any subclasses of MyClass'. subclass and superclass :
2-7B (MCA-Sem-2) 2-8 B (MCA-Sem-2)
Object Oriented Programming Inheritance, Interfaces and Packages
I/Create a super class. Que 2.6. Discuss in detail about "Object class in Java.
class A
Answer
A) 1 In Java, the Object class is the root class of the class hierarchy.
2. All other classes in Java extend or inherit from the Object class, either
System.out.printin("Inside A's constructor."); directly or indirectly.
3. This means that every object in Java is an instance of the Object class,
and can access the methods defined in the Object class.
) 4. Following are some of the key features and methods of the Object class
I/Create a subclass by extending class A. in Java:
class B extends A i. equals(Object obj) : This method is used to determine whether
two objects are equal. By default, it checks whether two objects
B)
have the same memory address.
ii. hashCode() : This method returns a hash code value for an object.
System.out.println("Inside B's constructor.; The hash code is used for hashing algorithms and data structures,
such as hash tables. It is defined as an integer value, and must be
consistent with the equals() method.
iii. toString) : This method returns a string representation of an
object. By default, it returns the name of the class and the object's
I/Create another subclass by extending B.
class C extends B hash code.
iv. getClass() : This method returns the class object for an object. The
class object contains information about the object's class, such as its
CO name and superclass.
System.out.printÉn("Inside C's constructor."); V. wait), notify), and notifyAll):These methods are used for
inter-thread communication and synchronization. They allow
threads to wait for a signal or notification fromn another thread, and
to notify other threads when a condition has been met.
vi. finalize(): This method is called by the garbage collector before an
class CallingCons finalization
object is destroyed. It can be used to perform cleanup or
tasks, such as releasing resources or closing files.
public static void main(String args) The copy is a new
vi. clone() : This method createsa copy of anobject.but witha different
object with the same state as the original object, protected, and
Cc = new C0;
memory address. However, the clone() method is
used.
must be overridden in a class to be
Java ? Ilustrate with an
Que 2.7. What is an abstract class in
Output:
Inside A's constructor example to demonstrate abstract class.
Inside B's constructor
Answer directly.,
Inside Cs constructor class that cannot be instantiated
1
In Java, an abstract classtoisbea used.
and must be subclassed
2-9 B (MCA-Sem-2) 2-10B (MCA-Sem-2)
Object Oriented Programming Inheritance, Interfaces and Packages
a common interface or set of
2 Abstract classes are useful for creating
multiple subclassOs, while still
behaviors that can be shared acro88 implemontation. PART-2
allowing for each subelass to have its own unique Interfaces :Defining an Interface, Implementing Interface,
3. An abstract class is declarod by using abstract keyword. It can have iuDifferences Between Classes and Interfaces and Extending
abstract and non-abstract methods (method with body). Interfaces, Object Cloning, Inner Classes.
4 Here's an example of an abstract class in Java :
public abstract clas8 Animal (
private 8tring name; Questions-Arswers
publicAnimal(8tring name) ( Long Answer Type and Medium Answer Type Questions
this.name name;
)
public String getName() (
roturn name; Que 2.9,. Write a note on interfaces and present the syntax for
defining an interface.
public abstract void makeSound(); Answer
1. An interface in Java is a blueprint of aclass. It has static constants and
Explain about final' keyword in
Java. abstract methods.
Que 2.8. 2. The interface in Java is a mechanism to achieve abstraction.

Answer 3 There can be only abstract methods in the Java interface, not method
The inal' keyword is apowerful tool for creating constants, enforcing body.
1 programs, It is used to achieve abstraction and multiple inheritance in Java.
immutability, and preventing unexpected behavior indava 4

used with parameters to indicate that thev 5 There are mainly three reasons to use interface
2 Thefinal' keyword can be method.
It is used to achieve abstraction.
cannot be modified withina i.
a cateh block to ensure that an By inter face, we can support the functionality of
multiple
3 The final' keyword can also be used with ii
exception iscAught and not rethrown. inheritance.
following three ways: iii. It can be used to achieve loose coupling.
4 We can use final key word in keyword indieot keyword.
Constants:Whon usod with avariable, the inal' 6 An interface is declared by using the înterface'
changed after it is initiali methods in an interface are
that the variable's value cannot be
that can be Used throughout th
7 It provides total abstraction; means all the static and
This creates a constant variable changes. declared with the empty body, and all the fields are public,
the risk of aceidental
Preventwithout
ii. program inheritance: When used with a class, the "final' keyword final by default.
must implement all the methods
cannot be subcla88ed or
8 A class that implements an interface
indicates that the class inherited.
modifying the behavio.
This declared in the interface.
prevents other classes from Syntax:
implementation of the final class.
overriding: When used with a method, intorface <interface_name>
iii. Prevent method
that the method cannot be the
'final' keyword
subclasses. This
indicates
ensure that the method's behavioroverridden
by
remains I/declare constant fields
program.
consistent throughout the / declare methods that abstract
/ by default.
2-11 B(MCA-Sem-2) 2-12 B(MCA-Sem-2) Inheritance, Interfaces and Packages
Object Oriented Programming
how to Answer
interface ? With an example explain Simple Interface:
Que 2.10. What is A
interface. A simple interface in Java is an interface that is deciared in its own
define and implement OR
1.
separate file.
example.
implemented in Java with an Here's an example of asimple interface:
Outline how interfaces are 2.
public interface Animal
and/or
Answer defines a set of methods public void eat();
interface is a contract that
1 In Java, an implement. public void sleep0;
constants that a class must an implementation
provide
Aclass that implements an interface agrees to
2
for all the methods
declared in the interface. In this example, Animal' is a simple interface that declares two methods,
implements keyword, 'eat()' and 'sleep(0.
interface in Java, you use the
3 To implement an interface. Any class that implements the 'Animal' interface must provide an
followed by the name of the 4.
implementation for both of these methods.
4. Here's an example: B. Nested Interface:
public interface Animal { declared inside another
1 A nested interface in Java is an interface that is
public void eat); class or interface.
public void sleep(0; 2 Nested interfaces are useful for organizing related interfaces.
3 Here's an example of a nested interface :
Animal {
public class Dog implements public class OuterClass {
public void eat(){ public interface InnerInterface f
System.out.println("Dog is eating"); public void doSomething ):

public void sleep(0{ Innernterface


System.out.println(Dog is sleeping"); public class MyClass implements OuterClass.
public void doSomething) {
System.out.printn(Doing something!);
called ('Animal' that declares
we define an interface
5. In the above example, 'sleep()'.
'eat()' and
two methods, intevfoa contains a nested interface called
We also define aclass called Dog that implements the 'Animal' 4 ln this example, OuterClass
6 both the eat)' and 'sleep()' methed
byproviding an implementation for InnerInterface'.
OuterClass.InnerInterface', which
an interface, it must provide an implement 5 The My Class' class implements
When a class implements the interface. implementation for the 'doSomething)' method
7.
for allthe methods declared in means it must provide an
provides animplementation for both the eat() declared in the interface.
containing class or
In this example, Dog' interface from outside the
8 and 'sleep()' methods.
Once a class implements an interface, you can create an instance ofthe
6 To access a nested to specify the outer class or interface
interface.
lnterface, we usse the dot notation
9 an instance of the followed by the interface name.
class and use it as
comparison between classes and
Que 2.11. Explain simple interface and nested interface in Java ue 2.12. Present a detailed
with examples.
interfaces. OR
interface.
Oterentiate between class and
2-14B MCA-Sem-2)
Object Oriented Programming 2-13 B (MCA-Sem-2) Inheritance, Interfaces and Packages

Answer
Que 2.14. Describe extending interfaces in Java with example.
S.No. Feature Classes Interfaces Answer
1. Definition Ablueprint for creating A contract for 1 An interface can extend another interface, which means that the child
objects. implementing behavior. interface inherits all the methods from the parent interface and can add
additional methods of its own. This is known as extending interfaces.
2 Fields Can contain instance andCannot contain instance or One interface can inherit another by use of the keyword 'extends'.
static fields. static fields. 2
3 Following is an example :
3 Methods Can contain concrete and Can only contain abstract public interface Animal{
abstract methods. methods. m
public void eat();
4 Access Can use public, protected, Can use only public and
modifiers private, and default. default access modifiers. public void sleep0;
}
5. Constructor Can have constructors. Cannot have constructors. public interface Pet extends Animal
6 Inheritance Can be extended by otherCan be implemented by public void play);
classes. classes or extended by
other interfaces.
7. Multiple Does not support multiple Supports multiple
4 In the above example, we have two îmterfaces: 'Animal' and 'Pet.
inheritance inheritance. inheritance through 5. Pet' extends 'Animal, which means it inherits the 'eat()' and »sleep(
multiple interface methods from 'Animal'.
|implementation. 6 In addition to the methods inherited from 'Animal, Pet' also adds anew
method called 'play)'.
Que 2.13. Differentiate betweenabstract class and interface. 7 Any class that ìmplements the Pet' interface must provide an
implementation for allthree methods: "eat(), sleepo and jplay().
Answer
Que 2.15.Explain object cloning in Java. Give its advantages and
S.No. Abstract Class Interface disadvantages.
1 Abstract class can have Interface can have only abstraot
abstract and non-abstract methods. Answer
methods. 1 Object cloning in Java is the process of ereatinga duplicate of an existing
2 Abstract class doesn't supportIntertace supports multinle object.
multiple inheritance. inheritance. 2 In Java, object cloning can be performed using the 'clone()' method,
3 Abstract class can have final, Interface has only which is defined in the Object' class.
non-final, static and non- variables. static and final 3 To enable cloning of an object, the class must implement the
static variables. Cloneable interface, which is a marker interface indicating that the
object can be cloned.
Abstract class can provide Interface can't
fa class attempts to clone an object without implementing the Cloneable'
4
the implementation of implementation of provide 4
interface. abstract class.the 5.
interface, it will throw a CloneNotSupportedException'.
Syntax of the clone() method :
5 The abstract keyword is usedThe interface
to declare abstract class. declare interfacekeeyword is used to protected Object clone() throws Clone NotSupportedException
-16B(MCA-Sem-2) Inheritance, Interfaces and Packages
Object Oriented Programming 2-15B (MCA-Sem-2)
Advantages of object cloning :
1. Object cloning provides away to create copies of objects without requiring
Questions-Answers
the original object to be modified. TongAnswer Type and Medium Answer Type Questions
2
Object cloning can improve performance in situations where it is
expensive to create a new object from scratch.
Disadvantages oB object cloning : Que 2.17. Explain in detail Ithe term package in Java.
1
Object cloning can be error-prone and can lead to unexpected behavior
if not implemented correctly.
Answer
2.
Object cloning can be slow for large objects or objects with complex In Java, a package is a way to group related classes, interfaces,
state. 1
enumerations, and annotations.
3.
Cloned objects can have references to the same objects as the original Apackage is a collection ofrelated classes that are stored together in a
object, which can lead to unexpected side effects if the referenced objects 2.
are modified. single directory or folder.
3. The main purpose of packages in Java is to provide a way to organize
Que 2.16. Explain about inner classes in Java. code and prevent naming conflicts.
and manage
Answer 4 By organizing classes into packages, it is easier to locate
conflicts between
code, and it also reduces the likelihood of naming
1 Inner classes in Java are classes that are defined inside another class, classes that have the same name.
beginning of a
2. Inner classes are used when a class needs to be closely tied to its 5. Packages are defined using the package' keyword at the
containing class, or when aclass is only needed within the context of its Java source file.
containing class. follows:
3 There are four types of inner classes in Java: 6 The syntax for defining a package is as
i. Non-static inner class :A non-static inner class, also known as an package com.example.mypackage;
inner class or a member class, is defined inside another class without com.example.mypackage is the name of the package.
the 'static' modifier. Non-static inner classes can access the members of In this example, using the
you need to import the package
their containing class, even if they are private. 7 To use a class froma package,
import' statement.
ii. Static nested class:A staticnested class is a class that is defined in eid is as tollows:
another class with the 'static' modifier. Static nested classes cx 8. The syntax for importing a package
import com.example.mypackage.MyClass;
instantiated without an instance of the containing class and can n
access the static members of their containing class. 'com.example.mypackage.MyClass'is the fully qualified
iii. Local inner class : Alocal inner class is a class that is defined In this example, that you want to import.
method or a block of code. Local inner classes can access the inside a name of the class
as java.lang', which
effectively final variables of the containing method or block final or predefined packages, suchand java.util', which
9 Java provides several of the Java language,
iv. Anonymous inner class : An anonymous inner class is a class that contains the core classes working with collections, dates, and other
is contains utility classes for
defined without a name and is used for implementing
extending classes. Anonymnous inner classes can only be interfaces data structures.
custom packages.
once and cannot be reused. instántiated 10. Java also allows
vou to ereate your own
a directory with the
package, you need to createfiles in that directory.
create a custom source
PART-3 4 To and place your Java
name of the package
CLASSPATH Setting for in Java.
Packages8 : Defining Package, CLASSPATH setting for packages
StaPPatic cImport
kages,
Import and Explain
Making JAR Files for Library Packages,
Networking, java.net
ue 2.18.
Naming Convention for Puckages,
Package.
2-18B(MCA-Sem-2) Inheritance, Interfaces and Packages
Object Oriented Programming 2-17B (MCA-Sem-2)

Que 2.20.
Write a short note on: import and staticimport.
Answer
1 In Java, the CLASSPATH is an environment variable that specifies the Answer
location of Java class files that the Java Virtual Machine (JVM) uses to Import :
run Java applications. A and
In Java, the import statement is used to bring classes, interfaces,
The CLASSPATH setting is used to tell the JVM where to look for other types into a Java source file, allowing you to use them without
2. 1
classes when they are referenced in code.
fully qualifying their names.
3 When a Java program is compiled, it is converted into bytecode, which is the java.util
For example, if you want to use the Scanner class from
2 follows :
package in your code, you can use the import statement as
stored in .class fles.
4. These files need to be located by the JVM when the program is executed. import java.util.Scanner;
5.
The CLASSPATH setting can be set using an operating system prefixing it with the
3. This allows you to use the Scanner' class without
environment variable or specified on the command line. full package name.
6 It can include directories, JAR files, and ZIP archives containing class
files. B. Static Import :
the import statement that
7 The static import statement is a variation of
For example, if you have aclass called MyClass' in a file called 1. class, such as static fields and
MyClass.class' located in the directory C:\java\classes', you can set the allows you to import static members of a
static methods.
CLASSPATH environment variable as follows on Windows :
constant from the java.lang. Math
set CLASSPATH=C:\java\classes 2. For example, ifyou want to use the PI import statemnent as follows:
can use the static
8. This willtell the JVM to look for classes in the 'C:\java\classes' directory. class in your code, you
import static java.lang. Math. PI;
directly without prefixing it with
Que 2.19. Explain how to make JAR files for library packages ? 3. This allows you touse the PI constant
the class name.
Answer
by naming convention for
1 In Java. a JAR(Java Archive) file is a file format used to bundle
multiple Que 2.21. What do you understand
Java class files and associated resources into a single file. packages ?
2. JAR files are commonly used to distribute libraries and frameworks
making it easy for developers to use and manage the code Answer
packages are guidelines for naming
3 To make a JAR file for a library package in Java, you can
follow these 1 In Java, naming conventions for understandable way.
Java packages in consistent
a and
general steps: helps make Java code more
Step 1: Create a directory for the JAR file and change to that 2 Following a standard naming convention helps avoid naming conflicts
and
directory.
Step 2: Compile the Javasource code into classfiles s using the javac readable, maintainable, and portable,
Step 3: Create a manifest file that specifies the main claass and any compiler. and errors. that package
for packages recommends domain
configuration options. The manifest file must be named other 3 The Java naming convention
lowercase and should be in reverse
name
placed in the META-INF directory. MANIFEST.MF
Step4 : Package the class files and the manifest file into a JAR file
and names should be all
order, separated by periods.
package name is unique and globally
jar' command. using the 4 This convention ensures that eachnaming conflicts between packages from
identifiable, and helps prevent
Step 5: Optionally, you can sign the JAR file usingtthe
to ensure the integrity and authenticity of the code. jarsigner command different organizations or developers.
organization's domain name is
'example.com' and
Once you have created the JAR file, you can
5 For example, if your library', the package name
4
developers who can then include it in their Java
distribute it to other you have a package for your library named
classes and resources it contains.
projects and use the should be com.example.library'.
Object Oriented Programming 2-19 B (MCA-Sem-2)
6 This naming convention makes it clear which organization the package
belongs to and avoids conflicts with other packages that might have the
same name.

7. In addition, the naming convention recommends using short, descriptive


names for packages that reflect their contents or purpose.
8 This makes it easier for developers to understand what each package
contains and helps make the code more maintainable.
Que 2.22. Discuss about networking ljava.net' package.

Answer
1.
The java.net' package in Java provides a set of classes and interfaces for
networking programming, including support for TCP/TP sockets, UDP
sockets, and URL connections.
2. This package is widely used for developing networked applications in
Java.
3. Some of the key classes and interfaces in the java.net package include:
i. Socket:Represents a client-side TCP/IP socket connection to a
server. This class provides methods for opening and closing the
connection, reading and writing data, and setting socket options.
ii. ServerSocket: Represents aserver-side TCP/IP socket that listens
for incoming connections. This class provides methods for
new connections, reading accepting
and writing data, and setting socket
options.
iüi. Datagram Socket : Represents a socket for sending and receiving
UDP datagrams. This class provides methods for sending and
receiving datagrams, setting socket options, and joining multicast
groups.
iv. URL:Represents a Uniform
toa resource on the Internet orResourc Locator (URL) that points
a local network. This class provides
methods for opening connections to the resource and
from it. reading data
V.
URLConnection : Represents a connection to a URL
This class provides methods for reading and
resource, setting request properties, and writing resource.
data to
the
4.
handling authentication.
Using the classes and interfaces in the java.net'' package,
a wide range of networked applications, including web you can develop
peer-to-peer applications,and more. clients, servers,
For example, you can use the 'Socket' and
5.
ServerSocket'
implement a simple client-server application or the classes
'URLConnection' classes to access web resources over HTTp URL' andto
Object Oriented Programming SQ-8B MCA-Sem-2)
SQ-7B (MCA-Sem-2) 2 Marks Questions
2.6. What is interface 2

2 UNT
Inheritance, Interfaces
and Packges
(2 Marks Questions)
AnS In Java, an interface is a
or constants that a class contract that defines a set of methods and/
2.7. Describe the uses of
Ans. Following are sone of the
must implement.
interfaces in Jaya.
1. Defining contracts : main uses of interfaces in Java:
Interfaces can be used to define contracts
between different components of a Java program.
2. Encapsulating behavior : Interfaces can be used to
the behavior of a class. encapsulate
3. Enforcing standärds : Interfaces can be used to enforce
2.1. Discuss protected members in Java. programming standards across a project or
Ans. In Java,
protected access 4. Supporting multiple implementations :organization.
(variable or modifier is used to declare a member
method) of a class Iaterfaces can be
tosupport multiple impleméntations of a particular behavior. used
subclasses, and other classesthatin istheaccessible within the class, its
same package. Protected 5. Facilitating polymorphism : Interfaces ean be used to facilitate
members are less restrictive than
be accessed within the private nmembers, which can only polymorphism in Java programs.
class they are declared in.
2.2. 2.8. Distinguish between class and interface.
Ans.
Exemplify
In
the use of 'super
keyword. Ans.
object-oriented
refer to the immediateprogramming, the 'super' keyword is used to S.No. Feature Classes Interfaces
parent class of a subclass, also
superclass. It is commonly used in inheritance known as the 1 Definition Abiueprint for ereating Acontract for
methods and constructors from the
superclass. relationships to call objects implementing behavior.
2.3. What is class hierarchy in Java ? 2. Fields Can containinstance and Cannot containinstance or
Ans. In Java, aclass static felds static fields.
hierarchical tree hierarchy is a way
structure based of i
on their classes into a
Inheritance is a
class to inheritfeature object-oriented
of relationships. 2.9. Outline the use of 'extends' keywordin Java with syntax.
and methodsprogramming
a that allows
properties
class hierarchy in Java is from another class. The
Ans nJava, the extends keyword is used to ereate a subclass that
inherits the properties and behaviors of asuperclass. The syntax
Object class at typically
the root and other represented tree,
as a with the for using 'extends' is as follows :
classes in Java are classesbranching off from it. All
either direct or indirect class ChildClass extends ParentQlass
Object' class. descendants of the WChildClass body
2.4. Discuss about
Object'
Ans. In Java, the Object class isclass in Java.
2.10. What is extending interfaces in Java ?
other classes in Java extendthe root class of theclass
Objecthierarchy.
or inherit from the All Ahs: An interface can extend anbther interface, which means that the
directly or indirectly. This means class, either child interface inherits all the methods from the parent interface
that
instance the Object class, and can access
of every object in Java is an and can add additional methods of its own. This is known as
the Object class. the methods defined in extending interfaces.
2.5. Discuss. final' keyword in 2.11. What is object cloning ?
Ans. The 'final' keyword is a Java. Ans: Object cloning in Java is the process of creatinga duplicate of an
powerful tool for creating
enforcing immutability, and constants, existing object. In Java, object cloning can be performed using the
Java programs. preventing unexpected behavior in clone()' method, which is defined in the Object class.

2.12. What are inner classes in Java ?


sQ-10B (MCA-Sem-2)
2 Marks Questions
Object Oriented Programming SQ-9 B (MCA-Sem-2)
ASS n Java, the import statement is used to
Ans. Inner classes in Java are classes that are defined inside another andother types into a Java source file, bring classes, interfaces,
class. Inner classes are used when a class needs to be closely tied to without fully qualifying their names. allowing you to use them
its containing class, or when a class is only needed within the context
of its containing class. 2.18. What is static import statement in Java ?
AsE The static import statement is a variation of the import
2.13. Explain the term package' in Java. that allowsyou to import static nembers of a class, suchstatement
as static
Ans. In Java, a package is a way to related classes, interfaces, fields and static methois.
enumerations, and annotations. age is a collection of related
classes that are stored together in a single directory or folder. The 2.19. What do you meaa by naming convention for packages ?
main purpose of packages in Java is to provide a way to organize An. n Java, naming conventions for packages are guidelines for naming
code and prevent naming conflicts. Java packages in a consistent and understandable way. Following a
standard naming convention heips make Java code more readable,
2.14. Write the syntax for importing packages ina Java source maintainable, and portable, and heips avoid naming confliets and
file and give an example. errors.
Ans. Syntax : The syntax for importing packages in Java is as follows :
import package.name.ClassName:
2.20. Discuss about networking java.net package.
Example : Following is an example of how to import the java.util
package and use the ArrayList class from that package : Ans: The java.net' package in Java provides aset ofciases and interfaces
import java.util.ArrayList;
for networking programming, inciuding support for TCPIP sockets,
UDP sockets, and URL connections. This package is widely used
public class MyArrayList { for developing networked applications in Java.
public static void main(Stringi]args) {
ArrayList<String> myList = new ArrayList<String>); e s
myList.add(apple");
myList.add(banana");
System.out.println(myList);

1. In the above example, we import the java.util.ArrayList' class using


the import statement, and then create an instance of 'ArrayList
called 'myList'.
2. We add some elements to 'myList' and then print it to the console.
2.15. Explain CLASSPATH setting for packages in Java.
Ans. In Java, the CLASSPATH is an environment variable that specifies
the location of Java class files that the Java Virtual Machine (JVM)
uses to run Java applications. The CLASSPATH setting is used to
tellthe JVM where to look for classes when they are referenced in
code.

2.16. What are JAR files in Java ?


Ans. In Java, a JAR (Java Archive) file is a file format used to bundle
multiple Java class files and associated resources into a single file.
JAR files are commonly used to distribute litbraries and frameworks,
making it easy for developers to use and manage the code.
2.17. What is import statement in Java ?

You might also like