Inheritance is a mechanism in which the derived class borrows the properties of the base class.
Two Marks Questions with Answers
Q.1 Define the term inheritance.
Ans.
Inheritance is a mechanism in which the derived class borrows the properties of
the base class.
Q.2 Explain the use of extend keyword with suitable
example.
Ans;
• The
extend keyword is used in inheritance.
• When
the child class is derived from its parent class then the keyword extends is
used.
Q.3 What is the difference between superclass
and subclass?
Ans. :
• The
superclass is a parent class or base class from which another class can be
derived.
• The subclass is always a derived class which
inherits the properties of the base class or superclass.
• The
superclass is normally a generalized class whereas the subclass is normally for
specific purpose.
Q.4 Enlist various forms of inheritance.
Ans.
Various forms of inheritance are -
1.
Single level inheritance
2.
Multiple inheritance
3.
Multi-level inheritance
4.
Hierarchical inheritance
5. Hybrid inheritance
Q.5 What is the use of super keyword ?
Ans. The
super class is used to access immediate parent class from the subclass. It is
used to
1.
access parent's variable,
2.
access parent's method
3.
access patent's constructor invocation
Q.6 Differentiate between inheritance and
polymorphism.
Ans. :
Q.7 Distinguish between static and dynamic
binding.
Ans.
Static binding is a type of binding in which the function call is resolved at
compile time. The dynamic binding is a type of binding in which the function
call is resolved at run time. The static binding is called the early binding
and the dynamic binding is called late binding.
Q.8 What is the purpose of 'final' keyword ?
Ans. The
keyword 'final' is used to avoid further modification of a variable, method or
a class. For instance if a variable is declared as final then it can not be
modified further, similarly, is a method is declared as final then the method
overriding is avoided.
Q.9
Define inheritance hierarchy.
Ans. The
inheritance hierarchy represents the collection of classes the inherit from
their base classes and thereby make use of the code present in the base class.
The data reusability can be achieved by inheritance hierarchy. For example
Q.10 How to prevent inheritance?
Ans. If
we declare particular class as final then no class can be derived from it. For
example
final
class Test
{
………..
………
……….
}
class
Test1 extends Test
{
………..
………
……….
}
The
above code will produce an error" cannot inherit from final Test".
Thus the
use of keyword final for the class prevents the inheritance.
Q.11 Write a class declarations for the
following relationship, assuming that all classes are public: a Bulldog is a
kind of dog, and a Dog is a kind of Animal.
Ans;
class
Animal
{
………….
}
class
dog extends Animal
{
………...
}
class
Bulldog extends dog
{
…………..
}
Q.12 What is meant by dynamic method dispatch?
Ans. The
dynamic method dispatch is run time polymorphism in which a call to overridden
function is resolved at runtime instead of at compile time. Hence is the name.
For example
class
Test1
{
public
void method1()
{ }
}
class
Test2 extends Test1
{
public
void method1()
{ }
}
void
main()
{
Test1
obj=new Test2();
obj.method1();
}
Q.13 Can an abstract class be final? Why?
Ans. The
abstract class cannot be final because once we declared the abstract base class
with the keyword final then it can not be inherited.
Q.14 Can an abstract class in Java be
instantiated? Give the reason.
Ans. The
abstract class can not be instatiated (i.e. we can not create the object of
this class using new operator) because the abstract class is very much general
and less specific. It does nothing and simply lists out only common features of
other classes.
Q.15 Why
is multiple inheritance using classes a disadvantage in Java?
Ans. In
multiple inheritance, the child class is derived from two or more parent
classes. It can lead to ambiguity when two base classes implement a method with
the same name. Q.16 State the condition for method overriding in Java
Ans. Method
overriding occurs only when the name of the two methods(method in super class
and method in subclass) are same and their type signature is same.
Q.17 What is package?
Ans.:
Package represent a collection of classes, methods and interfaces. The name of
the package must be written as the first statement in the Java source program.
The syntax of specifying the package in the Java program is
package
name_of_package
Due to
package the systematic arrangement of classes is possible. All standard classes
in Java are stored in named packages.
Q.18 Mention the necessity for import
statement.
Ans. The
import statement is used to refer the classes and methods that are present in
particular package. This statement is written at the beginning of any Java
program. For example -
import
java.io.*
This
statement allows to use the useful functionalities for performing input and
output operations.
Q.19 List any four packages in java and
highlight their features.
Ans. :
Q.20 What is API package?
Ans.: • The
API packages are application programming interface packages used by java.
• These packages include important classes and
interfaces which are used by the programmer while developing the java
applications.
• For
example java.io, java.util, java.net and so on are the commonly used API.
Q.21 Explain any three uses of package.
Ans. :
1.The
classes defined in the packages of other program can be easily reused.
2. Two
classes from two different packages can have the same name. By using the
package name the particular class can be referred.
3.
Packages provide the complete separation between the two phases- design and
coding.
Q.22
Explain the term CLASSPATH.
Ans. The
packages are nothing but the directories. For locating the specified package
the java run time system makes use of current working directory as its starting
point. This directory path is called CLASSPATH.
Q.23 What are the ways of importing the java
packages? OR Write the syntax for importing packages in a Java source file and
give an example.
Ans. The
import statement can be written at the beginning of the Java program, using the
keyword import. For example -
import java.io.File
or
import java.io.*
Either a
class name can be specified explicitly or a * can be used which indicated that
the Java compiler should import the entire package.
Q.24 In Java what is the use of interface?
OR What
is interface mention its use.
Ans. In
java, the interface is used to specify the behaviour of a group of classes.
Using interfaces the concept of multiple inheritance can be achieved.
Q.25 Define Interface and write the syntax of
the Interface.
Ans. The
interface can be defined using following syntax access_modifier interface
name_of_interface
{
return_type
method_name1(parameter1,parameter2,...parametern);
…………
return_type
method_name1(parameter1,parameter2,...parametern);
type
static final variable_name=value;
………...
}
The
interface is a collection of various methods that can be used by the class that
is attached to the interface. The interface name must be preceded by the
keyword interface.
Q.26 What modifiers may be used with an
interface declaration ?
Ans. :
An interface may be declared as public or abstract.
Q.27 Why the variables in interface static and
final ?
Ans. The
members of interface are static and final because -
1) The
reason for being static - The members of interface belong to interface only and
not object.
2) The
reason for being final - Any implementation can change value of fields if they
are not defined as final. Then these members would become part of the
implementation. An interface is pure specification without any implementation.
Q.28 What is the purpose of nested interface?
Ans. The
nested interfaces are used to group related interfaces so that they can be easy
to maintain. The nested interface must be referred by the outer interface or
class. It can't be accessed directly.
Q.29 What are the properties of nested interface?
Ans;
1)
Nested interfaces are static by default. You don't have to mark them static
explicitly.
2)
Nested interfaces declared inside class can take any access modifier.
3)
Nested interface declared inside interface is public implicitly.
Q.30 If a class B is derived from class A and
extends the interface myinterface, then how will you write this statement for
class B definition?
class B
extends class A implements myinterface
{
....//body
of class B
}
3
Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : Inheritance, Packages and Interfaces | Object Oriented Programming - Two marks Questions with Answers
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation