0% found this document useful (0 votes)
9 views7 pages

Assignment 2 Solution

The document provides an overview of key concepts in Java including interfaces, inheritance, and packages. It defines interfaces and inheritance, lists types of inheritance, and explains the use of the super keyword. Additionally, it covers method overloading vs. overriding, the final keyword, and provides examples for better understanding.

Uploaded by

chavansauu2700
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)
9 views7 pages

Assignment 2 Solution

The document provides an overview of key concepts in Java including interfaces, inheritance, and packages. It defines interfaces and inheritance, lists types of inheritance, and explains the use of the super keyword. Additionally, it covers method overloading vs. overriding, the final keyword, and provides examples for better understanding.

Uploaded by

chavansauu2700
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

Assignment -2 Unit-2 Inheritance, Interface and Packages

Q.1. Define the interface in java. Write the syntax.


Answer:
An interface is just like a class, which contains only abstract method. The interface
keyword is used to declare or define an interface.
Syntax:
interface interface_name
{
static final return-type var_nm = value;
return-type method_nm(parameter-list);
}

Q.2. Define Inheritance. Enlist types of Inheritance.


Answer:
When we construct a new class from existing class in such a way that the new class
access all the features and properties of existing class called inheritance.
Types of Inheritance:
1.​ Single/Simple Inheritance
2.​ Multilevel Inheritance
3.​ Multiple Inheritance
4.​ Hierarchical Inheritance

[Link] any four inbuilt packages in java..


Answer:
1.​ [Link]
2.​ [Link]
3.​ [Link]
4.​ [Link]

Q.4. List the use of super keyword


Answer:
1.​ Accessing the immediate parent class variable.
2.​ Invoking the immediate parent class method.
3.​ Invoking the immediate parent class constructor.
Q.5. Explain single and multilevel Inheritance with suitable example.
Answer:
a.​ Single inheritance: Single inheritance nothing but which contain only one super
class and only one sub class is called Single inheritance.
Syntax:
public class class_nm1{
…………
}
public class class_nm2 extends class_nm1{
…………
}

​ Example:
​ class student {
​ ​ ​ int rno,marks;
​ ​ ​ string name;
​ ​ ​ void input() {
​ ​ ​ ​ [Link]("Enter marks,roll no., and name: ");
}
​ ​ ​ }
class Ankush extends student {
​ ​ ​ void display() {
​ ​ ​ ​ rno = 20;
​ ​ ​ ​ name = “Ankit”;
marks = 90;​
[Link]("Name: "+name+ “Roll No.: ”+rno+ “Marks: ”+marks);
​ ​ ​ }
}
class Result {
​ ​ ​ public static void main(String[] args) {
​ ​ ​ ​ Ankush obj = new Ankush();
[Link]();
​ ​ ​ [Link]();
​ ​ ​ }
​ ​ }
b.​ Multilevel Inheritance:
In multilevel inheritance we have only one super class and multiple sub classes
called Multilevel Inheritance.
Syntax:
public class class_nm{
……..
}
public class class_nm1 extends class_nm{
…….
}
public class class_nm2 extends class_nm1{
…….
}

Example:
class A{
​ int a,b,c;
​ ​ ​ void add(){
​ ​ ​ a=10; b=20; c = a+b;
​ ​ ​ [Link]("Addition:= "+c);
​ ​ ​ }
​ ​ ​ void sub(){
​ ​ ​ a=200; b=100; c = a-b;
​ ​ ​ [Link]("Substraction:= "+c);
​ ​ ​ }
​ }
class B extends A{
​ ​ ​ void mul(){
​ ​ ​ a=10; b=20; c = a*b;
​ ​ ​ [Link]("Multiplication:= "+c);
​ ​ ​ }
​ ​ ​ ​ void div(){
​ ​ ​ a=10; b=2; c = a/b;
​ ​ ​ [Link]("Division:= "+c);
​ ​ ​ }
​ }
class C extends B{
​ ​ ​ void mod(){
​ ​ ​ a=20; b=2; c = a%b;
​ ​ ​ [Link]("Addition:= "+c);
​ ​ ​ }
​ }
class Result{
​ ​ public static void main(String[] args) {
​ ​ ​ C obj = new C();
[Link]();
​ ​ ​ [Link]();
​ ​ ​ [Link]();
​ ​ ​ [Link]();
​ ​ ​ [Link]();
​ ​ }
​ ​ }

Q.6. Describe the package in java with suitable example.


Answer:
-​ Package in java arrange number of classes and interfaces into a particular group
called as Package.
-​ Package is nothing but folder in windows.
-​ Packages act as container for classes.
-​ Packages helps in organizing large programs, avoiding name conflicts, and
controlling access (using access modifiers)
There are two types of packages:
1.​ Built-in Packages: These are predefined packages that come with the Java
Development Kit (JDK).
Example 1. [Link]
2. [Link]
3. [Link]
2.​ User-defined Packages: Developers can create their own packages to organize
application-specific code.
This helps manage large projects by grouping related functionalities (e.g.,
database logic, user interfaces).
Example:
​ package mypack;

public class Addition {


​ ​ ​ ​ public int add(int a, int b) {
​ ​ ​ ​ ​ ​ ​ ​ return a + b;
​ ​ ​ ​ ​ ​ ​ }
}

Q.7. Differentiate between method overloading and method overriding.


Answer:

Method Overloading Method Overriding

It is a Compile time Polymorphism It is a Run time Polymorphism

It is Static Binding It is Dynamic Binding

It is implemented in single class It is implemented in two classes

Method overloading may or may not Method overriding always needs


require inheritance inheritance

In Method overloading, methods must In Method overriding, methods must have


have the same name and different the same name and same parameters
parameters

In method overloading the return type can In method overriding the return type
be different should be same

Static methods can be overloaded Static methods cannot be overridden

Early binding Late binding


Q.8 Explain the use of final keyword with suitable example.
Answer:
The final keyword in java which generally means, cannot be changed once created.
final is a modifier which provides restriction in java.
The final keyword when declared with variables, methods and classes specifically means:
1.​ The final variables cannot be modified.
2.​ The final method cannot be overridden.
3.​ The final class cannot be inherited or extended.

final variables:
A variable can be declared as, “final” then it cannot be modified further. I.e. we
cannot perform reassignment.
Syntax:
final int A = 10;
Example:
​ class sample{
​ ​ ​ public static void main(String args[])
​ ​ ​ {
​ final int A = 20;
​ [Link]. println(“Value of A: ”+A);
​ A = 30; ​ ​ ​ ​ // Not possible
​ [Link]. println(“Value of A: ”+A);
}
final methods:
The final keyword can also be applied to the method.
When a method is declared as “final”, then that method cannot be overridden by
subclasses.
Syntax:
final return_type method_nm(arguments)
{
……
}
When we try to override method that has been declared “final” it will generate compile
time error.
​ Example:
​ ​ class sample{
​ ​ ​ ​ void mNumber(){
​ ​ ​ ​ ​ [Link](“8956425872”);
​ ​ }
final void atmPIN(){​
​ [Link]. println(“2586”); }
class test extends sample{
​ void mNumber(){
​ ​ ​ ​ ​ [Link](“8956425872”);
​ ​ }
void atmPIN()​ ​ ​ ​ // Cannot override
{ [Link]. println(“2586”); }

final classes:
Classes declared “final” cannot be extended or inherited.
To prevent the inheritance concept definitely, you can declare classes as “final”.
Syntax:
final class class_name()
{
…….
}
Example:
​ ​ final class sample{
​ ​ ​ ​ void mNumber(){
​ ​ ​ ​ ​ [Link](“8956425872”);
​ ​ }
void atmPIN(){​
​ [Link]. println(“2586”); }
class test extends sample​ ​ ​ ​ ​ // cannot inherited
{
​ void mNumber(){
​ ​ ​ ​ ​ [Link](“8956425872”);
​ ​ }
void atmPIN(){​
[Link]. println(“2586”); }

You might also like