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

java

Uploaded by

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

java

Uploaded by

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

1.

Method Overloading and Method Overriding

• Method Overloading: Involves defining multiple methods in the same class with the same name
but different parameter lists. It is an example of compile-time polymorphism.

• Method Overriding: Occurs when a subclass provides a specific implementation for a method
already defined in its parent class. It is an example of runtime polymorphism.

2. Function Overloading (Method Overloading) with Example:


Method overloading allows multiple methods with the same name to exist, differentiated by the number
or type of parameters.

3. Comparison of Method Overloading and Overriding

Feature Method Overloading Method Overriding

Same method name, different parameter Same method signature, redefined in


Definition
lists. subclass.

Polymorphism
Compile-time Runtime
Type

Inheritance Not required Required

Binding Static Dynamic

add(int, int) and add(double, double) in the


Example Overriding sound() in a subclass.
same class.

1. Relationship between Inheritance and Polymorphism:


Inheritance allows a class to acquire properties and methods from a parent class. Polymorphism
allows methods to behave differently based on the object invoking them.
2.

OOP vs Structured Programming

Feature Object-Oriented Programming Structured Programming


Focus Objects and their interactions. Procedures and functions.
Data and functions are
Data Handling Data encapsulated in objects.
separate.
Reusability High, due to inheritance. Limited.
Advantages of Modularity, scalability, and
Simpler for small tasks.
OOP maintainability.
Classpath in Java

Classpath is an environment variable that tells the Java compiler and JVM where to look for user-defined
classes and packages.
1. Why is Java Machine Independent?
Java programs are compiled into bytecode, which the JVM interprets on any platform.

2. Relationship between Abstraction and Encapsulation:

• Abstraction: Hides implementation details and focuses on functionality.

• Encapsulation: Wraps data and methods together while restricting access.

Here are detailed explanations for each of the provided questions:

1. What is Classpath? Explain the Utility of Classpath with an Example.

Classpath is an environment variable or command-line argument in Java that specifies the location of
user-defined classes and packages. It tells the Java compiler and JVM where to look for class files.

Utility of Classpath:

• Ensures the JVM can find all required .class files.

• Supports modularity by organizing dependencies.

• Can include directories, JAR files, or even remote locations

Basic Features of Object-Oriented Programming

1. Encapsulation:
Bundling data and methods that operate on the data within a class, while restricting direct
access to some components.

2. Inheritance:
Enabling a class (subclass) to inherit fields and methods from another class (superclass).

3. Polymorphism:
Allowing a single interface to represent different types. Achieved via method overloading
(compile-time) and overriding (runtime).

4. Abstraction:
Hiding implementation details while exposing only the essential features.

5. Reusability:
Code can be reused across different parts of a program via inheritance and classes.

3. Why is Java Called Machine-Independent? Explain JVM.

Java is machine-independent because it uses bytecode as an intermediate representation. When Java


code is compiled, it is converted into platform-neutral bytecode, which can be executed on any machine
that has a Java Virtual Machine (JVM).

Functionality of JVM:

1. Class Loader: Loads class files into memory.


2. Bytecode Verifier: Ensures code adheres to security and execution standards.

3. Interpreter/Just-In-Time Compiler: Converts bytecode into native machine instructions.

Relationship Between Data Abstraction and Encapsulation

• Abstraction focuses on hiding implementation details and only exposing the essential
functionality.
Example: Abstract classes and interfaces.

• Encapsulation is the mechanism of bundling data and methods together and restricting direct
access.

• 5. What is a Class? How Does it Accomplish Data Hiding?


• A class in Java is a blueprint for creating objects. It defines properties (fields) and
behaviors (methods).
Data Hiding: It is achieved using access modifiers like private. Only the class can
access private fields, and external access is provided via public methods.

What is ‘this’ Keyword? Explain its Usage with an Example.

The this keyword in Java refers to the current instance of a class. It is used to:

1. Distinguish between instance variables and parameters with the same name.

2. Call another constructor in the same class.

3. Pass the current object as an argument to a method or constructor.

Why is the main Method in Java Static? Justify Your Answer.

The main method in Java is static to allow the JVM to invoke it without creating an instance of the class.
This is necessary because:

1. At the start of execution, no objects exist.

2. A static method belongs to the class, not an instance, enabling JVM to execute it directly.

You might also like