Unit 1 answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Q.1: Explain the features of OOP.

The following are the features of OOP


1. Classes: A class is a user-defined blueprint or prototype from which
objects are created.
•You can create multiple objects with the same behavior instead of
writing their code multiple times
•It can have as many methods as it wants to retrieve the value of
different types of methods
2. Objects: Objects are used to store data and values.
•Variables and methods are used to define the behaviour or function
of that object.
3. Encapsulation: It is the way toward restricting object’s state (fields)
and behaviours (methods) together in a single entity called "Class".
•We can limit the access to the methods of a class utilizing access
modifiers.
•Whenever a class is created in Java, it implies we are doing
encapsulation.
• It is also known as data hiding.
4. Abstraction: Abstraction is an enhancement of encapsulation.
•It is used to show only required data and hide rest of the data of an
object from the client.
• It can be done in 2 ways: Using Abstract Class, Using Interface
5. Inheritance: By using the concept of inheritance we can inherit few/all
properties of another object.
•Reusability is a significant benefit of using inheritance
• Inheritance use two types of class: Superclass and subclass
Superclass is also known as parent or base class and subclass is
also known as derived or child class
6. Polymorphism: Polymorphism provides us the flexibility to define an
object in multiple forms.
•The main use of polymorphism in OOP is when reference of a parent
class is made to a child class object.
• It must be supported by all languages based on OOP.
Q2. Explain fundamental programming structure in java with example .
First Java Program

Java is an object oriented programming language, so the complete


program must be written inside a class. Let’s consider an example
Class FirstProgram

{
Public static void main(String[] args)
{
System.out.println(“My first program”);
}
}

Save the above file as FirstProgram.java Prerequisites Before start writing


a java program
Before writing java program one must make sure that following software or
applications are properly installed.

1. First of all, download JDK and install it.


2. Set path of the jdk/bin directory.
3. Create the Java program.
4. Compile and run the program.

Besides this learning Java is easy doesn’t require any previous knowledge
of other programming language but requires some logics for writing
programs.

The syntax of writing a Java program is similar to that of C.

Writing the program


To write a program, first of all open the notepad by following the sequence
start menu -> All Programs -> Accessories -> Notepad and then write the
below code.
Class FirstProgram
{
Public static void main(String[] args)
{
System.out.println(“My first program”);
}
}

After writing the code save the file as FirstProgram.java.

Different ways to write a Java program


1. Sequence of the modifiers doesn’t matter, prototype of method will be
same.
Static public void main(String args[])

2. The string array can be written in many ways all have the same
meaning.
Public static void main(String[] args) Public static void main(String []args)
Public static void main(String args[])
3. By using three ellipses we can provide var-args to the main().
Public static void main(String...args)
4. Placing a semicolon at the end of class is optional.

Class FirstProgram
{
Static public vid main(String...args)
{
System.out.println(“My first program”);
}
};

Executing the program.


At the time of execution, the main class file is passed to JVM (Java Virtual
Machine) which the goes through following stages:
Class file-> Class loader ->Bytecode Verified -> Interpreter-> Runtime ->
Hardware

Q3. ARCHITECTURE OF JVM


1. Class Loader: This part is used to load class files. There are three
major functions of class loader as Loading, Linking and Initialization.
2. Method Area: Structure of class like metadata, the constant runtime
pool, and the code for methods are stored method area.
3. Heap: Heap is the memory where all the objects, instance variables
and arrays are stored. This is a shared memory which is accessed by
multiple threads.
4. JVM Language Stacks: These hold the local data variables and their
results. Each thread has their own JVM stack used to store data.
5. PC Registers: The currently running IVM instructions are stored in PC
Registers. In Java, every thread has its their own PC register.
6. Native Method Stacks: It hold the instruction of native code
depending on their native library.
7. Execution Engine: This engine is used to test the software, hardware
or complete system. It doesn’t contain any information about the
tested product.
8. Native Method Interface: It is a programming interface. It allows Java
code to call by libraries and native applications.
9. Native Method Libraries: It consists of native libraries which are
required by execution engine.

Q. 4 Constructor and types

Constructor
Constructors perform the job of initializing an object. It resembles a method
in Java, but a constructor is not a method and it doesn’t have any return
type.

Properties of a constructor:
1. Name of the constructor is the same as its class name.
2. A default constructor is always present in a class implicitly.
3. There are three main types of constructors namely default,
parameterized and copy constructors.
4. A constructor in Java can not be abstract, final, static and
Synchronized.

1. Default constructor:
A default constructor is a constructor that is created implicitly by the JVM.
Program to demonstrate default constructors:
Class student
{
Int id;
String name; Student()
{
System.out.println("Student id is 20 and name is Amruta");
}
Public static void main(String args[])
{
Student s = new student();
}
}

2. Parameterized constructor:
It is a constructor that has to be created explicitly by the programmer. This
constructor has parameters that can be passed when a constructor is
called at the time of object creation.
Program:
Class Student4
{
Int id;
String name; Student4(int i,String n)
{
Id = i; Name = n;
}
Void display()
{
System.out.println(id+" "+name);
}
Public static void main(String args[])
{
Student4 s1 = new Student4(20,"Rema");
Student4 s2 = new Student4(23,"Sadhna");
s1.display();
s2.display();
}
}

3. Copy constructor:
A content of one constructor can be copied into another constructor using
the objects created. Such a constructor is called as a copy constructor.

Program:
Class Student6
{
Int id;
String name; Student6(int i,String n)
{
Id = i; Name = n;
}
Student6(Student6 s)
{
Id = s.id;
Name =s.name;
}
Void display()
{
System.out.println(id+" "+name);
}
Public static void main(String args[])
{
Student6 s1 = new Student6(20,"AMRUTA");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}

Q. static method, variable

A static variable is defined as a variable that has been declared as static.


The static variable can be used to refer to a property that is shared by all
objects The static variable is only stored in memory once in the class area,
when the class is loaded.

//Java Program to demonstrate the use of static variable


Class Student{
Int rollno;//instance variable
String name;
Static String college ="ITS";//static variable
//constructor Student(int
r, String n){ Rollno = r;
Name = n;
}
//method to display the values
Void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
Public class TestStaticVariable1{
Public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT"; s1.display();
s2.display();
}
}

Output
111 Karan ITS
222 Aryan ITS

Static method
Any method that uses the static keyword is referred to as a static method.

A class's static method, rather than the class's object, belongs to the class.
A static method can be called without having to create a class instance.
A static method can access and update the value of a static data member.

With variables, methods, blocks, and nested classes, we can use the static
keyword. The static keyword refers to a class rather than a specific
instance of that class

//Java Program to demonstrate the use of a static method.


Class Student{
Int rollno; String name;
Static String college = "ITS";
//static method to change the value of static variable
Static void change(){
College = "BBDIT";
}
//constructor to initialize the variable Student(int
r, String n){
Rollno = r;
Name = n;
}
//method to display values
Void display(){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to create and display the values of object Public class
TestStaticMethod{
Public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method s1.display();
s2.display();
s3.display();
}
}

Output

111 Karan BBDIT


222 Aryan BBDIT
333 Sonoo BBDIT

Q. Diferent data types Q. Primitive data type


In Java, there are two types of data types:

Primitive data types - Boolean, char, byte, short, int, long, float, and double
are examples of primitive data types.
Non - primitives data types - Classes, Interfaces, and Arrays are examples
of non-primitive data types.

Primitive data types


Primitive data types are the building blocks of data manipulation in the Java
programming language. These are the most basic data types
1. Boolean:Only two potential values are stored in the Boolean data
type: true and false. Simple flags that track true/false circumstances
are stored in this data type.
2. Byte: The byte data type is used to preserve memory in huge arrays
where space is at a premium. Because a byte is four times smaller
than an integer, it saves space. It can also be used in place of "int"
data type.
3. Short: The short data type, like the byte data type, can be used to
save memory. A short data type is twice the size of an integer.
4. Int: Unless there is a memory constraint, the int data type is usually
chosen as the default data type for integral values.
5. Long: A 64-bit two's complement integer is the long data type. It has
a value range of - 9,223,372,036,854,775,808(-263 -1) to
9,223,372,036,854,775,807(263 -1). (inclusive) Its lowest and
maximum values are 9,223,372,036,854,775,808 and
9,223,372,036,854,775,807. It has a value of 0 by default. When you
need a larger range of values than int can supply, you should utilize
the long data type.
6. Float: The float data type is a 32-bit IEEE 754 floating point with
single precision. It has an infinite value range. If you need to preserve
memory in big arrays of floating point integers, use a float (rather than
a double). For precise numbers, such as currency, the float data type
should never be used. 0.0F is the default value.
7. Double: A double data type is a 64-bit IEEE 754 floating point with
double precision. It has an infinite value range. Like float, the double
data type is commonly used for decimal values. For precise values,
such as currency, the double data type should never be utilized. 0.0d
is the default value.

You might also like