Java Arrays: Declaration, Access, and Operations
Java Arrays: Declaration, Access, and Operations
Your Name
Department of Computer Science
October 21, 2025
Contents
7 Sorting of Arrays 13
7.1 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.2 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7.3 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1
Your Name / Class Java Notes
9 Array Class 15
9.1 Methods in java Array class . . . . . . . . . . . . . . . . . . . . . . . . . 15
10 2D Arrays in Java 15
10.1 Declaring and Initialsing a 2D Array . . . . . . . . . . . . . . . . . . . . 16
10.2 Accessing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
10.3 Iterating through a 2D Array . . . . . . . . . . . . . . . . . . . . . . . . 16
13 Arrays as Vectors 18
13.1 convert an array to a vector . . . . . . . . . . . . . . . . . . . . . . . . . 18
14 Introduction to Inheritance 20
14.1 Benefits of using inheritance in Java . . . . . . . . . . . . . . . . . . . . . 20
14.2 Key Terminologies Used in Java Inheritance . . . . . . . . . . . . . . . . 20
15 Process of Inheritance 21
15.1 Types of Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
15.1.1 Is-A Relationship in Java . . . . . . . . . . . . . . . . . . . . . . . 21
15.1.2 Has-A-Relation in Java . . . . . . . . . . . . . . . . . . . . . . . . 21
15.2 Has-A-Relation in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
15.2.1 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
15.2.2 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
15.3 How to Decide which Type of Relation We Need . . . . . . . . . . . . . . 22
16 Super 22
16.1 Code to demonstrate the order of constructor calls with a parameterized
constructor in the sub class. . . . . . . . . . . . . . . . . . . . . . . . . . 23
16.2 Code to demonstrate the use of Super method when both super class and
sub class has parameterized constructors. . . . . . . . . . . . . . . . . . . 24
16.3 Code for demonstrating order of constructor calls and the use of Super
method in Multi level inheritance. . . . . . . . . . . . . . . . . . . . . . . 25
Page 2
Your Name / Class Java Notes
21 Method overriding 28
23 Abstract class 30
24 Interfaces 32
24.1 Multiple Interfaces in Java . . . . . . . . . . . . . . . . . . . . . . . . . . 32
24.2 Nested Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
24.3 Inheritance of Interfaces in Java . . . . . . . . . . . . . . . . . . . . . . . 35
24.4 Default Methods in Interface . . . . . . . . . . . . . . . . . . . . . . . . . 36
24.5 Static Methods in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 37
24.6 Difference between method overloading and overriding . . . . . . . . . . . 38
24.7 Difference between class and interface . . . . . . . . . . . . . . . . . . . . 38
24.8 Difference between Abstract class and interface . . . . . . . . . . . . . . 38
Page 3
Your Name / Class Java Notes
Examples:
Page 4
Your Name / Class Java Notes
1 // Declaration
2 int [] numbers ;
3
• **Reference:** The reference variable of the array holds the address of the array
object in memory.
• **Array Object:** Contains metadata (like length) and the actual array data.
• **Array Data:** The data elements are held in **contiguous memory locations**.
For instance, an int[] stores each integer in 4 bytes consecutively.
• Since arrays are reference types (created using new), they are stored in the **heap
area**.
Example
Page 5
Your Name / Class Java Notes
Formula:
The memory address of an element at index i can be calculated as:
For example, if the base address of the array is 0x1000 and each integer occupies 4
bytes, the address of the element at index 2 would be:
3.1 Syntax
arrayRefVar [ i n d e x ] ;
The index is an integer literal or an expression that evaluates to an integer. The index
of the last element is always one less than the number of elements ([Link] - 1).
p u b l i c c l a s s ArrayElements {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
i n t [ ] numArray = new i n t [ 5 ] ;
int i ;
Page 6
Your Name / Class Java Notes
f o r ( i = 0 ; i < 5 ; i ++) {
numArray [ i ] = i n p u t . n e x t I n t ( ) ; // Read number
}
Page 7
Your Name / Class Java Notes
• Insertion in an Array
• Deletion in an Array
• Traversing of an Array
• Merging of an Array
32 }
33 }
Page 8
Your Name / Class Java Notes
3 int main () {
4 int arr1 [10] , arr2 [10] , merged [20];
5 int n1 , n2 , i , j , k ;
6
Page 9
Your Name / Class Java Notes
31 return 0;
32 }
• **Scenario-3: Use the clone() method** of the array. This creates a new array of
the same size with a shallow copy of elements.
8 int a [] = { 1 , 8 , 3 };
9
13 // Copying elements of a [] to b []
14 for ( int i = 0; i < a . length ; i ++)
15 b [ i ] = a [ i ];
16
Page 10
Your Name / Class Java Notes
Syntax of [Link]():
v o i d a r r a y c o p y ( Object s r c , i n t srcPos ,
Object de st , i n t destPos , i n t l e n g t h )
Page 11
Your Name / Class Java Notes
7 int a [] = { 1 , 8 , 3 };
8
12 // Copying elements of a [] to b []
13 System . arraycopy (a , 0 , b , 0 , 3) ;
14
Page 12
Your Name / Class Java Notes
7 Sorting of Arrays
7.1 Bubble Sort
Bubble Sort is a comparison-based sorting algorithm where each element is compared
with the next and swapped if they are not in the correct order.
• **Time Complexity**: O(N 2 )
• **Space Complexity**: O(1)
Page 13
Your Name / Class Java Notes
2. Compare the key (element at current index) with the preceding element.
3. If the key is smaller, compare it to other preceding elements. Shift greater elements
one position to their right to make space.
Algorithm
Algorithm (Recursive)
Page 14
Your Name / Class Java Notes
9 Array Class
• The Arrays class in [Link] package is a part of the Java Collection Framework.
• This class provides static methods to dynamically create and access Java arrays.
• It consists of only static methods and the methods of Object class.
• This class contains various methods for manipulating arrays (such as sorting and
searching).
• The methods of this class can be used by the class name itself.
10 2D Arrays in Java
A 2D array in Java is an array of arrays. Think of it like a table with rows and columns,
where each cell holds a value. It’s like a grid or matrix that can store data in a more
organized way.
Page 15
Your Name / Class Java Notes
2 package j av a p ro g r am m i ng d e mo ;
3 import java . util . Scanner ;
4 public class javalabclass {
5 public static void main ( String args [])
6 {
7 int jaggedArr [][] = new int [4][];
8
Page 16
Your Name / Class Java Notes
14 // filling values
15 for ( int i =0; i < jaggedArr . length ; i ++)
16 {
17 for ( int j =0; j < jaggedArr [ i ]. length ; j ++)
18 {
19 jaggedArr [ i ][ j ] = i +1;
20 }
21 }
22
23 // printing
24 for ( int m []: jaggedArr )
25 {
26 for ( int k : m )
27 {
28 System . out . print ( k + " " ) ;
29 }
30 System . out . println () ;
31 }
32 }
33 }
34
35 Output
36 1 1 1 1 1
37 2 2
38 3 3 3 3
39 4 4
An array list of data elements makes a 1-D (one-dimensional) array. An array of 1-D
arrays makes a 2-D (two-dimensional) array. Similarly, an array of 2-D arrays makes a
3-D ( three-dimensional) array.
1 class HelloWorld {
2 public static void main ( String args [] ) {
3
Page 17
Your Name / Class Java Notes
23 }
24 }
25 }
26 }
27 }
13 Arrays as Vectors
• Vector class implements a growable array of objects. It is compatible with Java
collections.
• It contains components that can be accessed using an integer index like an array.
• Using loop
Page 18
Your Name / Class Java Notes
11 Vector < String > v = new Vector < String >( Arrays . asList ( arr ) ) ;
12
7 String [] arr = { " I " , " love " ," java " };
8
Page 19
Your Name / Class Java Notes
Listing 7: forloop
14 Introduction to Inheritance
• Inheritance in Java is a mechanism in object-oriented programming (OOP) that
allows you to create new classes based on existing classes. The existing class is
known as the **parent class** or **superclass**, and the new class is known as the
**child class** or **subclass**.
• Inheritance allows you to reuse the code and behavior of the parent class in the
child class. This can save you time and effort when writing your code, and it can
also make your code more readable and maintainable.
• Code reuse: Inheritance allows you to reuse the code and behavior of the parent
class in the child class. This can save you time and effort when writing your code.
• Readability and maintainability: Inheritance can make your code more readable
and maintainable by making it clear what code is inherited from the parent class
and what code is new in the child class.
• Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
• Sub Class/Child Class: The class that inherits the other class is known as a sub-
class(or a derived class, extended class or child class). The subclass can add its own
fields and methods in addition to the superclass fields and methods.
Page 20
Your Name / Class Java Notes
15 Process of Inheritance
15.1 Types of Relationships
15.1.1 Is-A Relationship in Java
• The is-a relationship, also known as the inheritance relationship, represents a type of
relationship between two classes where one class is a specialized version of another.
1 class SuperClass {
2 void methodSuper () {
3 System . out . println ( " I am a super class method " ) ;
4 }
5 }
6
Page 21
Your Name / Class Java Notes
15.2.1 Composition
• Composition is a strong association between two classes
15.2.2 Aggregation
• Aggregation is a weaker form of association
• So, if your problem with a phrase containing ”.... is a . . . .” words, you should you
Is-a a relationship or else use Has-a relationship.
16 Super
• The ‘super‘ keyword in Java is used to refer to the superclass (parent) object. It is
used to call superclass methods, and to access the superclass constructor.
• The most common use of the super keyword is to eliminate the confusion between
superclasses and subclasses that have methods with the same name. For example,
if the ‘Animal‘ class has an ‘eat()‘ method, and the ‘Dog‘ class inherits from the
Page 22
Your Name / Class Java Notes
‘Animal‘ class, the ‘Dog‘ class will also have an ‘eat()‘ method. However, the ‘Dog‘
class’s ‘eat()‘ method may implement different behavior than the ‘Animal‘ class’s
‘eat()‘ method.
• To call the Animal class’s eat() method from the Dog class, you can use the ‘super‘
keyword. For example, the following code shows how to call the ‘Animal‘ class’s
‘eat()‘ method from the ‘Dog‘ class:
14 class person
15 {
16 protected String name ;
17 protected String address ;
18
19 public person () {
20 System . out . println ( " super class constructor called " ) ;
21 this . name = " xyz " ;
22 this . address = " pvpsit " ;
23 }
24
Page 23
Your Name / Class Java Notes
44 }
Listing 10: parameterised constructor
2 package javalabdemo ;
3 public class javalabclass
4 {
5 public static void main ( String args [])
6 {
7 faculty xyz = new faculty ( " xyz " ," pvpsit " ," 123 " ) ;
8 xyz . display_details () ;
9 xyz . display_faculty () ;
10 }
11 }
12
13 class person
14 {
15 protected String name ;
16 protected String address ;
17
Page 24
Your Name / Class Java Notes
44 }
Listing 11: super
13 class person
14 {
15 protected String name ;
16 protected String address ;
17
Page 25
Your Name / Class Java Notes
44 }
45
Page 26
Your Name / Class Java Notes
• Private members are not accessible directly. (Still exist in memory, but cannot be
accessed.)
Page 27
Your Name / Class Java Notes
• If a class does not extend any other class then it is a direct child class of the Java
Object class and if it extends another class then it is indirectly derived.
• There are 11 common properties that every Java class must implement. But it
would be difficult for developers to repeat the same boilerplate code in each class.
The Object class implements all these 11 properties through 11 methods, making
Java programming a little simpler,
• Constructors are not inherited (they are not members of the class).
• But the parent constructor is always called (directly or indirectly) when creating
an object of the child class.
1 class Animal {
2 Animal () {
3 System . out . println ( " Animal constructor called " ) ;
4 }
5 }
6 class Dog extends Animal {
7 Dog () {
8 super () ;
9 // Calls Animal () constructor \\
10 ( implicitly added if not written )
11 System . out . println ( " Dog constructor called " ) ;
12 }
13 }
14 public class Main {
15 public static void main ( String [] args ) {
16 Dog d = new Dog () ;
17 }}
Listing 15: call to parent constructor
21 Method overriding
If the same method is defined in both the superclass and the subclass, then the method
of the subclass class overrides the method of the superclass. This is known as method
Page 28
Your Name / Class Java Notes
overriding.
Method Overriding
• The method in the subclass must have the same name, return type, and parameters
as the method in the superclass.
• You create a reference of the superclass but store a subclass object in it.
• When you call the method, Java looks at the actual object type (not the reference
type) and calls the correct version of the method
1 . class A
2 {
3 void m1 ()
4 {
5 System . out . println ( " Inside A ’s m1 method " ) ;
6 }
7 }
8
9 class B extends A
10 {
11 // overriding m1 ()
12 void m1 ()
13 {
14 System . out . println ( " Inside B ’s m1 method " ) ;
15 }
16 }
17
18 class C extends A
19 {
20 // overriding m1 ()
21 void m1 ()
22 {
Page 29
Your Name / Class Java Notes
27 // Driver class
28 class Dispatch
29 {
30 public static void main ( String args [])
31 {
32 // object of type A
33 A a = new A () ;
34
35 // object of type B
36 B b = new B () ;
37
38 // object of type C
39 C c = new C () ;
40
47 // calling A ’s version of m1 ()
48 ref . m1 () ;
49
53 // calling B ’s version of m1 ()
54 ref . m1 () ;
55
59 // calling C ’s version of m1 ()
60 ref . m1 () ;
61 }
62 }
Listing 16: Dynamic Method Dispatch
23 Abstract class
Abstract classes in Java are classes that cannot be instantiated directly. They must be
extended by other classes, which are known as subclasses. Abstract classes can contain
both abstract and non-abstract methods. Abstract methods are methods that do not
have an implementation in the abstract class. Subclasses must provide their own imple-
mentations for abstract methods.
Page 30
Your Name / Class Java Notes
Abstract classes are used to provide a common functionality for a group of related
classes. For example, you could create an abstract class called ‘Animal‘ that contains ab-
stract methods such as ‘eat()‘ and ‘sleep()‘. Then, you could create subclasses of ‘Animal‘
such as ‘Dog‘, ‘Cat‘, and ‘Bird‘. Each subclass would provide its own implementation for
the abstract methods in the ‘Animal‘ class.
Use abstract classes to provide a common functionality for a group of related classes.
Be careful not to overuse abstract classes, as this can make your code difficult to read
and maintain.
Document the reasons for using abstract classes in your code.
1 package j av a p ro g r am m i ng d e mo ;
2 public class javalabclass {
3
Page 31
Your Name / Class Java Notes
40 }
41 // implementing the abstract method
42 // its mandatory to provide the implementation of the
43 // abstract methods inherited .
44 public void calculate_area () {
45 this . area = side * side ;
46 }
47 }
Listing 17: Abstract class
24 Interfaces
• An interface in Java is a reference type that defines a set of methods without
providing any implementations.
• Abstraction allows you to focus on the behavior of a class without worrying about
its implementation.
• Polymorphism allows you to treat different objects as if they were of the same type,
as long as they implement the same interface.
• Interface contain only abstract methods. The methods are abstract and public by
default.
• Interface can contain only static and final variables that are public.
• This is Java’s way of achieving multiple inheritance (since Java does not allow a
class to inherit from more than one class).
6 interface Swimmable {
7 void swim () ;
Page 32
Your Name / Class Java Notes
Figure 2: Interface
8 }
9
Page 33
Your Name / Class Java Notes
3 // Nested interface
4 public interface NestedInterface {
5 public void nestedMethod () ;
6 }
7
Page 34
Your Name / Class Java Notes
Key points:
• Any class that implements the child interface must provide implementations for all
methods of both parent and child interfaces.
• Unlike classes, interfaces can extend multiple interfaces (so one interface can inherit
from many)
1 interface InterfaceA {
2
3 void name () ;
4 }
5
8 void institute () ;
9 }
10
Page 35
Your Name / Class Java Notes
21 }
22
Key Points
• A class can override the default method if it wants to provide its own implementa-
tion.
• Used mainly to add new functionality to interfaces without breaking existing code.
1 interface TestInterface
2 {
3 // abstract method
4 public void square ( int a ) ;
5
6 // default method
7 default void show ()
8 {
9 System . out . println ( " Default Method Executed " ) ;
10 }
11 }
12
Page 36
Your Name / Class Java Notes
• The scope of the static method is limited to the interface in which it is defined.
1 interface NewInterface {
2
3 // static method
4 static void hello ()
5 {
6 System . out . println ( " Hello , New Static Method Here " ) ;
7 }
8
27 @Override
28 public void overrideMethod ( String str )
29 {
30 System . out . println ( str ) ;
Page 37
Your Name / Class Java Notes
31 }
32 }
Listing 23: static method in an interface
Page 38
Your Name / Class Java Notes
Page 39