Module 1-2
Module 1-2
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.
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
}
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.
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.
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);
}
}
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