0% found this document useful (0 votes)
2 views

Module 1-2

The document provides an overview of Object Oriented Programming (OOP) concepts, including classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It also details features of Java, such as its compiled and interpreted nature, platform independence, and robust security. Additionally, it covers the Java Development Kit (JDK), Java Virtual Machine (JVM), Integrated Development Environments (IDEs), and various programming constructs like constructors, visibility modifiers, method overloading, and exception handling.

Uploaded by

spokexo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 1-2

The document provides an overview of Object Oriented Programming (OOP) concepts, including classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It also details features of Java, such as its compiled and interpreted nature, platform independence, and robust security. Additionally, it covers the Java Development Kit (JDK), Java Virtual Machine (JVM), Integrated Development Environments (IDEs), and various programming constructs like constructors, visibility modifiers, method overloading, and exception handling.

Uploaded by

spokexo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Module 1

Q1.Define Object Oriented Programming


Object Oriented Programming (OOP) is a programming model that uses classes and
objects.
Q2.Explain the features of Object Oriented Programming
1. Classes and Objects
OOP is based on the concept of "classes". Classes are templates or blueprints for
creating objects. An object is an instance of a class, and can be created by calling the class
constructor. Objects have attributes (data) and methods (code), and can interact with other
objects through messages.

An Object is a real world entity. A Class is a group of objects that share common
properties and behaviour.

Example:

2. Encapsulation
The wrapping up of data and methods into a single unit (called class)is known as
encapsulation.The data can be accessed by the outside world only through those methods in
the class.

3. Abstraction
Data abstraction or information hiding refers to providing only essential information
to the outside world and hiding their background details.
4. Inheritance
Inheritance is the process by which one class can inherit properties and methods from
another class. The main advantage of inheritance is code reusability.
5. Polymorphism
Polymorphism means the ability to take more than one form. For example, an
operation may show different behaviour in different objects. The draw () method in Shape
class behaves differently in the three objects Circle, Rectangle and Square.

Q3.Explain the features of JAVA


1. Compiled and Interpreted: Java is a two-stage system. First, the Java compiler
translates source code into bytecode instructions. In the second stage. Java Interpreter
generates machine code.
2. Platform independent and Portable: Java bytecode is platform independent. It can
be run on any machine having Java Virtual Machine (JVM), which converts
bytecode into machine code. Java programs can be easily moved from one computer
system to another (Portable).
3. Object-Oriented: Java is a true-object oriented language. Almost everything in Java
is an object. All program code and data are written inside objects and classes.
4. Robust and Secure: Java has strict compile time and run time checking for data
types. Java has the concept of exception handling which captures serious errors and
eliminates any risk of crashing the system.
5. Distributed: Java is designed as a distributed language for creating applications on
networks. Java applications can open remote applications on the internet easily.
6. Simple, Small and Familiar: Java is a small and simple language. Java code looks
like C++ code. Many complex features of C and C++ are avoided in Java. Java does
not use pointers, header files, goto statements, operator overloading and multiple
inheritance.
7. Multithreaded: Java supports multithreading - handling multiple tasks
simultaneously.
8. High Performance: Java bytecode and multithreading facility makes Java
performance good.
Q4.Write notes on JDK
JDK stands for Java Development Kit. JDK comes with a collection of tools that are
used for developing and running Java programs. They include
1. appletviewer - Used to run Java applets
2. java - Java interpreter. Converts bytecode into machine code
3. javac - The java compiler which translates Java source code into bytecode
4. javadoc - Creates HTML documentation from Java source code files.
5. javah - Produces header files for use with native methods
6. javap - Java disassembler, which enables us to convert bytecode files into program
descriptions.
7. jdb - Java debugger, which helps us to find errors in our programs.
Q5.Define Java Virtual Machine (JVM)
The Java compiler produces an intermediate code known as bytecode for a machine
that does not exist. This machine is called theJava Virtual Machine and it exists only inside
the computer memory. JVM is responsible for running Java bytecode.
Q6.List IDE’s for developing Java applications
An Integrated Development Environment (IDE) is a software application that
provides a comprehensive environment for software development. It typically includes a
source code editor, a build automation tool, and a debugger, along with other features such as
version control, testing, and deployment tools. The most popular IDEs for Java development
are
1. Eclipse: Eclipse is a widely used open-source IDE that provides a range of features,
including a code editor, debugging tools, version control support, and more.
2. IntelliJ IDEA: IntelliJ IDEA is a commercial IDE that provides a range of features for
developing Java applications, including code completion, debugging tools, and support for
multiple languages and frameworks.
3. NetBeans: NetBeans is an open-source IDE that provides a range of features for
developing Java applications, including a code editor, debugging tools, and support for
multiple languages and frameworks.
4. JDeveloper: JDeveloper is a commercial IDE from Oracle that provides a range of
features for developing Java applications, including a code editor, debugging tools, and
support for multiple languages and frameworks.
5. BlueJ: BlueJ is a free IDE that is designed to be easy to use for beginners. It provides a
range of features for developing Java applications, including a code editor, debugging tools,
and support for multiple languages and frameworks.
6. Visual Studio Code: Visual Studio Code is a popular open-source code editor that
provides a range of features for developing Java applications, including debugging tools,
code completion, and support for multiple languages and frameworks.
Q7.Explain the general structure of a Java program
Documentation Section
The documentation section contains a set of comment lines giving the name of the
program, author and other details. There are three types of comment representations
// - Single line comment
/* …. */ - Multiline comment
/**.....*/ - Documentation comment
Package Statement
The package statement declares a package name and informs the compiler that the
classes defined here belong to this package. Example:package student;
Import Statement
This is similar to #include statements in C. For example,
import student.test;
This statement instructs the interpreter to load the test class contained in the package
student
Interface Statement
An interface is like a class but includes a group of method declarations.
Class Definitions
A Java program may contain multiple class definitions. Classes are the primary and
essential elements of a Java program.
Main Method Class
Every Java program requires a main method as its starting point, this class is the
essential part of a Java program. The main method creates objects of various classes and
establishes communication between them.
Q8.Explain the steps to create, compile and execute a Java program
1. Install the Java Development Kit (JDK): Before you can create, compile and execute a
Java program, you need to have the Java Development Kit (JDK) installed on your computer.
You can download the JDK from the Oracle website.
2. Write the Java program: Use a text editor or an Integrated Development Environment
(IDE) to write your Java program. Save the program with a .java extension.
3. Compile the Java program: To compile the program, we must run the Java compiler
javac, with the name of the source file on the command line
javac Test.java
This command will create a .class file that contains the bytecode for the program.
4. Running the Program: Once the program has been compiled successfully, you can
execute it using the following command:
java Test
Q9.Describe how classes and objects can be created and accessed
Creating Classes and Objects
Classes encapsulate logically related data items and functions that work on them. In
java, the data items are called fields and functions are called methods.
Defining a class

class classname [extends superclass name] Everything inside the square bracket is
{ optional
[fields declaration] class Empty
[methods declaration] {
} }
This is a valid class definition

Fields Declaration
class Rectangle
{
int length;
int width;
}
The variables inside the class are called instance variables. These variables are only
declared and there is no storage space created for them in the memory. Instance variables are
also known as member variables.
Methods Declaration
Methods are necessary for manipulating the data contained in the class. The general
form of method declaration is

type methodname (parameter list)


{
method body ;
}
A method declaration has four basic parts.
1. Name of the method
2. Return type
3. A list of parameters
4. The body of the method
Example:
class Rectangle
{
int l, b;
void getData (int x, int y)
{
l=x ;
b=y ;
}
int rectArea()
{
int a = l * b;
return a;
}

}
Creating Objects
Objects in Java are created using new operator. The new operator creates an object of
the specified class and returns a reference to that object.
Example:
Rectangle rect = new Rectangle ( );
Accessing Class Members
To access class members, we must use a concerned object and dot operator.

Objectname.variable name = value;


Objectname. methodname (parameter list);
Example:
rect. l = 10;
rect. b = 5;
rect..getData(10,5);
rect.rectArea( );
Q10.Explain Constructors
Constructor is a special type of method supported by Java. It enables an object to
initialise itself when it is created. Constructors have the same name as the class itself.. They
do not specify a return type, not even void.
Example:
class Rectangle
{
int length,breadth;
Rectangle(int x, int y)
{
length=x;
breadth=y;
}
int area()
{
int a = length * breadth;
return a;
}
}
class RectArea
{
public static void main(String args[])
{
Rectangle rect=new Rectangle(10,5);
int area=rect.area();
System.out.println("Area="+area);
}
}
Constructor Types
Constructors are of two types
1. Parameterised Constructor
2. Default Constructor
In a parameterised constructor, constructors are initialised by passing certain
arguments. In the default constructor, the constructor automatically initialises the object
variables with some default values.
Parameterised Constructor
class Rectangle
{
Rectangle(int x, int y)
{
length=x;
breadth=y;
}
}
Default Constructor
class Rectangle
{
Rectangle()
{
length=0;
breadth=0;
}
}
Q11.Explain Visibility Controls (Visibility Modifiers / Access Modifiers)
The access modifier specifies the accessibility or scope of a field, method, constructor
or class. Different types of java access modifiers are
1. Private: Private fields have the highest degree of protection. They are accessible only
within their own class. They cannot be inherited by subclasses. Private methods cannot be
accessed in subclasses.
2. Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default (also called friendly access)

3. Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.

5. Private Protected: This modifier makes the fields visible in all subclasses regardless of
what package they are in. They are not accessible by other classes in the same package.

Q12.Distinguish between instance and static variables and methods


Instance variables: Instance variables belong to an instance of a class (object) and are
unique for each instance. When an object is created, a copy of the instance variable is
created.
Instance Methods: Instance methods belong to an instance of a class and operate on the
instance variables of that instance. They are declared inside the class and are called using an
object of the class.

Static variables: Static variables are common to all objects and can be accessed without
using a particular object. If an object modifies a static variable, all objects of the same class
are affected.
class Cube
{
int side;
static int objectCount;
}

Static methods: Like static variables, static methods can be called without using the objects.
For example, Math class in Java provides a static method sqrt() which can be accessed as
follows
float x = Math.sqrt(25.0); // Here no object of Math class is created.

Q13.Method Overloading
In Java, it is possible to create methods that have the same name but different
parameters. This is called method overloading. Method overloading is used when objects are
required to perform similar tasks but using different parameters. When we call a method in an
object, java matches up the method name first and then the number and types of parameters
to decide which one of the definitions to execute. This process is known as polymorphism,
Method overloading is an example of polymorphism.
Example:
class Room
{
int l, w;
Room(int x, int y)
{
l=x;
w=y;
}
Room(int x)
{
l=w=x;
}
int area()
{
int a = l * w;
return a;
}
}
class RoomArea
{
public static void main (String args[])
{
Room r= new Room(100,50);
int area1=r.area();
System.out.println("Area of the Room="+area1);

Room r1= new Room(50);


int area2=r1.area();
System.out.println("Area of the Room="+area2);

}
}
Q14.Explain Exception Handling in Java
An exception is a condition that is caused by a run-time error in the program. Exception
Handling is a mechanism used to detect and report exceptions so that appropriate action can
be taken. Exception Handling involves the following steps:
1. Find the problem (Hit the exception)
2. Inform that an error has occurred (Throw the exception)
3. Receive the error information (Catch the exception)
4. Take corrective actions (Handle the exception)
Common Java Exceptions
ArithmeticException - Caused by maths errors such as division by zero
ArrayIndexOutOfBoundsException - Caused by bad array indexes
IOException - Caused by general I/O failures
Exception Handling Code

Format
…………..
try
{
Statement; // generates an exception
}
catch (Exception-type e)
{
Statement;
}
……………
…………...
Try Block
The try block can have one or more statements that could generate an exception. If
any one statement generates an exception, the remaining statements in the block are skipped
and execution jumps to the catch block
Catch Block
The catch block contains one or statements that are necessary to process the
exception. Every try statement should be followed by at least one catch statement.
Finally Statement
It can be used to handle an exception that is not caught by any of the previous catch
statements.
Example:
class Error3
{
public static void main(String args[])
{
int a = 10;
int b = 5;
int c = 5;
int x, y;
try
{
x = a / (b-c); //Exception here
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
y = a / (b+c);
System.out.println("y =" +y);

}
}
throw keyword
In Java, throw keyword is used to explicitly throw an exception.
Example:
public class TestThrow
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("Not valid");
else
System.out.println("Welcome to Vote");
}
public static void main(String args[])
{
validate(13);
}
}
Q15. Explain creating array of objects in Java with an example
Array of objects can be created in Java just like creating an array of primitive data
types.
Array of integers
int numbers[] = new int[size];
Array of objects
Student students[] = new Student[3];
Here students is an array containing objects of class Student

You might also like