Java Concepts for tester - All About Testing Java Concepts for tester

Sunday 18 September 2016

Java Concepts for tester

Java Concepts for tester
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
There are two ways to achieve abstraction in java
  1. Abstract class (0 to 100%)
  2. Interface (100%)
An interface in java is a blueprint of a class. It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction.
         interface printable{
           void print();
}
       class A6 implements printable{
       public void print(){System.out.println(“Hello”);}
       public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
differences between abstract class and interface
Abstract class

Interface
1) Abstract class can have abstract and non-abstract methods.

Interface can have only abstract methods.
2) Abstract class doesn’t support multiple inheritance.

Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables.

Interface has only static and final variables.
4) Abstract class can have static methods, main method and constructor.

Interface can’t have static methods, main method or constructor.
5) Abstract class can provide the implementation of interface.Interface can’t provide the implementation of abstract class.
6) The abstract keyword is used to declare abstract class.The interface keyword is used to declare interface.
7) Example:
public abstract class Shape{
public abstract void draw();
}


Example:
public interface Drawable{
void draw();
}
Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines.
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
  class Subclass-name extends Superclass-name
{                //methods and fields
}
Polymorphism in java is a concept by which we can perform a single action by different ways.
In java, we use method overloading and method overriding to achieve polymorphism.

Difference between method overloading and method overriding in java


Method Overloading


Method Overriding
Method overloading is used to increase the readability of the program.

Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
Method overloading is performed within class.

Method overriding occurs in two classes that have IS-A (inheritance) relationship.
In case of method overloading, parameter must be different.

In case of method overriding, parameter must be same.
Method overloading is the example of compile time polymorphism.

Method overriding is the example of run time polymorphism.
In java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.


Return type must be same or covariant in method overriding.
Serialization in java is a mechanism of writing the state of an object into a byte stream. The reverse operation of serialization is called deserialization.
Factorial of given number
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
//Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
System.out.println(“Enter the number:”);
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println(“Factorial of entered number is: “+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion: Function calling itself!!
output = fact(n-1)* n;
return output;
}
}
Prime number or not
import java.util.Scanner;
public class primeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,flag=0;
Scanner scan= new Scanner(System.in);
System.out.println(“Enter a number for check:”);
//capture the input in an integer
int n=scan.nextInt();
for(i=2;i<=n/2;i++){
if(n%i==0){
System.out.println(“Number is not prime”);
flag=1;
break;
}
}
if(flag==0)
System.out.println(“Number is prime”);
}
}
Fibonacci Series
public class Fibonacci {
public static void main(String[] args) {
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+” “+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
System.out.print(” “+n3);
n1=n2;
n2=n3;
}
}
}
Armstrong number is a number that is equal to the sum of cubes of its digits
153 = (1*1*1)+(5*5*5)+(3*3*3)  = 153
public class amstrong {
public static void main(String[] args) {
int c=0,a,temp;
int n=153;//It is the number to check armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println(“armstrong number”);
else
System.out.println(“Not armstrong number”);
}
}
Palindrome or not
public class PalindromeExample {
/**
@param args
*/
public static void main(String[] args) {
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10;  //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println(“palindrome number “);
else
System.out.println(“not palindrome”);
}
}

0 comments:

Post a Comment