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() {
System.out.println("This animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited method
d.bark();
}
}
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() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound(); // 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() {
System.out.println("No parameters");
}
void display(int a) {
System.out.println("Integer: " + a);
}
void display(String s) {
System.out.println("String: " + s);
}
public static void main(String[] args) {
Overload obj = new Overload();
obj.display();
obj.display(10);
obj.display("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() {
System.out.println("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 java.util, java.io
2. User-defined packages – created by the user.
Example:
package mypackage;
public class MyClass {
public void display() {
System.out.println("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() {
System.out.println("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() {
System.out.println(name + " - " + age);
}
}
public class Test {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person("Alice", 25);
p1.display();
p2.display();
}
}
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() {
System.out.println("Bark");
}
}
Q10. Difference Between import and static import
import – brings a class or package into scope:
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
static import – imports static members so they can be used directly:
import static java.lang.Math.*;
System.out.println(sqrt(16)); // No need to write Math.sqrt()