0% found this document useful (0 votes)
5 views4 pages

Chapter 3

Uploaded by

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

Chapter 3

Uploaded by

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

Chapter 3

1.List the types of inheritances in Java.


Ans.
Types of inheritances in Java:
1.Single level inheritance
2.Multilevel inheritance
3.Hierarchical inheritance
4.Multiple inheritance
5.Hybrid inheritance

2.Differentiate between method overloading and method overriding


Ans.

Sr.no. Method overloading Method overriding


1. Overloading occurs when two or more Overriding means having two methods
methods in one class have the same with the same method name and
method name but different parameters. parameters (i.e., method signature)

2. In contrast, reference type determines The real object type in the run-time,
which overloaded method will be used not the reference variable's type,
at compile time. determines which overridden method
is used at runtime

3. Polymorphism not applies to Polymorphism applies to overriding


overloading
4. overloading is a compiletime concept. Overriding is a run-time concept

3.Describe final variable and final method


Ans.
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.
Syntax:
final void findAverage()
{
//implementation
}
4.Example of declaring a final method:
Cl4ass A
{
final void show()
{
System.out.println(“in show of A”);
}
}
class B extends A
{
void show()
// can not override because it is declared with final
{
System.out.println(“in show of B”);
}
}

Final variable:
1. The value of a final variable cannot be changed.
2. Final variable behaves like class variables and they do not take any space on
individual objects of the class.
3. Example of declaring final variable:
final int size = 100;

5.Explain dynamic method dispatch in Java with suitable example.


Ans.
1. Dynamic method dispatch is the mechanism by which a call to an overridden
method is resolved at run time, rather than compile time.
2. When an overridden method is called through a superclass reference, Java
determines which version (superclass/subclasses) of that method is to be executed
based upon the type of the object being referred to at the time the call occurs. Thus,
this determination is made at run time.
3. At run-time, it depends on the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be
executed.
4. A superclass reference variable can refer to a subclass object.
5. This is also known as upcasting.
6. Java uses this fact to resolve calls to overridden methods at run time.
7. Therefore, if a superclass contains a method that is overridden by a subclass, then
when different types of objects are referred to through a superclass reference
variable, different versions of the method are executed.
6.Differentiate between class and interfaces.
Ans.

Sr.no. Class Interfaces


1. Doesn’t Supports multiple Supports multiple inheritance
inheritance
2. “extend ” keyword is used to inherit ”implements ” keyword is used to inherit

3. class contain method body interface contains abstract method(method


without body)
4. contains any type of variable contains only final variable
5. can have constructor cannot have constructor
6. can have main() method cannot have main() method
7. yntax : Syntax:
Class classname Inteface Innterfacename
{ {
Variable declaration, Final Variable declaration,
Method declaration abstract Method declaration
} }

7.List any four Java API packages.


Ans.
1. java.lang
2. java.util
3. java.io
4. java.awt
5. java.net
6. java.applet

8.Define package. How to create user defined package? Explain with example
Ans.
1. Java provides a mechanism for partitioning the class namespace into more
manageable parts.
2. This mechanism is the package.
3. The package is both naming and visibility controlled mechanism.
4. Package can be created by including package as the first statement in java source
code.
5. Any classes declared within that file will belong to the specified package.
6. Package defines a namespace in which classes are stored.
7. Syntax:
package pkg; Here, pkg is the name of the package
8. Example:
package mypack;
9. Packages are mirrored by directories.
10. Java uses file system directories to store packages.
11. 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.
12. The directory must match with the package name exactly.
13. A hierarchy can be created by separating package name and sub package name by
a period(.) as pkg1.pkg2.pkg3; which requires a directory structure as pkg1\pkg2\
pkg3.
14. Syntax: To access package In a Java source file, import statements occur
immediately following the package statement (if it exists) and before any class
definitions.
15. Syntax: import pkg1[.pkg2].(classname|*)

You might also like