Java Note4
Java Note4
The final keyword in java is used to restrict the user. The java final keyword can be
used in many context. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no
value it is called blank final variable or uninitialized final variable. It can be initialized
in the constructor only. The blank final variable can be static also which will be
initialized in the static block only. We will have detailed learning of these. Let's first
learn the basics of final keyword.
1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400;
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class
1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4.
5. class Honda extends Bike{
6. void run(){System.out.println("running safely with 100kmph");}
7.
8. public static void main(String args[]){
9. Honda honda= new Honda();
10. honda.run();
11. }
12. }
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.
Syntax:
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print(){System.out.println("Hello");}
6.
7. public static void main(String args[]){
8. A6 obj = new A6();
9. obj.print();
10. }
11. }
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. A7 obj = new A7();
13. obj.print();
14. obj.show();
15. }
16. }
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class TestInterface4 implements Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. TestInterface4 obj = new TestInterface4();
13. obj.print();
14. obj.show();
15. }
16. }
1) Abstract class can have abstract and Interface can have only abstract methods. Since
non-abstract methods. Java 8, it can have default and static
methods also.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) An abstract class can extend another An interface can extend another Java interface
Java class and implement multiple Java only.
interfaces.
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
String Array in Java
An Array is an essential and most used data structure in Java. It is one of the most
used data structure by programmers due to its efficient and productive nature; The
Array is a collection of similar data type elements. It uses a contiguous memory
location to store the elements.
o It is an object of the Array.
o It can be declared by the two methods; by specifying the size or without
specifying the size.
o It can be initialized either at the time of declaration or by populating the
values after the declaration.
o The elements can be added to a String Array after declaring it.
o The String Array can be iterated using the for loop.
o The searching and sorting operation can be performed on the String Array.
Declaration:
The Array declaration is of two types, either we can specify the size of the Array or
without specifying the size of the Array. A String Array can be declared as follows:
1. String[] stringArray1 //Declaration of the String Array without specifying the
size
2. String[] stringArray2 = new String[2]; //Declarartion by specifying the size
Another way of declaring the Array is String strArray[], but the above-specified
methods are more efficient and recommended.
Initialization:
The String Array can be initialized easily. Below is the initialization of the String Array:
1. 1. String[] strAr1=new String[] {"Ani", "Sam", "Joe"}; //inline initialization
2. 2. String[] strAr2 = {"Ani", "Sam", " Joe"};
3. 3. String[] strAr3= new String[3]; //Initialization after declaration with specific s
ize
4. strAr3[0]= "Ani";
5. strAr3[1]= "Sam";
6. strAr3[2]= "Joe";
Java String class methods
The java.lang.String class provides many useful methods to perform operations on
sequence of char values.
6 String substring(int beginIndex, int endIndex) It returns substring for given begin
index and end index.
20 int indexOf(int ch, int fromIndex) It returns the specified char value
index starting with given index.