Assignment (1)
Name - Shishir Mishra
Branch (CSE)
SUBJECT - oops
Q1. Define the Following Terms:
i. JVM (Java Virtual Machine):
JVM is a part of the Java Runtime Environment (JRE). It runs Java bytecode and provides
platform independence by converting bytecode into machine-specific code.
ii. JDK (Java Development Kit):
JDK is a software development environment used to develop Java applications. It includes
tools like the compiler (javac), debugger, and JRE.
iii. JRE (Java Runtime Environment):
JRE provides the environment to run Java programs. It includes JVM and necessary libraries
but does not include development tools.
Q2. (i) Concepts of Object-Oriented Programming (OOP):
1. Class
2. Object
3. Encapsulation
4. Abstraction
5. Inheritance
6. Polymorphism
(ii) Concept of Inheritance with Java Code:
Inheritance allows one class to inherit properties and behaviors from another class.
class Animal {
void eat() {
[Link]("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Inherited method
[Link]();
}
}
Q3. What are Overriding Methods?
Method Overriding occurs when a subclass provides a specific implementation of a method
already defined in the parent class.
class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Animal obj = new Dog();
[Link](); // Output: Dog barks
}
}
Q4. Demonstrate Method Overloading
Method Overloading means defining multiple methods with the same name but different
parameters.
public class Overload {
void display() {
[Link]("No parameters");
}
void display(int a) {
[Link]("Integer: " + a);
}
void display(String s) {
[Link]("String: " + s);
}
public static void main(String[] args) {
Overload obj = new Overload();
[Link]();
[Link](10);
[Link]("Hello");
}
}
Q5. What are Abstract Classes? How Are They Different from Concrete
Classes?
Abstract Class:
● Cannot be instantiated.
● May contain abstract (unimplemented) methods.
● Used for base classes to provide a common interface or structure.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing a circle");
}
}
Difference from Concrete Class:
● Concrete class has complete implementation.
● Abstract class may have unimplemented methods.
Q6. Define a Package in Java. List the Types of Packages
Package is a namespace that organizes classes and interfaces in a logical manner.
Types of Packages:
1. Built-in packages – like [Link], [Link]
2. User-defined packages – created by the user.
Example:
package mypackage;
public class MyClass {
public void display() {
[Link]("User-defined package class");
}
}
Q7. Demonstrate the Three Uses of final Keyword
Final variable – value cannot be changed:
final int x = 10;
// x = 20; // Error
1.
Final method – cannot be overridden:
class A {
final void show() {
[Link]("Cannot override");
}
}
2.
Final class – cannot be inherited:
final class MyClass {
// No subclass can extend this class
}
3.
Q8. What are Constructors? Constructor Overloading Example
Constructor is a special method used to initialize objects. It has the same name as the class
and no return type.
Constructor Overloading Example:
class Person {
String name;
int age;
Person() {
name = "Unknown";
age = 0;
}
Person(String n, int a) {
name = n;
age = a;
}
void display() {
[Link](name + " - " + age);
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person("Alice", 25);
[Link]();
[Link]();
}
}
Q9. What are Interfaces in Java? Syntax to Create a User-Defined Interface
An interface in Java is a reference type that can contain abstract methods and constants. It is
used for full abstraction.
Syntax:
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
[Link]("Bark");
}
}
Q10. Difference Between import and static import
import – brings a class or package into scope:
import [Link];
Scanner sc = new Scanner([Link]);
static import – imports static members so they can be used directly:
import static [Link].*;
[Link](sqrt(16)); // No need to write [Link]()