Oops
Oops
1) final variable
2) final method
3) final class
⚫ Java array can be also be used as a static field, a local variable or a method
parameter.
⚫ The size of an array must be specified by an int value and not long or short.
⚫ The direct superclass of an array type is Object.Every array type implements the
interfaces Cloneable and java.io.Serializable.
Types of Array in java
1. One- Dimensional Array
2. Multidimensional Array
One-Dimensional Arrays
An array is a group of like-typed variables that are referred to by a common name. An
array declaration has two components: the type and the name. type declares the element
type of the array. The element type determines the data type of each element that
comprises the array.
Syntax:
type var-name[ ];
Instantiation of an Array in java
array-var = new type [size];
Example:
class Testarray{
public static void main(String args[]){
int a[]=new int[5];
//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)
//length is the property of array
System.out.println(a[i]);
}}
Output:
10
20
70
40
50
Multidimensional Arrays
Multidimensional arrays are arrays of arrays with each element of the array holding the
reference of other array. These are also known as Jagged Arrays. A multidimensional
array is created by appending one set of square brackets ([]) per dimension.
Syntax:
type var-name[ ][ ]=new type[row-size ][col-size ];
Example:
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++) for(j=0; j<5; j++)
{ twoD[i][j] = k; k++;
} for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}}}
Output:
01234
56789
10 11 12 13 14
15 16 17 18 19
16 Explain the concept of inheritance with example
INHERITANCE
Inheritance can be defined as the procedure or mechanism of acquiring all the properties
and behaviors of one class to another, i.e., acquiring the properties and behavior of child
class from the parent class. When one object acquires all the properties and behaviours
of another object, it is known as inheritance. Inheritance represents the IS-A relationship,
also known as parent-child relationship.
⚫ single,
Syntax:
}
SINGLE INHERITANCE
In Single Inheritance one class extends another class (one class only).
Example:
}
}
b.dispA();
b.dispB();
Output :
MULTILEVEL INHERITANCE
In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived
class
c.dispA();
c.dispB();
c.dispC();
Output :
HIERARCHICAL INHERITANCE
Example:
{
//Assigning ClassB object to ClassB reference
b.dispB();
b.dispA();
c.dispC();
c.dispA();
d.dispD();
d.dispA();
Output :
The clone() method saves the extra processing task for creating the exact copy of an
object. If we perform it by using the new keyword, it will take a lot of processing time to
be performed that is why we use object cloning.
⚫ You don't need to write lengthy and repetitive codes. Just use an abstract class with
a 4- or 5-line long clone() method.
⚫ It is the easiest and most efficient way for copying objects, especially if we are
applying it to an already developed or an old project. Just define a parent class,
implement Cloneable in it, provide the definition of the clone() method and the task
will be done.
• To use the Object.clone() method, we have to change a lot of syntaxes to our code, like
implementing a Cloneable interface, defining the clone() method and handling
CloneNotSupportedException, and finally, calling Object.clone() etc.
⚫ We have to implement cloneable interface while it doesn?t have any methods in it.
We just have to use it to tell the JVM that we can perform clone() on our object.
⚫ Object.clone() is protected, so we have to provide our own clone() and indirectly
call Object.clone() from it. Object.clone() doesn?t invoke any constructor so we
don?t have any control over object construction.
EXAMPLE
int rollno;
String name;
Student(int rollno,String
name){ this.rollno=rollno;
this.name=name;
return super.clone();
try{
Student s2=(Student)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
catch(CloneNotSupportedException c){}
Output:
101 amit
101 amit
1. private
2. default
3. protected
4. public
In this example, we have created two classes A and Simple. A class contains private data
member and
private method. We are accessing these private members from outside the class, so there is
compile time
error.
class A{
If you don't use any modifier, it is treated as default bydefault. The default modifier is
accessible only within package.
Example:
In this example, we have created two packages pack and mypack. We are accessing the A class
from outside its package, since A class is not public, so it cannot be accessed from outside the
package.
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
//save by B.java
package mypack;
import pack.*;
class B{
}
}
In the above example, the scope of class A and its method msg() is default so it cannot be
accessed from
The protected access modifier is accessible within package and outside the package but
through inheritance
only
The protected access modifier can be applied on the data member, method and constructor. It
can't be applied on the class.
Example:
In this example, we have created the two packages pack and mypack. The A class of pack
package is public,so can be accessed from outside the package. But msg method of this package
is declared as protected, so it can be accessed from outside the class only through inheritance.
EXAMPLE
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
import pack.*;
class B extends A{
Output:
Hello
The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
Example:
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
import pack.*;
class B{
obj.msg();
Output:
Hello