Java Assessment 2
Java Assessment 2
6. Define polymorphism.
Polymorphism in Java allows objects of different classes to be treated as objects of a common
superclass. It can be achieved through method overriding or method overloading.
2-Mark Questions
11. Explain the concept of classes and objects with an example.
A class is a blueprint for creating objects. For example:
class Car {
String model;
int year;
void display() {
System.out.println(model + " " + year);
}
}
class Bike {
String model;
Bike(String model) {
this.model = model; // Constructor to initialize the model
}
}
15. What are the access modifiers in Java? Explain their scope.
public: Accessible from anywhere.
private: Accessible only within the class where it is defined.
protected: Accessible within the same package or subclasses.
default (no modifier): Accessible only within the same package.
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
2.5-Mark Questions
21. Write a program to demonstrate constructor overloading.
class Book {
String title;
int pages;
22. Describe the role of the super keyword in Java inheritance. The super keyword in
Java is used to refer to the immediate parent class object. It is commonly used to call the
superclass constructor and access superclass methods or variables.
Example:
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
24. Explain the difference between abstract classes and concrete classes with examples.
Abstract class: A class that cannot be instantiated and may have abstract methods
(methods without a body). It may also have concrete methods.
26. Describe how encapsulation helps in data security. Encapsulation helps in data security
by restricting direct access to the internal state of an object. It allows modification of data
only through getter and setter methods, ensuring that only valid data is set.
For example:
class Account {
private double balance;
interface Bird {
void fly();
}
28. Explain the role of inner classes with an example. Inner classes are classes defined
inside another class. They are used when you want to logically group classes that are only
used in one place or to access the outer class’s members directly.
Example:
class Outer {
private int x = 10;
class Inner {
void display() {
System.out.println("Value of x: " + x);
}
}
}
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void move() {
System.out.println("Dog runs");
}
}
// Inner class
class InnerClass {
void displayMessage() {
System.out.println(message); // Accessing outer class's private member
}
}
void createInnerClass() {
InnerClass inner = new InnerClass();
inner.displayMessage();
}
}
public class Main {
public static void main(String[] args) {
OuterClass outer = new OuterClass();
outer.createInnerClass(); // Calling method which uses the inner class
}
}
In this example, InnerClass is an inner class defined within OuterClass, and it can access the
message variable from the outer class.
// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
39. Explain the concept of polymorphism using both compile-time and runtime
examples.
Compile-time Polymorphism (Method Overloading): This is resolved during compile time.
Overloading occurs when methods with the same name are defined but differ in the number
or types of parameters.
class Printer {
void print(int a) {
System.out.println("Printing integer: " + a);
}
void print(String a) {
System.out.println("Printing string: " + a);
}
}
Each of these principles helps in building flexible, reusable, and maintainable object-oriented
software.
41. Develop a Java program that demonstrates classes, objects, and inheritance.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating.");
}
}
void bark() {
System.out.println(name + " is barking.");
}
}
42. Write a program that demonstrates abstract classes, interfaces, and polymorphism
in Java.
// Abstract class
abstract class Animal {
abstract void sound(); // Abstract method
}
// Interface
interface Movable {
void move(); // Interface method
}
43. Develop a program to manage employee details using encapsulation and inheritance.
// Base class with encapsulation
class Employee {
private String name;
private int age;
// Constructor
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Employee Name: " + name);
System.out.println("Employee Age: " + age);
}
}
// Derived class
class Manager extends Employee {
private String department;
// Constructor
public Manager(String name, int age, String department) {
super(name, age);
this.department = department;
}
void manage() {
System.out.println("Managing department: " + department);
}
@Override
void display() {
super.display();
System.out.println("Department: " + department);
}
}
public class Main {
public static void main(String[] args) {
Manager manager = new Manager("Alice", 35, "Sales");
manager.display(); // Using inherited display() method
manager.manage(); // Manager-specific method
}
}
This program uses encapsulation to protect employee details and demonstrates inheritance
with a Manager class inheriting from Employee. The Manager class adds functionality
specific to a manager (like managing a department).
44. Create a Java application that demonstrates method overloading and overriding.
class Shape {
void area(int radius) { // Overloaded method for circle
System.out.println("Area of Circle: " + (Math.PI * radius * radius));
}
45. Design a program to demonstrate the use of access modifiers, constructors, and
methods.
class Person {
private String name; // Private variable
public int age; // Public variable
46. Develop a mini-project that utilizes OOP concepts like inheritance, polymorphism,
and interfaces.
interface Vehicle {
void start();
void stop();
}
@Override
public void stop() {
System.out.println("Car stopped");
}
}
@Override
public void stop() {
System.out.println("Bike stopped");
}
}
47. Write a Java program that demonstrates multiple levels of inheritance and method
overriding.
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
48. Explain the role of encapsulation and its implementation in a Java project.
Encapsulation is the concept of wrapping data and methods together into a single unit, and
restricting access to the data by providing public getter and setter methods. It helps to protect
the integrity of the data and ensures that the internal state of an object is not directly accessed
or modified.
Example:
class Employee {
private double salary;
49. Develop a program that showcases the use of inner classes and interfaces in solving a
problem.
interface Task {
void performTask();
}
class OuterClass {
private String taskName;
// Constructor
OuterClass(String taskName) {
this.taskName = taskName;
}
void executeTask() {
Task task = new TaskImpl(); // Using inner class
task.performTask();
}
}
50. Write a program to create a student management system using OOP concepts.
class Student {
private String name;
private int age;
private int rollNo;
// Constructor
public Student(String name, int age, int rollNo) {
this.name = name;
this.age = age;
this.rollNo = rollNo;
}
// Getter and Setter methods (Encapsulation)
public String getName() {
return name;
}