0% found this document useful (0 votes)
8 views30 pages

Java Object-Oriented Programming Examples

The document contains several Java programming exercises focused on Object Oriented Programming concepts. It includes programs for matrix addition, stack operations, employee salary management, 2D point modeling, shape drawing with polymorphism, and abstract classes for calculating area and perimeter. Each program is accompanied by example code and expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

Java Object-Oriented Programming Examples

The document contains several Java programming exercises focused on Object Oriented Programming concepts. It includes programs for matrix addition, stack operations, employee salary management, 2D point modeling, shape drawing with polymorphism, and abstract classes for calculating area and perimeter. Each program is accompanied by example code and expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming with Java ( BCS306A)

JAV
PROGRAM-1

Add two matrices of suitable order N [The value of N should be read from command line argument]

package programs;
import [Link];
public class MatrixA {

public static void main (String[] args)


{
// TODO Auto-generated method stub
int n = [Link] (args[0]);

int i,j;

int[ ][ ] matrix1 = new int[n][n];


int[ ][ ] matrix2 = new int[n][n];
int[ ][ ] sum = new int[n][n];
Scanner sc=new
Scanner([Link]);

// Initialize matrices with some values, for example, i+j


[Link]("Enter the elements in the matrix1:");

for ( i = 0; i < n; i++)


{
for (j = 0; j < n; j++)
{
matrix1[i][j] = [Link]();

}
}

[Link]("Enter the elements in the matrix2:");

for (i = 0; i < n; i++)


{
for (j = 0; j < n; j++)
{
matrix2[i][j] = [Link]();

}
}

ISE, Dept of BIT Page 1


Object Oriented Programming with Java ( BCS306A)

// Add the matrices


for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];

}
}

// Print the result


[Link]("Sum of matrices is: ");
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
[Link](" " +sum[i][j] );
}

[Link]();
}
}
}

OUTPUT:

Enter the elements in the matrix1:


12
34
Enter the elements in the matrix2:
15
48
Sum of matrices is:
27
71

ISE, Dept of BIT Page 2


Object Oriented Programming with Java ( BCS306A)

OBJECT ORIENTED PROGRAMMING WITH


PROGRAM-2

Develop a stack class to hold a maximum 10 integers with suitable methods. Develop a method to
illustrate Stack operations

package programs;
import [Link];
class Stack {

private int[] elements;


private int top;

public Stack() {
elements = new int[10];
top = -1;
}

public boolean isEmpty() {


return top == -1;
}

public boolean isFull() {


return top == 9;
}

public void push(int element) {


if (isFull()) {
[Link]("Stack is full. Cannot push more elements.");
} else {
elements[++top] = element;
[Link]("Pushed: " + element);
}
}

public void pop() {


if (isEmpty()) {
[Link]("Stack is empty. Cannot pop elements.");
} else {
int poppedElement = elements[top--];
[Link]("Popped: " + poppedElement);
}
}

ISE, Dept of BIT Page 3


Object Oriented Programming with Java ( BCS306A)

OBJECT ORIENTED PROGRAMMING WITH JAVA


public void printStack() {

if (isEmpty()) {
[Link]("Stack is empty.");
} else {
[Link]("Stack: ");
for (int i = 0; i <= top; i++) {
[Link](elements[i] + " ");

}
[Link]();
}
}
}

public class Main {


public static void main(String[] args) {
Stack stack = new Stack();
while(true)
{
[Link]("Stack Operations");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Display");
[Link]("4. Exit");
Scanner sc = new Scanner([Link]);
[Link]("Enter your Choice: ");
int choice = [Link]();

switch(choice)
{
case 1: [Link]("Enter Number to push: ");
int num = [Link]();
[Link](num);
break;
case 2:
[Link]();
break;
case 3: [Link]();
break;
case 4: [Link](0);
break;
default: [Link]("Invalid choice ");
}
} }}
OBJEC

ISE, Dept of BIT Page 4


Object Oriented Programming with Java ( BCS306A)

OUTPUT:

Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
10
Pushed: 10
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 1
Enter Number to push:
20
Pushed: 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 3
Stack: 10 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 2
Popped: 20
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 3
Stack: 10
Stack Operations
1. Push
2. Pop
3. Display
4. Exit
Enter your Choice: 4

ISE, Dept of BIT Page 5


Object Oriented Programming with Java ( BCS306A)

PROGRAM-3

A class called Employee, which models an employee with an ID, name and salary, is designed as shown
in the following class diagram. The method raiseSalary (percent) increases the salary by the given
percentage. Develop the Employee class and suitable main method for demonstration.

package programs;
import [Link];
public class Employee {
private int empId;
private String name;
private double salary;

public Employee(int empId, String name, double salary) {


[Link] = empId;
[Link] = name;
[Link] = salary;
}

public void raiseSalary(double percentage) {


if (percentage > 0) {
double raiseAmount = salary * (percentage / 100);
salary += raiseAmount;
}
}

public void displayInfo() {


[Link]("Employee ID: " + empId);
[Link]("Name: " + name);
[Link]("Salary: Rs." + [Link]("%.2f", salary));
}

public static void main(String[] args) {

// Creating an Employee object


Employee emp = new Employee(1, "Dr. STHIRA", 50000.0);
Scanner scanner = new Scanner([Link]);

// Displaying employee information before raise


[Link]("Employee information before raise:");
[Link]();
[Link]("Enter the percentage of salary to raise:");

ISE, Dept of BIT Page 6


Object Oriented Programming with Java ( BCS306A)

BCS30
int percentage = [Link]();

// Raising salary by 10%


[Link](percentage);

// Displaying employee information after raise

[Link]("\nEmployee information after raise:");


[Link]();
}
}

OUTPUT:

Employee information before raise:


Employee ID: 1
Name: Dr. STHIRA
Salary: Rs.50000.00
Enter the percentage of salary to raise:
25

Employee information after raise:


Employee ID: 1
Name: Dr. STHIRA
Salary: Rs.62500.00

ISE, Dept of BIT Page 7


Object Oriented Programming with Java ( BCS306A)

PROGRAM-4

A class called MyPoint, which models a 2D point with x and y coordinates, is designed as follows Two
instance variables x (int) and y (int).
A default (or "no-arg") constructor that construct a point at the default location of (0, 0). A
overloaded constructor that constructs a point with the given x and y coordinates.
A method setXY() to set both x and y.
A method getXY() which returns the x and y in a 2-element int array.
A toString() method that returns a string description of the instance in the format "(x, y)".
A method called distance(int x, int y) that returns the distance from this point to another point
at the given (x, y) coordinates
An overloaded distance(MyPoint another) that returns the distance from this point to the given
MyPoint instance (called another)
Another overloaded distance() method that returns the distance from this point to the origin
(0,0)
Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint) to test all
the methods defined in the class.

package programs;

public class MyPoint {

private int x;
private int y;

// Default constructor
public MyPoint() {
this.x = 0;
this.y = 0;
}

// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}

// Setters for x and y


public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
// Getter for x and y
public int [] getXY() {

ISE, Dept of BIT Page 8


Object Oriented Programming with Java ( BCS306A)

BCS3
int[] coordinates = {x, y};
return coordinates;
}

// Returns the string description of the instance in the format "(x, y)"
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}

// Calculates distance from this point to another point (x, y)


public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return [Link](xDiff * xDiff + yDiff * yDiff);
}

// Calculates distance from this point to another MyPoint instance


public double distance(MyPoint another) {
int xDiff = this.x - another.x;
int yDiff = this.y - another.y;
return [Link](xDiff * xDiff + yDiff * yDiff);
}

// Calculates distance from this point to the origin (0, 0)


public double distance() {
return [Link](x * x + y * y);
}

public static void main(String[] args) {


MyPoint point1 = new MyPoint(); // Default constructor (0,0)
[Link]("Point 1: " + point1);

MyPoint point2 = new MyPoint(3,4); // Overloaded constructor (3,4)


[Link]("Point 2: " + point2);

[Link](1,2);//9 Set coordinates using setXY() method


[Link]("Point 1 after setXY(): " + point1);

int[] coordinates = [Link](); // Get coordinates using getXY() method


[Link]("Point 2 coordinates: (" + coordinates[0] + ", " + coordinates[1] +")");

ISE, Dept of BIT Page 9


Object Oriented Programming with Java ( BCS306A)

BCS306A
[Link]("Distance between Point 1 and (1,2): " + [Link](1,2));
[Link]("Distance between Point 1 and Point 2: " + [Link](point2));
[Link]("Distance from Point 2 to origin: " + [Link]());
}
}

OUTPUT:

Point 1: (0, 0)
Point 2: (3, 4)
Point 1 after setXY (): (1, 2)
Point 2 coordinates: (3, 4)
Distance between Point 1 and (1,2): 0.0
Distance between Point 1 and Point 2: 2.8284271247461903
Distance from Point 2 to origin: 2.23606797749979

ISE, Dept of BIT Page 10


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAV


PROGRAM-5

Develop a JAVA program to create a class named shape. Create three sub classes namely: circle, triangle
and square, each class has two member functions named draw() and erase(). Demonstrate polymorphism
concepts by developing suitable methods, defining member data and main program.

package programs;

//Shape class (Superclass)


class Shape {
public void draw() {
[Link]("Drawing a shape");
}

public void erase() {


[Link]("Erasing a shape");
}
}

//Circle class (Subclass)


class Circle extends Shape {
@Override
public void draw() {
[Link]("Drawing a circle");
}

@Override
public void erase() {
[Link]("Erasing a circle");
}
}

//Triangle class (Subclass)


class Triangle extends Shape {
@Override
public void draw() {
[Link]("Drawing a triangle");
}

@Override
public void erase() {
[Link]("Erasing a triangle");
}
}

ISE, Dept of BIT Page 11


Object Oriented Programming with Java ( BCS306A)

[Link]("Drawing a square");
}

@Override
public void erase() {
[Link]("Erasing a square");
}
}

//Main class
public class Main {
public static void main(String[] args) {

// Polymorphism: Creating objects of different subclasses using the reference of the superclass

Shape shape1 = new Circle();


Shape shape2 = new Triangle();
Shape shape3 = new Square();

// Demonstrating polymorphic behavior


[Link](); // Calls draw() method of Circle class
[Link](); // Calls erase() method of Circle class

[Link](); // Calls draw() method of Triangle class


[Link](); // Calls erase() method of Triangle class

[Link](); // Calls draw() method of Square class


[Link](); // Calls erase() method of Square class
}
}

OUTPUT:
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square

ISE, Dept of BIT Page 12


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM-6

Develop a JAVA program to create an abstract class Shape with abstract methods calculateArea()
and calculatePerimeter(). Create subclasses Circle and Triangle that extend the Shape class and
implement the respective methods to calculate the area and perimeter of each shape.

package programs;

// Abstract Shape class


abstract class Shape {
// Abstract methods to calculate area and perimeter
abstract double calculateArea();
abstract double calculatePerimeter();
}

// Circle class extending Shape


class Circle extends Shape {
private double radius;

// Constructor for Circle class


public Circle(double radius) {
[Link] = radius;
}

// Implementation of abstract method to calculate area for Circle


@Override
double calculateArea() {
return [Link] * radius * radius;
}

// Implementation of abstract method to calculate perimeter (circumference) for Circle


@Override
double calculatePerimeter() {
return 2 * [Link] * radius;
}
}

// Triangle class extending Shape


class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
ISE, Dept of BIT Page 13
Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

// Constructor for Triangle class


public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

// Implementation of abstract method to calculate area for Triangle using Heron's formula
@Override
double calculateArea() {
double s = (side1 + side2 + side3) / 2;
return [Link](s * (s - side1) * (s - side2) * (s - side3));
}

// Implementation of abstract method to calculate perimeter for Triangle


@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating Circle and Triangle objects
Circle circle = new Circle(5);
Triangle triangle = new Triangle(3, 4, 5);

// Calculating and displaying area and perimeter for Circle


[Link]("Circle - Area: " + [Link]() + ", Perimeter: " +
[Link]());

// Calculating and displaying area and perimeter for Triangle


[Link]("Triangle - Area: " + [Link]() + ", Perimeter: " +
[Link]());
}
}

OUTPUT:

Circle - Area: 78.53981633974483, Perimeter: 31.41592653589793


Triangle - Area: 6.0, Perimeter: 12.0

ISE, Dept of BIT Page 14


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM -7

Develop a JAVA program to create an interface Resizable with methods resizeWidth (int width)
and resizeHeight (int height) that allow an object to be resized. Create a class Rectangle that
implements the Resizable interface and implements the resize methods

package programs;

// Resizable interface
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}

// Rectangle class implementing Resizable interface


class Rectangle implements Resizable {
private int width;
private int height;

// Constructor
public Rectangle(int width, int height) {
[Link] = width;
[Link] = height;
}

// Implementation of resizeWidth method from Resizable interface


@Override
public void resizeWidth(int width) {
[Link] = width;
}

// Implementation of resizeHeight method from Resizable interface


@Override
public void resizeHeight(int height) {
[Link] = height;
}

// Method to display the dimensions of the rectangle


public void displayDimensions() {
[Link]("Width: " + width + ", Height: " + height);
}
}

ISE, Dept of BIT Page 15


Object Oriented Programming with Java ( BCS306A)

// Main class
public class Main {
public static void main(String[] args) {

ISE, Dept of BIT Page 16


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

// Creating a Rectangle object


Rectangle rectangle = new Rectangle(10, 20);
[Link]("Original Dimensions:");
[Link](); // Output: Width: 10, Height: 20

// Resizing the rectangle


[Link](15);
[Link](25);
[Link]("Dimensions after resizing:");
[Link](); // Output: Width: 15, Height: 25
}
}

OUTPUT:

Original Dimensions:
Width: 10, Height: 20
Dimensions after resizing:
Width: 15, Height: 25

ISE, Dept of BIT Page 17


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM-8

Develop a JAVA program to create an outer class with a function display. Create another class
inside the outer class named inner with a function called display and call the two functions in the
main class.

package programs;

// OuterClass containing an inner class Inner


class OuterClass {
// Outer class display method
public void display() {
[Link]("OuterClass display method");
}

// Inner class
class Inner {
// Inner class display method
public void display() {
[Link]("InnerClass display method");
}
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating an object of OuterClass
OuterClass outerObject = new OuterClass();

// Calling display method of OuterClass


[Link](); // Output: OuterClass display method

// Creating an object of Inner class (inside OuterClass)


[Link] innerObject = [Link] Inner();

// Calling display method of Inner class


[Link](); // Output: InnerClass display method
}
}

OUTPUT:

ISE, Dept of BIT Page 18


Object Oriented Programming with Java ( BCS306A)

OuterClass display method


InnerClass display method

ISE, Dept of BIT Page 19


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA


BCS306A OBJECT ORIENTED PROGRAMMING WITH J
PROGRAM-9

Develop a JAVA program to raise a custom exception (user defined exception) for DivisionByZero
using try, catch, throw and finally.

package programs;

// Custom exception class (extends Exception)


class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
}
}

// Main class
public class Excep {
public static void main(String[] args) {
int numerator = 10;
int denominator = 0;

try {

// Attempting division
if (denominator == 0) {

// Throwing custom exception if denominator is zero


throw new DivisionByZeroException("Division by zero is not allowed.");
}

int result = numerator / denominator;


[Link]("Result of division: " + result);
}
catch (DivisionByZeroException e) {

// Catching and handling the custom exception


[Link]("Exception caught: " + [Link]());
}
finally {

// Code in the finally block will always execute, whether an exception occurs or not
[Link]("Finally block executed.");
}
}

ISE, Dept of BIT Page 20


Object Oriented Programming with Java ( BCS306A)

}
OUTPUT:

Exception caught: Division by zero is not allowed.


Finally block executed.

ISE, Dept of BIT Page 21


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM-10

Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.

Create a directory named mypack in your project's source folder.


Create a Java File inside the mypack Package:

Inside the mypack directory, create a Java file named [Link].


[Link] (Inside mypack package):
*/

// Class inside the mypack package

package mypack; // Package declaration


public class MyPackageClass {
public void display() {
[Link]("Hello from MyPackageClass in mypack package!");
}
}

package programs; // Package declaration

import [Link]; // Importing the class from mypack package

public class Main {


public static void main(String[] args) {
MyPackageClass myPackageObj = new MyPackageClass();
[Link](); // Calling the display method from MyPackageClass
}
}

OUTPUT:

Hello from MyPackageClass in mypack package!

ISE, Dept of BIT Page 22


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM-11

Write a program to illustrate creation of threads using runnable class. (start method start each of the
newly created thread. Inside the run method there is sleep() for suspend the thread for 500
milliseconds).

// Runnable class implementation


class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Thread " + [Link]().getId() + ": " + i);
try {
[Link](500); // Suspend the thread for 500 milliseconds
} catch (InterruptedException e) {
[Link]();
}
}
}
}

// Main class
public class Main {
public static void main(String[] args) {
// Creating Runnable objects
MyRunnable myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();

// Creating threads and starting them


Thread thread1 = new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);

// Starting threads using the start() method


[Link]();
[Link]();
}
}

OUTPUT:
Thread 14: 1
Thread 15: 1
Thread 14: 2
Thread 15: 2
Thread 14: 3

ISE, Dept of BIT Page 23


Object Oriented Programming with Java ( BCS306A)
Thread 15: 3
Thread 14: 4
Thread 15: 4
Thread 14: 5
Thread 15: 5

ISE, Dept of BIT Page 24


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

PROGRAM-11

Develop a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and start the thread. The run method of the class starts after this. It can be
observed that both main thread and created child thread are executed concurrently.

package programs;

// Custom thread class MyThread extending Thread


class MyThread extends Thread {

// Constructor calling the base class constructor and starting the thread
public MyThread(String name) {
super(name);
start(); // Start the thread when the constructor is called
}

// Run method to be executed when the thread starts


public void run() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getName() + ": " + i);

try {
[Link](1000); // Suspend the thread for 500 milliseconds
} catch (InterruptedException e) {
[Link]();
}
}
}
}

// Main class
public class Main {
public static void main(String[] args) {

// Main thread executing concurrently with the MyThread instance


for (int i = 1; i <= 5; i++) {
[Link]("Main Thread: " + i);

try {
[Link](500); // Suspend the main thread for 500 milliseconds
ISE, Dept of BIT Page 25
Object Oriented Programming with Java ( BCS306A)

} catch (InterruptedException e) {
[Link]();

ISE, Dept of BIT Page 26


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

}
}

// Creating an instance of MyThread


MyThread myThread = new MyThread("Child Thread");

// Main thread and child thread executing concurrently


}
}

OUTPUT:

Main Thread: 1
Main Thread: 2
Main Thread: 3
Main Thread: 4
Main Thread: 5
Child Thread: 1
Child Thread: 2
Child Thread: 3
Child Thread: 4
Child Thread: 5

ISE, Dept of BIT Page 27


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

VIVA QUESTIONS

1. What is The Java Features ?


[Link] and Interpreted [Link]-Independent and Portable 3. Object -Oriented [Link] and
Secure [Link] [Link], Small and Familiar [Link] and interactive [Link] Performance
[Link] and Extensible

2. How Java Differs From C ?


Java does not include the C unique statement keywords goto, size of, and type def. [Link] does not
contain the data type struct,union and enum. [Link] does not define the type modifiers keywords
auto,extern register,signed,and unsigned. [Link] does not support an explicit pointer type.

3. How Java Differs From C ++ ?


java does not support operator overloading. 2. Java does not have template classes as in C++.
[Link] does not multiple inheritance of [Link] is accomplished using a new feature called
“interface”.

4. What is The Java Components?


[Link] and Classes [Link] Abstraction and Encapsulation [Link] [Link] [Link]
Binding [Link] Communication.

5. What is the Variables ?


A variable is an identifier that denotes a storage location used to store a data value.

6. What are Class?


Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a
type of object according to the data the object can hold and the operations the object can perform.

7. What are Primitive data types?


Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.

8. What is the Constructor?


Constructor is a special member function of class call automatically when object is created.

9. What is method overloading?


Function with same name but different argument perform different task known as method overloading.

ISE, Dept of BIT Page 28


Object Oriented Programming with Java ( BCS306A)

10. What is parameterized constructor?


The constructors that can take argument are called parameterized constructors

ISE, Dept of BIT Page 29


Object Oriented Programming with Java ( BCS306A)

BCS306A OBJECT ORIENTED PROGRAMMING WITH JAVA

11. What is OOPs?


Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined
interfaces to that data. An object-oriented program can be characterized as data controlling access to
code.

12. What are Encapsulation?


Encapsulation is the mechanism that binds together code and data it manipulates and keeps both safe
from outside interference and misuse

13. What are Polymorphism


Polymorphism is the feature that allows one interface to be used for general class actions.

14. What is an Object and how do you allocate memory to it?


Object is an instance of a class and it is a software unit that combines a structured set of data with a set of
operations for inspecting and manipulating that data. When an object is created using new operator,
memory is allocated to it

15. What is the difference between constructor and method


Constructor will be automatically invoked when an object is created whereas method has to be called
explicitly.

16. What are methods and how are they defined?


Methods are functions that operate on instances of classes in which they are defined.

ISE, Dept of BIT Page 30

You might also like