Unit-2 Java Array,Inheritance,Interface
Unit-2 Java Array,Inheritance,Interface
Oriented
Concepts
With Java
Java Array
• One dimensional, Two dimensional
• Using For...each with array Java Interface
• Passing arrays to methods and • Variables in Interface
returning arrays from method • Extending Interfaces
Java Inheritance
• Deriving classes using extends keyword
• Overriding Method
• super keyword, final keyword
• Abstract class
• User can declare array first after allocate memory to it that is totally valid. After that size allocation means memory
allocation done using new keyword.
• Syntax :-
datatype[] arrayName;
Or
class Main class Main
datatype arrayName[];
{ {
public static void main(String[] args) public static void main(String[] args)
• Example :-
{ {
int[] arr; int arr[];
arr=new int[5]; arr=new int[5];
} }
} }
Prof. Parthavi Varan
Method-2:- declaration + memory allocation
• In this method array declaration and memory allocation done by writing single line.
• Syntax :-
• Example :-
class
Class Main
Main class Main
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int[] arr=new int[5]; int arr[]=new int[5];
} }
} }
• When initialization done with declaration then size is automatically decide by javac [Link] no need to define
size.
• Syntax:
datatype arrayName[]={value1,value2,value3};
Or
Datatype arrayName[]=new datatype[] { value1,value2,value3};
• Example :
class Main
{
public static void main(String[] args)
{
String sname[]=new String[] {“mahi”,”siya”,”jemika”};
}
}
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
}
} Prof. Parthavi Varan
2) Access one Dimensional Array element :-
To find out how many elements an array has, use the length property:
Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
[Link]([Link]);
}
}
5) Access All elements of Array
Using For Loop
Access all the elements of array,loop is required so use for loop to access all element of array.
Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
for(int i=0;i<[Link];i++)
{
[Link](sname[i]);
}
} Prof. Parthavi Varan
}
Using For Each Loop
Access all the elements of array,loop is required so use for each loop to access all element of array. for each loop is easy to
write compare to for [Link] for each loop one variable is declared which take reference of all value of array variable.
Syntax :-
for (type variable : arrayName)
{
//code
}
Example :-
class Main
{
public static void main(String[] args)
{
String sname[]={“mahi”,”siya”,”jemika”};
for(String s : sname)
{
[Link](s);
}
}
}
• Syntax :-
datatype[][] arrayName;
Or
datatype arrayName[][];
• Example :-
class Main class Main
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
int[][] arr; int arr[][];
arr=new int[5][2]; arr=new int[5][2];
} }
} }
Prof. Parthavi Varan
Method-2:- declaration + memory allocation
• In this method array declaration and memory allocation done by writing single line.
• Syntax :-
• Example :-
• When initialization done with declaration then size is automatically decide by javac [Link] no need to define
size.
• Syntax:
datatype arrayName[][]={{value1,value2},{value3,value4}};
Or
Datatype arrayName[][]=new datatype[][] { {value1,value2},{value3,value4}};
• Example :
class Main
{
public static void main(String[] args)
{
int arr[][]=new int[][] { {2,3,4},{5,6,7,8} };
}
}
class Main
{
public static void main(String[] args)
{
Int arr[][]={ {2,3,4} , {5,6,7,8} };
}
} Prof. Parthavi Varan
2) Access Two Dimensional Array element :-
You can access an array element by referring to the row number and column number.
Example :-
class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
[Link](arr[1][1]);
}
}
3) Change Two Dimensional Array element :-
To change the value of a specific element, refer to the row number and column number.
Example :- class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
arr[1][1]=120;
[Link](arr[1][1]);
}
} Prof. Parthavi Varan
4) Array Length
To find out how many elements an array has, use the length [Link] gives number of [Link] find number of
column write arr[row].length it gives number of column in particular row suppose arr[0].length gives 3 because in 1 row 3
column are there.
Example :-
class Main
{
public static void main(String[] args)
{
int arr[][]={ {2,3,4},{5,6,7,8} };
[Link]([Link] + arr[0].length);
}
}
}
}
}
}
Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row
}
}
Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row
}
}
Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row
}
}
Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row
}
}
Outer loop:
for(int[] row : arr) – accesses one complete row at a time
row becomes:
{1,2,3}
{4,5,6}
{7,8,9}
for(int x : row) - accesses each element inside current row
class Main
{
static void display(int[] arr)
{
for(int x : arr)
{
[Link](x);
}
}
class Main
{
static void display(int[][] arr)
{
for(int i=0;i<[Link];i++)
{
for(int j=0;j<arr[i].length;j++)
{
[Link](arr[i][j]);
}
}
}
class Main
{
static int[] getArray()
{
int[] arr = {10,20,30};
return arr;
}
class Main
{
public static void main(String args[])
{
for(int i=0;i<[Link];i++)
{
[Link](args[i]);
}
}
}
Output :-
Output :-
It helps in:
• Code reusability
• Reducing duplicate code
• Creating parent-child relationships between classes
1) Single Inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
1. Single Inheritance
One child class inherits one parent class.
class Animal
{
void sound()
{
[Link]("Animal makes sound");
}
}
In multilevel inheritance:one class inherits another class,then another class inherits that inherited class.
class Animal
{
void eat()
{
[Link]("Animal eats");
}
}
class Dog extends Animal
{
void bark()
{
[Link]("Dog barks");
}
}
class Puppy extends Dog
{
void weep()
{
[Link]("Puppy weeps");
}
}
public class Main
{
public static void main(String[] args)
{
Puppy p = new Puppy(); Prof. Parthavi Varan
[Link]();
3. Hierarchical Inheritance
❌ Not allowed:
class A { }
class B { }
// ERROR
class C extends A, B { }
[Link]();
calls Pig’s method if overridden.
void start()
{
[Link]("Car starts with key");
}
}
Vehicle v;
v = new Car();
[Link]();
v = new Bike();
[Link]();
}
} Prof. Parthavi Varan
Super Keyword
Black
White Prof. Parthavi Varan
2) Access Parent Method
class Animal
Here Animal class has one method sound and Dog
{
void sound() class has also same method name sound.
{
[Link]("Animal sound"); If user want to access Animal class sound method
} then [Link] is used to access parent class
} method inside child class.
class Dog extends Animal
{
void sound()
{
[Link](); Animal sound
[Link]("Dog barks"); Dog barks
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
[Link]();
}
}
Prof. Parthavi Varan
class Animal
{ 3) Access Parent Constructor
Animal()
{
[Link]("Animal Constructor"); Parent constructor only called inside
} child constructor means inside any
} method of child class you can’t call
class Dog extends Animal parent constructor.
{
Dog()
{
super();
Animal Constructor
[Link]("Dog Constructor");
} Dog Constructor
void display()
{
super();
}
}
public class Main
{
public static void main(String[] args)
{
Dog d = new Dog();
}
} Prof. Parthavi Varan
Final Keyword
It is used to restrict:
Variables
Methods
Classes
1. Final Variable
Value cannot change once assigned.
class Demo
{
final int speed = 90;
void show()
{
[Link]("Speed : " + speed);
// speed = 100; // ERROR
}
public static void main(String[] args)
{
Demo d = new Demo();
[Link]();
}
} Prof. Parthavi Varan
2. Final Method
class Bank
{
final void interestRate()
{
[Link]("Interest Rate is Fixed");
}
}
class SBI extends Bank
{
// void interestRate() {} // ERROR
}
public class Main
{
public static void main(String[] args)
{
SBI s = new SBI();
[Link]();
}
}
Prof. Parthavi Varan
3. Final Class
The abstract keyword is a non-access modifier, used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another
class).
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass
(inherited from).
Interface contains:
abstract method
constants (public static final variables)
Normal method (must be with default keyword)
Static method
interface Animal
{
int x=10;
void sound();
}
Java automatically treats method as:
interface Animal
{
public static final int x=10;
public abstract void sound();
} Prof. Parthavi Varan
Variable in Interface
interface Demo
{
int x = 10;
}
public class Main
{
public static void main(String[] args)
{
[Link](Demo.x);
// Demo.x = 20; // ERROR
}
}
class A
{
}
class B
{
}
Example Difference
Interface Abstract Class