Java Practical Manual Exe 2 Ans
Java Practical Manual Exe 2 Ans
ANSWERS
1. In Java, OPP (Object-Oriented Programming) is a programming paradigm that
focuses on organizing code around objects, which are instances of classes. OPP
promotes the concepts of encapsulation, inheritance, polymorphism, and abstraction
to structure code and facilitate modular, reusable, and maintainable software
development.
3. Understanding of Classes:
In Java, a class is a blueprint or template for creating objects. It defines the attributes
(data members) and behaviors (methods) that objects of that class will have. A class
serves as a fundamental building block of object-oriented programming. It
encapsulates data and provides methods to interact with that data. It represents a
category or type of objects and defines the shared characteristics and functionalities
among them.
A class can have constructors to initialize objects, attributes to store data, and methods to
perform actions or provide functionality. Objects are created from classes using the
'new' keyword, and each object has its own set of attribute values and can invoke the
methods defined in its class. Classes facilitate code reuse, modularization, and the
organization of related data and behaviors in a structured manner.
4. An Object:
An object is an instance of a class. It represents a specific entity or item created from a
class blueprint. Objects have state (attributes) and behavior (methods) defined by the
class. Each object is a separate entity with its own memory allocation, and changes
made to one object do not affect other objects of the same class.
Objects are created using the 'new' keyword followed by the constructor of the class.
They can be used to store data, perform actions, or interact with other objects. Objects
provide a way to model real-world entities, encapsulate data and behavior, and
facilitate modular and reusable code design. They are the primary building blocks of
object-oriented programming.
LABORATORY EXERCISE 2
ANSWERS
QUESTION 1
1. INHERITANCE
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one
class to inherit properties and behaviors from another class. Inheritance establishes a "is-a"
relationship between classes, where the subclass (derived class) inherits characteristics from
the superclass (base class). The subclass can extend or specialize the functionality of the
superclass by adding new methods or overriding existing ones.
QUESTION 2
Here's an example program in Java that demonstrates the concept of inheritance:
```java
// Superclass
class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
public void sleep() {
System.out.println(name + " is sleeping.");
}
}
// Subclass inheriting from Animal
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println(name + " is barking.");
}
}
System.out.println();
In this example, we have a superclass called `Animal` and a subclass called `Dog`. The
`Animal` class has a `name` attribute and two methods, `eat()` and `sleep()`, which are
inherited by the `Dog` class. The `Dog` class adds a new method called `bark()`.
In the `main` method, we create an instance of the `Animal` class and call its `eat()` and
`sleep()` methods. Then, we create an instance of the `Dog` class and call all three methods:
`eat()`, `sleep()`, and `bark()`. The output will show the respective actions performed by the
animal and the dog.
This example demonstrates how the `Dog` class inherits the attributes and methods from the
`Animal` class, allowing it to access and utilize them. Inheritance promotes code reuse,
abstraction, and extensibility by allowing classes to inherit and extend the functionality of
existing classes.
LABORATORY EXERCISE 3
ANSWERS
2. Polymorphism is a key concept in object-oriented programming that allows objects of
different classes to be treated as objects of a common superclass. It enables a single
interface to be used to represent multiple types of objects. Polymorphism is achieved
through method overriding and method overloading.
// Subclass 1
class Dog extends Animal {
public void makeSound() {
System.out.println("The dog barks.");
}
}
// Subclass 2
class Cat extends Animal {
public void makeSound() {
System.out.println("The cat meows.");
}
}
In this program, we have a superclass `Animal` with a method `makeSound()`. The `Dog` and
`Cat` classes are subclasses of `Animal` and override the `makeSound()` method with their
specific implementations.
In the `main()` method, we create objects of `Dog` and `Cat` classes but assign them to
variables of type `Animal`. This demonstrates polymorphic behavior, as the objects of the
subclasses are treated as objects of the superclass. When we call the `makeSound()` method
on these objects, the overridden methods in the respective subclasses are invoked based on
the actual object type.
The program shows how polymorphism allows different objects to respond differently to the
same method call, based on their specific implementations.
LABORATORY EXERCISE 4
ANSWERS
1. Abstraction is a fundamental principle in object-oriented programming that focuses on
representing the essential features and behavior of an object or system while hiding
unnecessary details. It provides a simplified and generalized view of complex systems.
2. Here's an example program to demonstrate the concept of abstraction using abstract
classes and interfaces:
```java
// Abstract class
abstract class Shape {
// Abstract method
public abstract void draw();
// Concrete method
public void display() {
System.out.println("Displaying the shape.");
}
}
// Concrete subclass
class Circle extends Shape {
// Implementing the abstract method
public void draw() {
System.out.println("Drawing a circle.");
}
}
// Interface
interface Drawable {
void draw();
}
In this program, we have an abstract class `Shape` that defines an abstract method `draw()`
and a concrete method `display()`. The `Circle` class is a concrete subclass of `Shape` that
provides an implementation for the `draw()` method. The `Rectangle` class is a concrete class
that implements the `Drawable` interface, which specifies the `draw()` method.
In the `main()` method, we create objects of the `Circle` and `Rectangle` classes, but assign
them to variables of types `Shape` and `Drawable` respectively. This demonstrates
polymorphic behavior and abstraction, as we interact with the objects using the abstract class
and interface. We can call the `draw()` method on these objects, and the appropriate
implementations are invoked based on the actual object types.
The program illustrates how abstraction allows us to define common behavior in abstract
classes and interfaces, hiding the specific details of implementation. It provides a simplified
and generalized view of objects and promotes code modularity and flexibility.
LABORATORY EXERCISE 5
ANSWERS
1. Overriding is a feature in object-oriented programming that allows a subclass to
provide a different implementation of a method that is already defined in its
superclass. It is used to modify the behavior of inherited methods to suit the specific
needs of the subclass. The overridden method in the subclass must have the same
name, return type, and parameters as the method in the superclass.
```java
// Superclass
class Animal {
public void makeSound() {
System.out.println("The animal makes a sound.");
}
}
// Subclass
class Dog extends Animal {
// Overriding the makeSound() method
public void makeSound() {
System.out.println("The dog barks.");
}
}
LABORATORY EXERCISE 6
ANSWERS
1. Encapsulation is a fundamental principle in object-oriented programming that
combines data (attributes) and methods (functions) into a single unit called a class. It
provides data hiding and abstraction by controlling the access to the internal state of
an object. Encapsulation helps in maintaining the integrity and security of data by
preventing direct modification from external code.
In Java, encapsulation is achieved by declaring class variables as private and providing public
getter and setter methods to access and modify the data. This way, the internal state of the
object remains hidden, and interactions with the object are controlled through defined
methods.
```java
// Encapsulated class
class Person {
private String name;
private int age;
LABORATORY EXERCISE 7
ANSWERS
1. ATTRIBUTES
In object-oriented programming (OOP), attributes are the variables or data members
associated with a class. They represent the state or characteristics of an object. Attributes
store information about the object's properties and can have different data types, such as
integers, strings, booleans, etc. They define the data that an object can hold.
// Default constructor
public Person() {
name = "John Doe";
age = 0;
}
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter methods
LABORATORY EXERCISE 11
ANSWERS
1. In Java, multiple inheritance refers to the ability of a class to inherit characteristics
and behaviors from multiple parent classes. However, Java does not support multiple
inheritance for classes, meaning a class can only extend a single class. This is done to
avoid the complications and ambiguities that can arise from conflicting method
implementations and member variables.
interface Swimmable {
void swim();
}
@Override
public void swim() {
System.out.println("The duck is swimming.");
}
}
In this program, we have two interfaces: `Walkable` and `Swimmable`. Each interface
declares a single method without providing any implementation.
The `Duck` class implements both the `Walkable` and `Swimmable` interfaces. It
provides the implementation for the `walk()` and `swim()` methods defined in the
interfaces.
In the `main()` method, we create an object of the `Duck` class and invoke the `walk()`
and `swim()` methods. The output demonstrates how the class inherits and
implements behaviors from multiple interfaces, achieving a form of multiple
inheritance
LABORATORY EXERCISE 12
ANSWERS
1. In Java, an abstract method is a method declared in an abstract class or interface that
does not have an implementation. It provides a method signature without any method
body. The purpose of an abstract method is to define a contract that must be
implemented by the subclasses or implementing classes.
Abstract methods serve as placeholders for behavior that must be defined by concrete
classes. They provide a way to enforce a specific method signature and ensure that
subclasses provide their own implementation of the method.
```java
// Abstract class with an abstract method
abstract class Shape {
public abstract void draw();
}
In this program, we have an abstract class `Shape` with an abstract method `draw()`. The
`Shape` class provides the method signature without any implementation.
The `Circle` class is a concrete subclass of `Shape` and overrides the `draw()` method,
providing its own implementation to draw a circle.
In the `main()` method, we create an object of the `Circle` class and invoke the `draw()`
method. The output demonstrates how the abstract method in the abstract class is
implemented by the concrete subclass.
The program illustrates how abstract methods define a contract that must be fulfilled by
subclasses. Abstract methods play a crucial role in defining the behavior of abstract
classes and interfaces, allowing for polymorphism and providing a structure for the
implementation of specific methods by subclasses.