Java Learning
Java Learning
Java Basics :
Each Java Program has to start with “Class”. Without “Class” there will be no programme to
execute. Class is the file where we write a complete java program.
If we write the code outside of the Main method, java can’t execute the code.
int a=10;
int b=20;
int sum=a+b;
System.out.println("The addition of a and b is" +sum);
Note : Here “int” is the return type and a,b, sum are variables.
Objects In Java :
If we are automating a web application. In that we are having 5 pages and in all the 5 pages
there will be a header section where we need to validate all the links are working or not
In this case, if we write 5 class files for 5 pages and in all the 5 class files, we need to validate
the header section repeatedly in all those class files, which is redundant. To avoid this, write a
method in one class. Write an object class file and call the method.
}
How to create a object for the class -> classname object = new classname();
Here “new” means we are creating memory allocation to that particular object in the present
class file.
int a=10;
int b=20;
int sum=a+b;
System.out.println("The addition of a and b is" +sum);
Strings in Java :
For example, we need to validate a text which is “Payment $100 Paid”. Now the amount will be
varied, we are going to validate through $.
STRING is a class, where there will be a lot of methods available in it. STRING class will be
preloaded by JAVA and it will be in the packages.
Code :
Output :
$
17
8
PAYMENT $100 PAID
$100 Paid
Palindrome Programme :
System.out.println(b);
if(b.equalsIgnoreCase(a))
{
System.out.println("String got reversed and string is palindrome");
}
else
{
System.out.println("String got reversed and string is not palindrome");
}
Inheritance :
In Inheritance, we can call one class into another class by using “extends” keyword. And also
we can utilize all the methods from that class.
Example :
{
FatherInherit fi = new FatherInherit();
fi.Addition();
}
—------------
public class SonInherit {
Multiple Inheritance :
Interface :
An interface is an abstract "class" that is used to group related methods with
"empty" bodies: To access the interface methods, the interface must be
"implemented" (kinda like inherited) by another class with the implements keyword
(instead of extends ).
In Simple words, Interface will have a method, but without body. A class to take initiate to take
an agreement to implement those interface will implement the method which are present in that
particular interface.
Example : Taking Banking client with paycreditcard(), transfer balance() & checkingbalance()
methods
package Test;
When user implements the interface, we need to add their methods in the present class, if not
we will get this error as “Add unimplemented methods”. Click on “Add unimplemented methods”
Code :
package Test;
package Test;
{
Developingbanking db = new Developingbanking();
db.checkingbalance();
db.paycreditcard();
db.transferbalance();
db.devlopmethod();
BankingClient bc = new Developingbanking ();
bc.
@Override
public void paycreditcard()
{
System.out.println("paycreditcard");
}
@Override
public void transferbalance()
{
System.out.println("transferbalance");
}
@Override
public void checkingbalance()
{
System.out.println("checkingbalance");
}
Runtime Polymorphism :
In Java, polymorphism refers to the ability of a class to provide different
implementations of a method, depending on the type of object that is passed to
the method. To put it simply, polymorphism in Java allows us to perform the same
action in many different ways.
From the above code we have used “BankingClient bc = new Developingbanking ();”
In which “BankingClient” is an interface and we are creating object with “bc” and calling all the
method which are present in “BankingClient” interface
In this we are able to see the methods which are related to only “BankingClient” not the
methods from “Developingbanking”
Arrays :
A container which stores multiple values of the same data type is called an Array.
{
System.out.println(a[i]);
}
}
Output :
1
2
3
4
5
{
System.out.println(c[i]);
}
In an index, we come across a matrix kind of format. It will be having ‘n’ rows & ‘n’ columns.
Each argument will be taken as X axis and Y axis. X axis is a row and Y axis is a column.
Passing an object in to both X axis and Y axis is nothing but Multi dimensional array. Multi in the
sense we are viewing array from the multi corner perspective like X axis and Y axis.