0% found this document useful (0 votes)
4 views

Java Preparation Unit - 3

Uploaded by

Vighnesh Pote
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Preparation Unit - 3

Uploaded by

Vighnesh Pote
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Preparation

Unit- 3
Inheritance, Interfaces and Package
2 Marks
1. Give a syntax to create a package and accessing package in java.
Ans) Syntax to create a package:
package package_name;
Syntax to access a package in Java:
import package_name.*;

Here's an example to illustrate how to create a package and access it:


1. Creating a package:
• Create a new directory with the name of your package (e.g., myPackage).
• Inside this directory, create one or more Java source files with the package
declaration at the top (e.g., MyClass.java):
// MyClass.java
package myPackage;

public class MyClass {


public void display() {
System.out.println("This is my package.");
}
}

2. Accessing the package:


• In another Java file (e.g., Main.java), import the package using the import
statement:
// Main.java
import myPackage.*;

public class Main {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}

• Compile both files using the command line or an IDE.


• Run the Main class, and it will print: This is my package.

2. List out different ways to access package from another package.


Ans) 1. import package.*:
• This statement imports all classes and interfaces from the specified package
into the current source file.
2. import package.classname:
• This statement imports a specific class or interface from the specified
package into the current source file.
3. Fully Qualified Name:
• In this approach, you use the fully qualified name of a class or interface to
reference it directly in your code without importing the package.
• Example: myPackage.MyClass obj = new myPackage.MyClass();

3. Define the interface in Java.


Ans) An interface in Java is a reference type that defines a set of abstract methods and
constants.
Syntax:
public interface InterfaceName {
// Constant declarations (optional)
// Method signatures (abstract methods)
}
Key Points:
1. Interfaces cannot be instantiated directly; they are implemented by classes.
2. An interface can contain method declarations without method bodies (abstract
methods).
3. Interfaces can also include constant declarations (public, static, final variables).
4. Classes implementing an interface must provide concrete implementations for all
its abstract methods.
5. A class can implement multiple interfaces, but it can only extend one class.
6. Interfaces support multiple inheritance in Java.

4. List the types of inheritance which is supported by java.


Ans) Types of inheritances in Java:
i. Single level inheritance
ii. Multilevel inheritance
iii. Hierarchical inheritance
iv. Multiple inheritance
v. Hybrid inheritance

5. Enlist any four inbuilt packages in Java.


OR
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
6. Enlist any four compile time errors.
Ans) 1)Missing semicolon
2)Missing of brackets in classes and methods
3)Misspelling of variables and keywords.
4)Missing double quotes in Strings.
5)Use of undeclared variable.
6)Incompatible type of assignment/initialization.
7)Bad reference to object.
4 Marks
1. Describe interface in java with suitable example.
Ans) • An interface in Java is a reference type that defines a set of abstract methods and
constants.
• It serves as a blueprint for classes that implement it.
• Interfaces cannot be instantiated directly; they are implemented by classes.
• An interface can contain method declarations without method bodies (abstract
methods).
• Interfaces can also include constant declarations (public, static, final variables).
• Classes implementing an interface must provide concrete implementations for all its
abstract methods.
• A class can implement multiple interfaces, but it can only extend one class.
• Interfaces support multiple inheritance in Java.

Syntax:
public interface InterfaceName {
// Constant declarations (optional)
// Method signatures (abstract methods)
}

Example:
interface Greeting {
void greet();
}

class Greeting1 implements Greeting {


public void greet() {
System.out.println("Hello, World!");
}
}

public class Main {


public static void main(String[] args) {
Greeting1 Obj = new Greeting1();
Obj.greet();
}
}
2. Differentiate between method overloading and method overriding.
Ans)
Feature Method Overloading Method Overriding

Multiple methods in the same A method in a subclass with the same


class with the same name but signature (name and parameters) as a
Definition different parameters. method in its superclass.

Involves inheritance; occurs in a


Inheritance Not affected by inheritance. subclass inheriting from a superclass.
Can have the same or different Must have the same return type or a
Return Type return types. covariant return type.
Must not have more restrictive access
Access Can have different access modifiers (can have less restrictive
Modifiers modifiers. modifiers).

Must have different parameter Must have the same parameter types
Parameters types or numbers. and number, or covariant types.

Static Cannot override static methods; hiding


Methods Can overload static methods. occurs instead.
.
3. Explain single and multilevel inheritance with proper example.
Ans) 1. Single Inheritance:
• Single inheritance refers to a scenario where a class inherits properties and
behaviors from a single superclass.
• Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}

public static void main(String[] args) {


Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
2. Multilevel Inheritance:
• Multilevel inheritance refers to a scenario where a class inherits properties
and behaviors from a superclass, and another class inherits from the
subclass.
• Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

class Labrador extends Dog {


void color() {
System.out.println("Labrador is black");
}

public static void main(String[] args) {


Labrador labrador = new Labrador();
labrador.eat();
labrador.bark();
labrador.color();
}
}
3. Hierarchical Inheritance:
• Hierarchical inheritance refers to a scenario where more than one subclass
inherits properties and behaviors from a single superclass.
• Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

class Cat extends Animal {


void meow() {
System.out.println("Cat is meowing");
}

public static void main(String[] args) {


Dog dog = new Dog();
dog.eat();
dog.bark();

Cat cat = new Cat();


cat.eat();
cat.meow();
}
}
4. Multiple Inheritance (through Interfaces):
• Java does not support multiple inheritance through classes, but it supports it
through interfaces.
• Example:
interface A {
void methodA();
}

interface B {
void methodB();
}

class MyClass implements A, B {


public void methodA() {
System.out.println("Method A");
}

public void methodB() {


System.out.println("Method B");
}

public static void main(String[] args) {


MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
}
}
5. Hybrid Inheritance:
• Hybrid inheritance is a combination of multiple types of inheritance.
• Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

interface Pet {
void play();
}

class Dog extends Animal implements Pet {


void bark() {
System.out.println("Dog is barking");
}

public void play() {


System.out.println("Dog is playing");
}

public static void main(String[] args) {


Dog dog = new Dog();
dog.eat();
dog.bark();
dog.play();
}
}

4. Differentiate between class and interfaces.


Ans) Feature Class Interface
Blueprint for creating objects Blueprint for implementing
Definition and defining behavior. methods and constants.
Can be inherited by other
Inheritance classes. Can be implemented by classes.
Does not support multiple Supports multiple inheritance
Multiple Inheritance inheritance. through interfaces.
Can have abstract or concrete Can only have abstract methods
Abstract Methods methods. (no method bodies).
Can have instance variables Can only have static final
Fields and static variables. variables (constants).
Constructors Can have constructors. Cannot have constructors.
Can use public, protected, By default, all methods are
Access Modifiers private, and default. public.
Implementation Can extend only one class Can implement multiple
Restrictions (single inheritance). interfaces.
Used to create objects and Used to define a contract for
Usage define behavior. classes to implement.
.
5. State the use of final keyword with respect to inheritance.
Ans) The final keyword in Java is used in the context of inheritance to prevent certain aspects of
inheritance from being overridden or extended.

Here's how the final keyword is used with respect to inheritance:


1. Final Classes:
• When a class is declared as final, it cannot be subclassed or extended by any
other class.
• Example:
final class FinalClass
{
// Class members and methods
}
2. Final Methods:
• When a method is declared as final in a superclass, it cannot be overridden
by any subclass.
• Example:
class Parent {
final void finalMethod() {
// Method implementation
}
}

class Child extends Parent {


// This will cause a compilation error
void finalMethod() {
// Attempting to override a final method
}
}
3. Final Variables:
• When a variable is declared as final, its value cannot be changed after
initialization.
• Example:
class Example {
final int value = 10;

void changeValue() {
// This will cause a compilation error
value = 20;
}
}

6 Marks
1. Define packages . How to create user defined package in Java. Explain with an
suitable example.
OR
Explain how to create a package and how to import it.
OR
Describe the package in java with suitable example.
OR
How to create user defined package in Java. Explain with an suitable example.
Ans) Package:
A package is nothing but the namespace used for organizing classes and interfaces,
providing a way to manage and structure large-scale Java applications.

Here's an example demonstrating how to create and use a user-defined package:


Step 1: Create Directory Structure:
project_folder→myPackage→ MyClass.java

Step 2: Add Package Declaration:


MyClass.java:
package myPackage;

public class MyClass {


public void display() {
System.out.println("This is a package class.");
}
}

Step 3: Compile Java Files:


Open terminal or command prompt, navigate to the project_folder directory, and compile
the MyClass.java file:
javac myPackage/MyClass.java

Step 4: Import the Package:


Main.java:
import myPackage.MyClass;

public class Main {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}

Step 5: Compile and Run:


Compile the Main.java file:
javac Main.java
Run the Main class:
java Main

Example:
Package: package1
Box.java:
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:
volume.java:
import package1.Box;

public class volume {


public static void main(String args[]) {
Box b = new Box();
b.display();
}
}

You might also like