OOM Assignment
OOM Assignment
Assignment -1
Ans. A programming language is a vocabulary and set of grammatical rules for instructing a
computer or computing device to perform specific tasks.
Ans. Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
o Message Passing
o Dynamic Binding
1. Object
Any entity that has state and behaviour is known as an object. For example, a chair, pen,
table,
up some space in memory. Objects can communicate without knowing the details of
each
other's data or code. The only necessary thing is the type of message accepted and the
type
OOM ASSIGNMENTS
Example: A dog is an object because it has states like colour, name, breed, etc. as well
as
2. Class
A class can also be defined as a blueprint from which you can create an individual
object.
3. Inheritance
When one object acquires all the properties and behaviours of a parent object, it is
known as
4. Polymorphism
convince the customer differently, to draw something, for example, shape, triangle,
rectangle,
etc.
Another example can be to speak something; for example, a cat speaks meow, dog
barks
woof, etc.
5. Abstraction
Hiding internal details and showing functionality is known as abstraction. For example,
phone
6. Encapsulation
Binding (or wrapping) code and data together into a single unit are known as
encapsulation.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
7. Message Passing:
Objects communicate with one another by sending and receiving information to each
other. A
message for an object is a request for execution of a procedure and therefore will invoke
a
function in the receiving object that generates the desired results. Message passing
involves
specifying the name of the object, the name of the function and the information to be
sent.
8. Dynamic Binding
Dynamic binding also called dynamic dispatch is the process of linking procedure call to
a
specific sequence of code (method) at run-time. It means that the code to be executed
for a
specific procedure call is not known until run-time. Dynamic binding is also known as
Ans, It is easy to model a real system as real objects are represented by programming
objects in OOP. The objects are processed by their field (member data) and methods
• With the help of inheritance, we can reuse the existing class to derive a new class such
that the redundant code is eliminated and the use of existing class is extended. This
• In OOP, data can be made private to a class such that only member functions of the
class can access the data. This principle of data hiding helps the programmer to build
a secure program that cannot be invaded by code in other part of the program.
• With the help of polymorphism, the same function or same operator can be used for
• Large problems can be reduced to smaller and more manageable problems. It is easy
interference i.e. each object has its own separate field (member data) and methods
(functions).
OOM ASSIGNMENTS
Assignment -2
Ans. Java is a programming language and a platform. Java is a high level, robust, object-
oriented and secure programming language. Ø Java was developed by Sun Microsystems (which
is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java.
Before Java, its name was Oak. Since Oak was already a registered company, so Ja mes Gosling
and his team changed the Oak name to Java.
Ans. A variable is a name which is associated with a value that can be changed. For example
when I write int i=10; here variable name is i which is associated with value 10, int is a data type
that represents that this variable can hold integer values.
Ø In the figure below, 5 phases of the Java Program are described clearly with edit,
Ø Phase 1: Edit
Ø We create the program on editor, after that it stored in the disk with the
Ø Compiler translate from high -level language program to byte codes and store
Ø Phase 3: Load
Ø Class loader compile read and put those byte codes from disk to Primary
Memory.
Ø Phase 4: Verify
Ø Verify byte codes to confirm that all byte codes are valid and do not risk for
Ø Phase 5: Execute
Ø Java Virtual Machine (JVM) read and translates those byte codes to language
Ans. Ø Java Virtual Machine (JVM) is a engine that provides runtime environment to drive
1) Class Loader
The class loader is a subsystem used for loading class files. It performs
2) Method Area
JVM Method Area stores class structures like metadata, the constant
3) Heap
All the Objects, their related instance variables, and arrays are stored in
the heap. This memory is common and shared across multiple threads.
Java language Stacks store local variables, and it’s partial results. Each
thread has its own JVM stack, created simultaneously as the thread is
5) PC Registers
OOM ASSIGNMENTS
register.
7) Execution Engine
systems. The test execution engine never carries any information about the
tested product.
The Native Method Interface is a programming framework. It allows Java code which is
running in a JVM to call by libraries and native applications.
here value is optional because in java, you can declare the variable first and then
int num;
OOM ASSIGNMENTS
Ø Similarly we can assign the values to the variables while declaring them, like this:
char ch = 'A';
char ch;
int number;
...
ch = 'A';
number = 100;
OOM ASSIGNMENTS
Assignment -3
Ans. A Java method is a collection of statements that are grouped together to perform an
operation. When you call the System.out.println() method, for example, the system
Ans. is created from a class. In Java, the new keyword is used to create new objects.
SYNTAX:
EXAMPLE:
Ans. A constructor in Java is a special method that is used to initialize objects. The
constructor
In Java, a constructor is a block of codes similar to the method. It is called when an instance
OOM ASSIGNMENTS
of the class is created. At the time of calling constructor, memory for the object is allocated
in the memory.
DEFAULT CONSTRUCTOR:
If you do not implement any constructor in your class, Java compiler inserts a default
constructor into your code on your behalf. This constructor is known as default constructor.
You would not find it in your source code(the java file) as it would be inserted into the code
NO-ARG CONSTRUCTOR:
default constructor, however body can have any code unlike default constructor where
PARAMETERIZED CONSTRUCTOR:
constructor.
OOM ASSIGNMENTS
Assignment -4
Ans. StringBuilder objects are like String objects, except that they can be modified.
At any point, the length and content of the sequence can be changed through
method invocations.
Ans. Java StringBuffer class is used to create mutable (modifiable) string object. A string
buffer is like a String, but can be modified.
Every string buffer has a capacity. As long as the length of the character sequence contained in
the string buffer does not exceed the capacity, it is not necessary to allocate a new internal
buffer array. If the internal buffer overflows, it is automatically made larger.
Ans. Java String length(): The Java String length() method tells the length of the string. It
returns count of total number of characters present in the String. For example:
String s1="hello";
String s2="whatsup";
}}
Here, String length() function will return the length 5 for s1 and 7 for s2 respectively.
Java String concat() : The Java String concat() method combines a specific string at the
end of another string and ultimately returns a combined string. It is like appending
String s1="hello";
System.out.println(s1);
}}
Java String IsEmpty() : This method checks whether the String contains anything or
not. If the java String is Empty, it returns true else false. For example:
String s1="";
String s2="hello";
System.out.println(s1.isEmpty()); // true
System.out.println(s2.isEmpty()); // false
}}
Java String Trim() : The java string trim() method removes the leading and trailing
spaces. It checks the unicode value of space character (‘u0020’) before and
after the
string. If it exists, then removes the spaces and return the omitted string. For
example:
}}
In the above code, the first print statement will print “hello how are you” while
the
second statement will print “hellohow are you” using the trim() function.
OOM ASSIGNMENTS
Java String toLowerCase() : The java string toLowerCase() method converts all
the
String s1lower=s1.toLowerCase();
System.out.println(s1lower);}
Ø Changes made to formal parameter do not get transmitted back to the caller.
Ø Any modifications to the formal parameter variable inside the called function
or method affect only the separate storage location and will not be
reflected in
Call by reference(aliasing):
Ø Any changes to the formal parameter are reflected in the actual parameter in
Assignment – 5
Ans. Ø Inheritance is a mechanism in which one object acquires all the properties and
Multiple Inheritance
Multilevel Inheritance
Hierarchial Inheritance
Hybrid Inheritance
Ans. Ø In multiple inheritance there exist multiple base classes and single derived class.
Program:
class A
void msg()
System.out.println("Hello");
class B
void msg()
{
OOM ASSIGNMENTS
System.out.println("Welcome");
{ //suppose if it were
C obj=new C();
Ø Output:
Ø In above code we call both class A and class B msg() method then it confusion which
class method is call. So due to this ambiguity problem in java do not use multiple
Ans. When more than one classes inherit a same class then this is called hierarchical
Ø As you can see in the above diagram that when a class has more than one child classes
(sub classes) or in other words more than one child classes have the same parent class
Program:
class Animal{
void eat()
System.out.println("eating...");
void bark()
System.out.println("barking...");
void meow()
System.out.println("meowing...");
class TestInheritance3
c.meow();
c.eat();
//c.bark();//C.T.Error
Ø Output:
meowing...
eating...
Ø Program:
class A
{
OOM ASSIGNMENTS
class B extends A
class C extends A
class D extends A
class JavaExample
obj1.methodA();
obj2.methodA();
obj3.methodA();
Ø Output:
method of Class A
method of Class A
method of Class A
OOM ASSIGNMENTS
Assignment-6
Ans. Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
Polymorphism uses those methods to perform different tasks. This allows us to perform a single
action in different ways. For example, think of a superclass called Animal that has a method
called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also
have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):
Method Overloading is a feature that allows a class to have more than one method
having the same name, if their argument lists are different. It is similar
to constructor overloading in Java, that allows a class to have more than one
let’s get back to the point, when I say argument list it means the parameters that
a method has: For example the argument list of a method add(int a, int b) having
two parameters is different from the argument list of the method add(int a, int b,
This example shows how method overloading is done by having different number
of parameters
OOM ASSIGNMENTS
class DisplayOverloading
System.out.println(c);
class Sample
obj.disp('a');
obj.disp('a',10);
Output:
a 10
Declaring a method in sub class which is already present in parent class is known
as method overriding. Overriding is done so that a child class can give its own
case the method in parent class is called overridden method and the method in
child class is called overriding method. In this guide, we will see what is method
OOM ASSIGNMENTS
Lets take a simple example to understand this. We have two classes: A child class
Boy and a parent class Human. The Boy class extends Human class. Both the classes
have a common method void eat(). Boy class is giving its own implementation to
class Human{
//Overridden method
System.out.println("Human is eating");
//Overriding method
System.out.println("Boy is eating");
obj.eat();
Output:
Boy is eating
OOM ASSIGNMENTS
Assignment – 7
Ans. Package in Java is a mechanism to encapsulate a group of classes, sub packages and
interfaces.
Ans, Package names and directory structure are closely related. For example if a
directories, college, staff and cse such that cse is present in staff and staff is
present in CLASSPATH. The idea is to make sure that classes are easy to
locate.
Ans. The only time we need to pay attention to packages is when we have a
name conflict . For example both, java.util and java.sql packages have a
import java.util.*;
import java.sql.*;
//And then use Date class, then we will get a compile-time error :
The compiler will not be able to figure out which Date class do we want.
import java.util.Date;
import java.sql.*;
If we need both Date classes then, we need to use a full package name
For Example:
Ans. These are the packages that are defined by the user. First we create a
Then create the MyClass inside the directory with the first statement being
package myPackage;
System.out.println(s);
import myPackage.MyClass;
// with a value
// the package.
obj.getNames(name);
}}
OOM ASSIGNMENTS
Assignment-8
Ans, In Java, an exception is an event that disrupts the normal flow of the program. It is an
object which is thrown at runtime.
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application; that is
Ans. A method catches an exception using a combination of the try and catch keywords.
A try/catch block is placed around the code that might generate an exception. Code
within a try/catch block is referred to as protected code, and the syntax for using
Syntax
try {
The following is an array declared with 2 elements. Then the code tries to access the
import java.io.*;
try {
} catch (ArrayIndexOutOfBoundsException e) {
Output
Returns a detailed message about the exception that has occurred. This
getMessage().
Prints the result of toString() along with the stack trace to System.err, the
Returns an array containing each element on the stack trace. The element
at index 0 represents the top of the call stack, and the last element in the
Fills the stack trace of this Throwable object with the current stack trace,