_Java Programming
(3350703)
Assignment -3
1. Write a short note on class in java with example.
answer:-
A class is declared using class class keyword.
A class contain both data and code that operate on that data.
The data or variable defined within a class are called instance variables and the code that operates on this data is known as methods.
A class describes objects that has common properties.
A class in java contains:
-Method
-Constructor
Example:-
2. Write two steps to create an object.
answer:-
There are three steps to create an object.
2) Instantiation
3) Initialization
Instantiation:-
The new keyword is used to create the object.
Syntax is :- ClassName objName = new ClassName();
Instantiation mean to call the constructor of a class that creates an object of the type of that class.
Initialization:- Using a call to a constructor. this call initializes the new object.
The new operator is followed by a call to a constructor, which initializes the new object.answer:-
Accessing control is the process of controlling visibility of a variable or method.
Each object has member which can be declared to have specific access.
Java has 4 level and 3 access modifiers.
Access levels are listed below in the least to most restrictive order.
Public:- Public members are available anywhere outside the class.
Protected:- Accessible by subclases and form classes of same package.
Default:- Accessible only by classes in the same package.
Private:- Strictly controlled, which means they cannot be accessed by anywhere outside the enclosing class.
4. Explain this keyword with example.
answer:-
"this" keyword is used to refer to current object.
"this" keyword is always a reference to the object on which method was invoked.
"this" keyword can be used to invoke current class constructor.
"this" keyword can be passed as an argument to another method.
Example:-
Student.java
5. Explain static variable, method and block in java.
answer:-
Static variable:-
Instance variables declared as static are, essentially, global variables.
When objects of its class are declared, no copy of a static variable is created.
Data stored in static variables is common for all the objects of that class.
Static Method:-
Static Methods can access class variables without using object of the class.
It can access non-static methods and non-static variables by using objects.
Static methods can be accessed directly in static and non-static methods.
Static Block:-
Static Block gets executed when the class is loaded in the memory.
A class can have multiple Static Blocks, which will execute in the same sequence in which they have been written into the program.
Static block is a set of statements, which will be executed by the JVM before execution of main method.
6. Explain final variable, method and class in java.
answer:-
Final keyword is used to make data, method or class as final.
it means it can not be modified further in the program.
final at variable level:-
Final keyword is used to make a variable as a constant.
A variable declared with the final keyword cannot be modified by the program after initialization.
final at method level:-
It makes a method final, means sub classes can not override this method.
The compiler checks and gives an error if you try to override the method.
final at class level:-
It makes a class final, meaning that the class can not be inheriting by other class.
When we want to restrict inheritance then make class as a final.
7. Explain method overloading with example.
answer:-
If two or more methods in a class has same name but different parameters, it is known as method overloading.
Method overloading is a type of polymorphism.
If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.
Example:-
8. What is constructor? Write its properties. List different types of constructor.
answer:-
A constructor initializes an object when it is created.
It has the same name as its class and do not have any return type.
All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero.
However, once you define your own constructor, the default constructor is no longer used.
A constructor is used to initialize a new object.
Constructor is automatically called immediately when the object is created using "new" key-word.
Constructors are not methods, they aren't inherited.
There are Three Types Of Constructors:-
-Parameterized Constructor
-Copy Constructor
9. Explain default constructor with example.
answer:-
If you don't define any constructor in class, Java will generate a constructor by default
which is called as Default Constructor.
Example:-
Student.java
10. Explain parameterized constructor with example.
answer:-
The constructors with arguments is known as parameterized constructor.
Example:-
Student.java
Student2.java
11. Explain copy constructor with example.
answer:-
There are many ways to copy the values of one object into another. They are:
-By assigning the values of one object into another
-By clone () method of Object class
A copy constructor is used to initialize an object from another object, Copy constructor takes object as argument.
Example:
Student.java
12. What is constructor overloading? Demonstrate with example.
answer:-
Constructors can also be overloaded.
Overloaded constructor are differentiated on the basis of their type of parameters or number of parameters.
Whereas in Constructor overloading you have multiple constructor with different signature but only difference is that constructor doesn't have return type in java .
Example:
13. How an object can be passed as parameter?
answer:-
We can also pass object as a parameter to method in Java programming.
We learn Passing object as parameter conepts by below example:
Example of Passing Object as Parameter
14. Write a program in JAVA that implements a class with instance variable and method as class member.
answer:-
15. Write a program in JAVA that implements the class Box with attributes length, width,
height and calculate the volume of the Box using class method.
answer:-
16. Write a program in JAVA that implements the class Box with attributes length, width,
height and method setdata to assign the value to these attributes and also calculate
the volume of the Box using class method volume.
answer:-
2 Comments
Q 14 and Q 15 kyu nai hai
ReplyDeleteThe answer may be wrong so that why i not uploaded that
Deleteapology for that.