Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Oops Assignment

The document provides an overview of key concepts in Java, including definitions of JVM, JDK, and JRE, as well as principles of Object-Oriented Programming such as classes, objects, encapsulation, and inheritance. It explains method overriding and overloading, abstract classes, packages, the final keyword, constructors, and interfaces, along with examples in Java code. Additionally, it distinguishes between import and static import in Java programming.

Uploaded by

Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Oops Assignment

The document provides an overview of key concepts in Java, including definitions of JVM, JDK, and JRE, as well as principles of Object-Oriented Programming such as classes, objects, encapsulation, and inheritance. It explains method overriding and overloading, abstract classes, packages, the final keyword, constructors, and interfaces, along with examples in Java code. Additionally, it distinguishes between import and static import in Java programming.

Uploaded by

Matrix
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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()

You might also like