0% found this document useful (0 votes)
4 views

Computer Notes

Uploaded by

Shubhadeep Nag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Computer Notes

Uploaded by

Shubhadeep Nag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

COMPUTER

Introduction to Object Oriented Programming Concepts


III. 1. Object Oriented Programming is an approach in which stress is laid on data rather than
functions. The data values remain associated with the functions of a particular block of the
program so as to encourage data security. Two Object Oriented Programming languages are C++
and Java.
2. Four basic principles of object-oriented programming are:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
3. Object Oriented approach offers advantages like:
1. Data Values are secured.
2. Mishandling of data is protected.
3. Error detection and correction becomes easier.
4. Easier in coding complex programs.
For these reasons, Object Oriented approach is preferred for complex programming.
4. The class that is inherited is known as base class and the class that inherits from the base is
known as derived class.
5. Two limitations of procedure-oriented programming approach are:
1. No restriction on data values.
2. Limited and difficult code reusability.
6.Wrapping of data and functions that operate on that data into a single unit is called
Encapsulation.
7.a. Inheritance is a property by virtue of which one class acquires some features from another
class. It promotes reusability.
As an example of inheritance, we can consider the case of vehicles. All vehicles have some common
features like they can move on the road and transport people and goods from one place to another.
These vehicles differ from each other in certain aspects like whether it transports passengers or
goods, how many passengers it can accommodate at a time, whether it is a two-wheeler or
fourwheeler, etc. So, we have different types of vehicles like Cars, Bikes, Scooters, Auto rickshaw,
Buses, Trucks, etc. Using inheritance, we can make vehicles the base class with the different types
of vehicles being the derived class as shown below:
b. Polymorphism is the process of using a function for carrying multiple operations. During this
process, an object may include a function for multiple operations.
As an example, if we ask different animals to speak, they respond in their own way. Dog will
bark, duck will quack, cat will say meow and so on. So, the same action of speaking is performed in
different ways by different animals. This is the concept of Polymorphism.
8. Data Hiding and Data Abstraction are complementary concepts. Data Abstraction focuses on the
observable behaviour of an object, whereas Data hiding or Data Encapsulation focuses upon the
implementation that gives rise to this behaviour. In other words, Data Abstraction cares about
what something does but not how it does it. Data Encapsulation cares about how something does
what it does such that others don't have to worry about the implementation details. Hence, we can
say that Encapsulation is a way to implement Data Abstraction.
9. Data abstraction is an act of representing the essential features without knowing the background
details.
As an example,let`s consider the electrical switchboard.Switchboard provides us a very simple way
to switch ON/OFF lights, fans and other electrical appliances. It hides all the details like the
internal wiring of the house, how the switch is turning the light ON/OFF, etc.
10. The insulation of data that does not allow it to be accessed directly outside the class premises,
although it is available in the same program is known as Data Hiding.
11. Two differences between Data Hiding and Encapsulation are as follows:
1. Data hiding focuses more on data security whereas, encapsulation focuses more on hiding the
complexity of the system.
2. Data hiding focuses on restricting the use of data whereas, encapsulation deals with wrapping
of data and functions.
12. Encapsulation means wrapping of data and methods into a single unit and restricting direct
access to some of the object's components.
Let us take the example of an ATM machine. When we want to withdraw money from an ATM
machine, we swipe our card, enter the pin and then enter the amount of money we want to
withdraw. The ATM machine processes our request and gives us the required money.
Here, money is treated as data and the various processes such as verification of the authenticity of
the user, pin and balance in the account etc., are the methods. The data and methods are
encapsulated in the ATM (which is treated as a class). Since the user doesn't have to understand
the actual working of the ATM machine, encapsulation reduces the complexity and makes the
system (ATM machine) easier to use.
13.
Procedure Oriented languages Object Oriented languages

Procedure Oriented languages lay emphasis on Object Oriented languages lay emphasis on
functions and procedures rather than data. data rather than functions.

Limited and difficult code reusability leading to Versatile and easy code reusability leading
lengthy programs which are difficult to debug and to simpler programs which are easier to
maintain. debug and maintain.

Values and Data Types


III.3.a. Variables are identifiers that are used to name a data that holds a value in the memory.
The value can change depending upon our requirements in the program.
Example:
int mathScore = 95;
b. The keyword final before a variable declaration makes it a constant. Its value can't be changed
in the program.
Example:
final int DAYS_IN_A_WEEK = 7;
c. A boolean data type is used to store one of the two boolean values — true or false. The size of
boolean data type is 8 bits or 1 byte.
Example:
boolean bTest = false;
d. In a mixed mode expression, when the data type of the result gets converted into the higher
most data type available in the expression without any intervention of the user, is known as
Implicit Type conversion or Coercion.
Example:
int a = 42;
long b = 50000;
long c= a + b;
Here, a is int, b is long so the result of a + b automatically gets converted into long and assigned to
variable c which is of long type.
e. Primitive data types are the basic or fundamental data types used to declare a variable.
Examples of primitive data types in Java are byte, short, int, long, float, double, char, boolean.
f. A non-primitive data type is one that is derived from Primitive data types. A number of
primitive data types are used together to represent a non-primitive data type. Examples of non-
primitive data types in Java are Class and Array.
5. Type casting is a data conversion in which the data type of the result in a mixed mode
expression, gets converted into a specific type as per user's choice. The choice of data type must be
written within braces ( ) before the expression.
For example,
int a; float b; char c;
char d = (char) (a + b * c);
7.a. b.

8. The process of converting one predefined type into another is called type conversion. In an
implicit conversion, the result of a mixed mode expression is obtained in the higher most data type
of the variables without any intervention by the user. For example:
int a = 10;
float b = 25.5f;
c = a + b;
In case of explicit type conversion, the data gets converted to a type as specified by the programmer.
For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
10. In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example:
int mathScore = 100;
double p = 1.4142135;
char ch = 'A';
In dynamic initialization, the initial value of the variable is the result of an expression or the
return value of a method call. Dynamic initialization happens at runtime. For example:
int a = 4;
int b = Math.sqrt(a);
double x = 3.14159, y = 1.4142135;
double z = x + y;
In static initialization, the initial value of the variable is provided as a literal at the time of
declaration. For example: int mathScore = 100; double p = 1.4142135; char ch = 'A'; In dynamic
initialization, the initial value of the variable is the result of an expression or the return value of a
method call. Dynamic initialization happens at runtime. For example: int a = 4; int b =
Math.sqrt(a); double x = 3.14159, y = 1.4142135; double z = x + y;
12.(a) int m =155;
This assignment is correct as 155 is an integer literal and it is assigned to an int variable m.
(b) float f = 0.002654132; This assignment is incorrect as data type of 0.002654132 is double but
it is assigned to a float variable. The correct assignment will be float f = 0.002654132f;
(c) String str = 'Computer'; This assignment is incorrect as the String literal Computer is
enclosed in single quotes. It should be in double quotes. The correct assignment will be String str =
"Computer";
(d) boolean p = false; This assignment is correct as false is a valid boolean literal and it is
assigned to a boolean variable.
(e) String b = "true"; This assignment is correct as "true" is a string literal not a boolean literal
as it is enclosed in double quotes. It is correctly assigned to a String variable.
(f) char ch = "apps"; This assignment is incorrect as "apps" is a string literal not a character
literal and it is assigned to a variable ch of char data type.
(g) String st= "Application";
This assignment is correct as "Application" is a string literal and it is correctly assigned to a
String variable.
(h) double n = 455.29044125;
This assignment is correct as 455.29044125 is a literal of double data type and it is correctly
assigned to a double variable.

Operators in Java
V.1. An operator is a symbol or sign used to specify an operation to be performed in Java
programming.
The three main types of operators are Arithmetical, Logical and Relational. An operator is a
symbol or sign used to specify an operation to be performed in Java programming.
The three main types of operators are Arithmetical, Logical and Relational.
2.An expression is a set of variables, constants and operators i.e. an expression is a combination of
operators and operands. When an expression is assigned to a variable, the complete set is referred
to as a statement.
3.a. Arithmetic operators are used to perform mathematical operations on its operands. Operands of
arithmetic operators must be of numeric type. A few arithmetic operators operate upon one
operand. They are called Unary Arithmetic operators. Other arithmetic operators operate upon two
operands. They are called Binary Arithmetic operators.
As an example, consider the below statement:
int a = 10 + 20;
Here, the addition arithmetic operator, represented by the symbol + will add 10 and 20. So variable
a will be 30.
b. Relational operators are used to determine the relationship between the operands. Relational
operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less
than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The
result of an operation involving relation operators is a boolean value — true or false.
Example:
int a = 8;
int b = 10;
boolean c = a < b;
Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.
c. Logical operators operate on boolean expressions to combine the results of these boolean
expression into a single boolean value.
Example:
int a = 7;
int b = 10;
boolean c = a < b && a % 2 == 0;
Here, the result of first boolean expression a < b is true and the result of second boolean expression
a % 2 is false. The logical AND operator ( && ) combines these true and false boolean values and
gives a resultant boolean value as false. So, boolean variable c becomes false.
d. condition ? expression 1 : expression 2
Ternary operator evaluates the condition. If the condition is true then result of ternary operator is
the value of expression 1. Otherwise, the result is the value of expression 2.
Example:
boolean isLeapYear = true;
int febDays = isLeapYear ? 29 : 28;
Here, the ternary operator checks if the value of boolean variable isLeapYear is true or false. As it
is true, expression 1, which in this example is the value 29, is the result of the ternary operator. So,
int variable febDays becomes 29.
4.a. d.

c. b.

5.a. b.

Introduction to Java
IV.4.a. A set of statements written in a High-Level programming language like Java, C++, Python,
etc. is called as Source Code.
b. Machine code is a low-level programming language. Instructions in machine code are
written as a sequence of 0s and 1s. It is directly understood and executed by the processor.
c. Java compiler converts Java source code into an intermediate binary code called Bytecode.
Bytecode can't be executed directly on the processor. It needs to be converted into Machine Code
first.
5. BlueJ is an integrated development environment for Java. It was created for teaching Object
Oriented programming to computer science students.
Features of BlueJ are:
1. Simple beginner friendly graphical user interface.
2. It allows creating objects of the class dynamically, invoking their methods and also supplying
data to the method arguments if present.
3. It supports syntax highlighting. (Syntax highlighting means showing the different tokens of the
program like keywords, variables, separators, etc. in different colours so that they show up more
clearly.)
4. It facilitates easier debugging as lines causing compilation errors are marked clearly and the
error is displayed at the bottom of the window.
5. It provides a code editor, compiler and debugger integrated into a single tool.
6. We commonly use two output statements in Java. There syntax are:
1. System.out.println(); — This statement prints data on the console. The data to be printed is
passed to the println method as a String. After printing the string, it places the cursor at the start
of the next line. So, the next printing happens at the start of the next line.
2. System.out.print(); — This statement also prints data on the console. The data to be printed is
passed to the print method as a String. After printing the string, the cursor remains on the same
line at the end of the printed string. So, the next printing starts in the same line just after the end
of the previous printed string.
As an example, the below statements will generate the following output:
System.out.println("JVM stands for");
System.out.print("Java ");
System.out.print("Virtual ");
System.out.print("Machine");
Output
JVM stands for
Java Virtual Machine
7. In Java, a reserved word is a word that has a predefined meaning in the language. Due to this,
reserved words can’t be used as names for variables, methods, classes or anya System.out.println()
System.out.print() It prints data to the console and places the cursor in the next line. It prints data
to the console but the cursor remains at the end of the data in the same line. Next printing takes
place from next line. Next printing takes place from the same line. ny other identifier. other
identifier. Reserved words are also known as keywords. Five commonly used Java reserved words
are:
1. public
2. class
3. int
4. double
5. char
9. Java compiler compiles Java source code to Bytecode. Bytecode cannot run on the processor
directly as processor only understands Machine Code. Java Virtual Machine (JVM) takes this
Bytecode as input and converts it into Machine Code line by line. So, JVM acts as an interpreter
for converting Bytecode to Machine Code. In this way, a Java program uses both a Compiler as well
as an Interpreter to get executed on the processor.
8.
Input in Java
IV.2. In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently structure their
code.
java.util, java.lang are a couple of examples of built-in packages.
import keyword is used to import built-in and user-defined packages into our Java program.
4.Errors that occur during the execution of the program primarily due to the state of the program
which can only be resolved at runtime are called Runtime errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all nonzero values of n entered by the user. When the user enters
zero, a run-time error will occur as the program is trying to perform an illegal mathematical
operation of division by 0. When we are compiling the program, we cannot say if division by 0
error will occur or not. It entirely depends on the state of the program at run-time.
6.

Mathematical Library Methods


V.1. 2.
Conditional Statements in Java
V.2. The function System.exit(0) is used when we want to terminate the execution of the program
at any instance. As soon as System.exit(0) function is invoked, it terminates the execution, ignoring
the rest of the statements of the program.
Syntax:
System.exit(0);
For example, if I am writing a program to find the square root of a number and the user enters a
negative number then it is not possible for me to find the square root of a negative number. In such
situations, I can use System.exit(0) to terminate the program. The example program to find the
square root of a number is given below:
import java.util.Scanner;
public class SquareRootExample {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("Cannot find square root of a negative number");
System.exit(0);
}
double sqrt = Math.sqrt(n);
System.out.println("Square root of " + n + " = " + sqrt);
}
}
3. default case is optional in a switch statement. If no case is matched in the switch block for a
given value of control variable, default case is executed implicitly.
Consider the below example:
int number = 4;
switch(number) {
case 0:
System.out.println("Value of number is zero");
break;
case 1: System.out.println("Value of number is one");
break;
case 2: System.out.println("Value of number is two");
break;
default: System.out.println("Value of number is greater than two");
break;
}
Here, value of number is 4 so case 0, case 1 and case 2 are not equal to number. Hence println()
of default case will get executed printing "Value of number is greater than two" to the console.
If we don't include default case in this example then also the program is syntactically correct
but we will not get any output when value of number is anything other than 0, 1 or 2.
4. Use of break statement in a switch case statement is optional. Omitting break statement will
lead to fall through where program execution continues into the next case and onwards till
end of switch statement is reached.
Consider the below example:
int shape = 2;
switch(number) {
case 0:
System.out.println("Circle");
break;
case 1:
System.out.println("Triangle");
case 2:
System.out.println("Square");
case 3:
System.out.println("Rectangle");
case 4:
System.out.println("Pentagon");
break;
default:
System.out.println("Shape not found in the list!");
break;
}
Here, we have omitted break statement in cases 1, 2 and 3. When the program executes with the
value of control variable as 2, 'Square' will be printed on the screen. Since no break statement is
encountered, 'Rectangle' and ' Pentagon' will also be printed. Finally, in case 4 break statement
will act as a case terminator and the control will come out of the switch case.
5. break statement at the end of case is optional. Omitting break leads to program execution
continuing into the next case and onwards till a break statement is encountered or end of
switch is reached. This is termed as Fall Through in switch case statement.
Consider the below example:
int day = 4;
switch(number) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
case 4:
System.out.println("Thursday");
case 5:
System.out.println("Friday");
case 6:
System.out.println("Saturday");
case 7:
System.out.println("Sunday");
default:
System.out.println("Invalid choice");
}
Output:
Thursday
Friday
Saturday
Sunday
Invalid choice
Explanation
Here, we have omitted break statement in all the cases. When the program executes with the
control variable 4, the program prints 'Thursday' and falls through case 5, 6, 7 and default.
Thus, 'Friday', 'Saturday', 'Sunday' and 'Invalid choice' gets printed on the output screen.
8. Iterative Constructs in Java :- V.5.

Library Classes
VII.1. In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to efficiently structure their
codes.
java.util.,java.lang are examples of built-in packages.
2.The asterisk(*) sign indicates that all the classes in the imported package can be used in the
program.
3. Wrapper classes wrap the value of a primitive type in an object. Wrapper classes are present in
java.lang package. The different wrapper classes provided by Java are Boolean, Byte, Integer,
Float, Character, Short, Long and Double.
4.a. b. c.

Arrays
VI.1. 6. 3.

2. 4. 8.
5. 7.

String Handling
VI.1. 2.

3. 4.

VII.1. An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions. Two exception handling blocks are try and catch.
2.a. It returns the index within the string of the first occurrence of the specified character or - 1 if
the character is not present. Its return type is int.
b. It compares two strings lexicographically. It results in the difference of the ASCII codes of the
corresponding characters. Its return type is int.
5. endsWith() tests if the string object ends with the string specified as its argument. startsWith()
tests if the string object starts with the string specified as its argument.
Consider the below example:
public class Example {
public static void main(String args[]) {
String str = "ICSE Computer Applications";
System.out.println("Does " + str + " starts with ICSE? " + str.startsWith("ICSE"));
System.out.println("Does " + str + " ends with tions? " + str.endsWith("tions"));
}
}
Here, both str.startsWith("ICSE") and str.endsWith("tions") returns true as str starts with "ICSE"
and ends with "tions".
VIII.1. It converts a string into upper case characters. If any character is already in uppercase or is
a special character then it will remain same.
Syntax:
String <variable-name>= <string-variable>.toUpperCase();
2. It removes all leading and trailing space from the string.
Syntax:
String <variable-name>= <string-variable>.trim();
3. It converts a string into lowercase characters. If any character is already in lowercase or is a
special character then it will remain same.
Syntax:
String <variable-name>= <string-variable>.toLowerCase();
4. It returns the length of the string i.e. the number of characters present in the string.
Syntax:
int <variable-name>= <string-variable>.length();
5. It replaces a character with another character or a substring with another substring at all its
occurrences in the given string.
Syntax:
String <variable-name>= <string-variable>.replace(, );
6. It compares two strings lexicographically. It results in the difference of the ASCII codes of the
corresponding characters. Its return type is int.
Syntax:
int <variable-name>= <string-variable>.compareTo();
7. It is a method of StringBuffer class and it is used to reverse the sequence of characters.
Syntax:
<StringBuffer-Variable>.reverse();
8. It returns the index of the first occurrence of the specified character within the string or -1 if
the character is not present.
Syntax:
int <variable-name> = <string-variable>.indexOf(<character>);
9. It tests if the string object starts with the string specified as its argument.
Syntax:
boolean <variable-name> = <string-variable>.startsWith(<string>);
10. It ignores the case of the characters and checks if the contents of two strings are same or not.
Syntax:
boolean <variable-name> = <string-variable>.equalsIgnoreCase(<string>);

User Defined Methods


VI.1. A program module used at different instances in a program to perform a specific task is
known as a method or a function.
First line of method definition that contains the access specifier, return type, method name and a
list of parameters is called method prototype.
3. Yes, when a method returns a value, we can assign the entire method call to a variable. The
given example illustrates the same:
public class Example {
public int sum(int a, int b) {
int c = a + b; return c;
}
public static void main(String args[]) {
Example obj = new Example();
int x = 2, y = 3;
int z = obj.sum(x, y);
System.out.println(z);
}
}
7. Pass by reference means that the arguments of the method are a reference to the original objects
and not a copy. So any changes that the called method makes to the objects are visible to the calling
method. Consider the example given below:
class PassByReferenceExample {
public void demoRef(int a[]) {
for (int i = 0; i < 5; i++) {
a[i] = i;
}
}
public static void main(String args[]) {
PassByReferenceExample obj = new PassByReferenceExample();
int arr[] = { 10, 20, 30, 40, 50 };
System.out.println("Before call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
obj.demoRef(arr);
System.out.println("After call to demoRef value of arr");
for (int i = 0; i < 5; i++) {
System.out.print(arr[i] + " ");
}
}
}
The output of this program is:
Before call to demoRef value of arr
10 20 30 40 50
After call to demoRef value of arr
01234
Here demoRef changes the values of array a and these changes are reflected in the array in the
main method as well.
8. For a method to return a value, it should have a return type other than void in its method
prototype and it should return a value of the corresponding type using the return statement in the
method body.
9. 11.

12. Advantages of defining methods in a program are:


1. Methods help to manage the complexity of the program by dividing a bigger complex task into
smaller, easily understood tasks.
2. Methods are useful in hiding the implementation details.
3. Methods help with code reusability.
13. Method overloading is the process of defining functions/methods within a class, that have the
same name but differ in the number and/or the data types of their arguments.
The advantages of method overloading are:
1. Method overloading is one of the ways in which Java implements the object oriented concept of
Polymorphism.
2. With method overloading, programmers don't have to create and remember different names for
functions doing the same thing for different data types.
14.a. Return data type specifies the type of value that the method should return. It is mentioned
before the method name in the method prototype. It can be any valid primitive or composite data
type of Java. If no value is being returned, it should be void.
b. Access specifiers determine the type of access to the method. It can be either public, private or
protected.
c. Parameter list is a comma-separated list of variables of a method along with their respective data
types. The list is enclosed within a pair of parentheses. Parameter list can be empty if the method
doesn't accept any parameters when it is called.
d. A method that calls itself inside its body is called a Recursive method.
e. Method signature comprises of the method name and the data types of the parameters. For
example, consider the below method: int sum(int a, int b) {
int c = a + b;
return c;
}
Its method signature is: sum(int, int)
15. A method returns a value through the return statement. Once a return statement is executed, the
program control moves back to the caller method skipping the remaining statements of the current
function if any. A method can have multiple return statements but only one of them will be
executed. For example, consider the given method:
int sum(int a, int b) {
int c = a + b;
return c;
}
It uses a return statement to return a value of int type back to its caller.
16.

18. A method can have multiple return statements but only one of them will be expected because
once a return statement is executed, the program control moves back to the caller method skipping
the remaining statements of the current method.

Class as the Basis of All Computation


IV.5. When a class is declared with public access specifier it is said that the class is publicly
accessible. A publicly accessible class is visible everywhere both within and outside its package. For
example:
public class Example {
//Class definition
}
8. Variables that are declared inside a class without using the keyword 'static' and outside any
member methods are termed instance variables. Each object of the class gets its own copy of
instance variables. For example, in the below class:
class Cuboid {
private double height;
private double width;
private double depth;
private double volume;
public void input(int h, int w, int d) {
height = h;
width = w;
depth = d;
}
public void computeVolume() {
volume = height * width * depth;
System.out.println("Volume = " + volume);
}
}
height, width, depth and volume are instance variables.
9. private — A data member or member method declared as private is only accessible inside the
class in which it is declared.
public — A data member or member method declared as public is accessible inside as well as
outside of the class in which it is declared.
10. A member method of a class declared with private access specifier is said to have private
visibility. Only other member methods of its class can call this method.
V.1. Class is a blueprint of an object. When a class is defined, it doesn't acquire any space in
memory, it is only the attributes that must be common to all the objects of that class. Moreover,
when an object of a class is created, it includes instance variables described within the class. This
is the reason why an object is called an instance of a class.
2. 5. 7.
4. The classes that contain public static void main(String args[]) method are not considered as user
defined data type. Only the classes that don't contain this method are called user defined data type.
The presence of public static void main(String args[]) method in a class, converts it into a Java
application so it is not considered as a user defined data type.
6. Private members are only accessible inside the class in which they are defined and they cannot
be inherited by derived classes. Protected members are also only accessible inside the class in which
they are defined but they can be inherited by derived classes.

Constructors
V.1. A constructor is a member method having the same name as that of a class and is used to
initialise the instance variables of the objects. It is invoked at the time of creating any object of the
class. For example:
Employee emp = new Employee();
Here, Employee() is invoking a default constructor.
5. Constructors are special member methods of the class. Objects are non-primitive data types so
they are passed by reference and not by value to constructors. If objects were passed by value to a
constructor then to copy the objects from actual arguments to formal arguments, Java would again
invoke the constructor. This would lead to an endless circular loop of constructor calls.
10. The first statement abc p = new abc(); is calling a non-parameterised constructor to create and
initialize an object p of class abc. The second statement abc p = new abc(5,7,9); is calling a
parameterised constructor which accepts three arguments to create and initialize an object p of
class abc.
6. 8.

You might also like