OOP
OOP
a. Class/Template,
A class is a collection of objects.That defines and describes the attributes
and behaviors.It is a user defined data type that act like a template,plan for creating objects.
b. Object/Instance,
An object is a real world entity that has attributes(properties) and
behaviors.object also called an instance of class.
c. Method/Functions,
A method is a block of code only runs when it is called.We can pass data
known as parameters into a method.Methods are declared within a class and they are
used to perform certain actions and they are also known as functions.
d. Properties/Attributes,
Properties are declared within a class and have specific data types
that determine the kind of data they can store.By defining properties in a class,we
create a blueprint for objects with specific attributes.
e. Reference variables,
In oop reference variables are variables that store memory address or
reference to object rather than directly holding the actual objects data.Which means
that is a variable that points to an object of a given class letting user o access the
value of an object.
f. Primitive variables,
A primitive type variable can store exactly one value of it’s type at a
time.Primitive type instance variables are initialized by default.Variables of types
byte,char,short,int,long,float and double are initialized to 0 while other types boolean
true or false and String as null.
g. Method parameters,
Int oop method parameters refer to the variables that are defined within
a methods parentheses and used to accept input values from the caller.
h. Local variables,
In java a local variable is a variable that is declared within a specific code
block or a function(method).Local variables have limited scope and are accessible only within
the block in which they are declared.
i. Default values,
In java default values are values assigned by the compiler to the
variables(primitive data types and non primitive data types) which are declared but not
initialized or given a value.Both primitive data types and non primitive data types have it’s
own default value.
j. Declaration values,
The process of defining a variable along with it’s data type and name is
called the declaration values also known as declaration of state of an object.
3. A
4.
Attributes includes both non primitive and primitive data types, and methods include
arguments or properties with their data types.
In a class Car, the Color attribute would represent the color of the car.In Car class the start();
method represents the action of starting the car’s engine.
In a class Person, the Age attribute would represent the age of the person.In Person class the
Work(); method represent the action of person working.
5.
Line 2,3 and 4 are illegal because the System out in the main method is trying to access the
properties in the class Box through the object b1.
6.
The output will be,
Volume : 0
length of box : 0
width of box : 0
height of box : 0
because the properties in the class box already have a default value and it done by the
compiler.
7.
The output will be,
Volume :180
length of box : 12
width of box : 5
height of box : 3
8.
The output will be,
default constructor
length of box : 2
width of box : 2
height of box : 2
Because the program first runs the constructor of the Box class and therefor the first output is
“default constructor” because when a class Box object created using new Box the constructor
is a special method that is automatically called when an object is created and then the rest of
the code will be continues to run.
9.
The output will be,
Parameterized constructor
Volume : 60
Parameterized constructor
Volume : 180
Because the program first runs the constructor of the Box class and therefor the first output is
“parameterized constructor” because when a class Box object created using new Box the
constructor is a special method that is automatically called when an object is created and then
the program will come to the main method form there now a new object is created the the
values of the properties that values will assign with the attributes in the constructor after that
the volume will be print.
10.
This code will not successfully complied and run.Because the width property is marked as
private which means it only can accessed by within the box class and it not directly accessible
from the main method.
11.
The lines 4,5 and 6 are illegal because these lines are assignments to the attributes
length,width and height after the variables are declared.But the reason these are illegal
because java does not allow direct statement to be placed directly within a class.
12.
The constructor is similar to the method.The property of the constructor is that it must have
the same name as the class name and it has no return type and we do not need to call the
constructor manually it automatically invoked when the program is running.They are
responsible for creating and setting up the object so that it is in a valid and usable state.
13.
Constructors
Constructors are special methods with the same name as the class, and they
are automatically called when an object of the class is created using the new
keyword.The primary purpose of constructors is to initialize the state of an object and
set its initial values.Constructors do not have a return type, not even void, and cannot
be called explicitly like regular methods.They are responsible for creating and setting
up the object so that it is in a valid and usable state.
Example : if the user creates a Car class. When a new Car object is formed,
the constructor of the Car class might be in charge of initializing the car's attributes,
such as defining the car's color, brand, and weight. It makes sure that the car object is
prepared for use and starts with accurate information.
Methods
A method is a block of code only runs when it is called.We can pass data known as
parameters into a method.Methods are declared within a class and they are used to
perform certain actions and they are also known as functions.
Example : Keeping with the Car class example, startEngine() is a method in
the Car class that, when called, simulates the operation of starting the car's engine.
Another approach is drive(int distance), which uses the distance as an input and
advances the vehicle appropriately.
14.
B,D and E
15.
This code will not compile and therefore it will give compiler errors because the
Sustem.out.print is not a statement in java.
16.
C
17.
A,B,C and D
18.
The output will be,
100 101
00
The reason for this output is first class A have a property int a and within that class the A
constructor is created to take the values from the object a1 and a2. after that the system out
will print the values f a1 and a2 which is 100 and 101.After that in the a1 object a2 is
assigning it’s values to the a1 then again using the a1 object int a is assigned by 0.Now it will
have the value of 0.Because the statement of a1 = a2 now both a1 and a2 have the same
values because now a is 0 it will print 0 0.
19.
The output will be,
3001
The reason is that the code attribute in object t1 of class Item is set to 2001 when it is created
using new Item(2001).The code value provided, which is 3001, is printed when the method
t1.printCode(3001) is invoked. The printCode method's local variable code hides the Item
class's attribute code. Therefore, rather than printing the 2001 attribute code of t1 as requested,
the method displays the given value 3001.
20.
The output will be,
2001
Because now even the ti.printCode(3001) is declared, In the class Item printCode method now
uses the keyword this to refer the attribute code of the object not the parameter (3001).Since
the attribute code is set to 2001 it prints the 2001.
21.
B,C and D
22.
A,D,E
23.
encapsulation describes binding data and methods that work on that data within one unit, like
a class in Java. We often often use this concept to hide an object’s internal representation or
state from the outside. This is called information hiding.It other words, only member
functions defined in a class will have access to the data.In encapsulation properties are
declared as private and methods are declared as public.
class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
24.
In a tightly encapsulated design, the class keeps its internal attributes and methods well-
enclosed and hidden from the outside world.Which means that the attributes of the calss is
declared as private.
In a loosely encapsulated design, the class might have some data members declared as public
or protected, which exposes its internal data to the outside world.
25.
The output will be C because the System out is declared in the class template and that is
illegal in java.The class variables and methods should be declared inside the class methods.
26.
class Date{
private int year = 1970;
private int month = 1;
private int day = 1;
/*d1.year = 2016;
d1.month = 5;
d1.day = 31;*/
d1.setYear(2016);
d1.setMonth(5);
d1.setDay(31);
27.
In Demo class in line 1 the user has declared a reference Customer c1.
In line 2,using line 1 reference the user has declared a new object and in line 2 directly pass
the values to the attributes in the Customer class.Which are id and name.
In line 3 all the values are printed using the method printCustomer().
28.
A and B.
29.
E
30.
import java.util.*;
class Rectangle{
private double length = 1.0;
private double width = 1.0;
Rectangle(){}
Rectangle(double l,double w){
length = l;
width = w;
}
public void setLength(double l){
if (l > 0.0 && l < 20.0){
this.length = l;
}else{
System.out.println("Invalid length");
}
}
public double getLength(){
return length;
}
public void setWidth(double w){
if (w > 0.0 && w < 20.0){
this.width = w;
}else{
System.out.println("Invalid width");
}
}
public double getWidth(){
return width;
}
public double getPerimeter(){
return 2 * (length + width);
}
public double getArea(){
return length * width;
}
}
class Example{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
31.
A,C
32.
F
33.
Line 5 and Line 8
34.
Instance variables are created when an object is created with the use of the keyword 'new' and
destroyed when the object is destroyed.Static variables are created when the program starts
and destroyed when the program stops.Which means static methods can be called without the
object of the class. Instance methods require an object of the class. Static methods are
associated with the class. Instance methods are associated with the objects.
35.
A,D,F and I
36.