Saturday, 28 October 2017

interface in Java


interface keyword


An interface is a keyword in java, it is used to declare interface in java. Like a class, interface can have methods and variables, but the methods declared in interface must be abstract. It is used to achieve abstraction and multiple inheritance in java. Methods in an interface are implicitly public.

Writing interface similar to writing a class. But class describes the attributes and behaviours of an object. And an interface contains behaviours that a class implements.  

Syntax to declare interface:


interface <interface-name>{

//abstract methods

}

Example:

interface Add{

public int add( );

}

How to implement interface


A class uses implement keyword to implement an interface. If class does not define all the methods of interface then the class must be declared as abstract.

Now we will explain how to implement an interface via this following example.

Example

File: Sum.java

interface Add{

public int add( int a, int b);

}

class Complete implements Add{

public int add(int a, int b){

return a+b;

}

}

public class Sum{

public static void main(String [ ]q){

Complete obj=new Complete();

int c=obj.add(5,12);

System.out.println("Sum of  two numbers is "+c);

}

}

Output:

Sum of two numbers is 17

Code explanation:

In this program First we created one interface named as Add and then after we implement this interface by Complete class and this Complete class define the method that is declared inside interface. Now we create third class named as Sum and this class contains main method. Inside this main method we create object of Complete class and via this object we access the method and get the value of addition of two numbers. After getting the addition of two numbers we print that value using System.out.println( ).

Note:

Since java 8, interface can have default and static methods.

Friday, 27 October 2017

abstract keyword in java


What is abstract keyword


abstract keyword is used to implement the abstraction in java. A method which does not have method definition must be declared as abstract. You can't instantiate abstract classes. Abstract methods must be implemented in the sub classes. We can't use abstract keyword with variables and constructors.

What is abstract class


An abstract class is a class that is declared as abstract. It may or may not be include abstract method. If a class has at least one abstract method, then the class must be declared as abstract class. abstract classes cannot be instantiated , but they can be extended. If we extended an abstract class, we have to provide implementation to all the abstract methods in it.

What is abstract method


An abstract method is a method that is declared without an implementation.

Example 1:

public abstract int add(int a, int b);

An abstract method must always be declared in an abstract class or in other word we can say that if a class has an abstract method, it should be declared as abstract.



Now we will take one example to explain abstract class and abstract method

Example 2:

Filename: Sum.java

abstract class Add{

abstract public int add(int a, int b);

}

class Complete extends Add{

public int add(int a, int b){

return a+b;

}

}

public class Sum{

public static void main(String [ ]p){

Complete obj=new Complete( );

int c=obj.add(5,12);

System.out.println("addtion of two numbers is"+c);

}

}



Output of the program:

addition of two numbers is 17



Code Explanation:

In above program first we created an abstract class named as Add and it contains one abstract method add and then we created a class named as Complete that inherits the Add class. Complete class provide implementation to the abstract method of Add class. Now we create third class named as Sum. Inside this main class we create object of Complete class and by using the object we call the add method and it returns sum of the given values and we print the value.

Thursday, 26 October 2017

Keywords in Java


The java programming language has total of 50 reserved keywords which have special meaning for the compiler and can not be used for used as identifiers. 

Here list of keywords in the Java programming language



1. abstract           2.assert                           3.boolean          4.break                5.byte   

6.case                 7.catch                            8.char                9.class                 10.const 

11.continue       12.default                        13.do                 14.double            15.else 

16.enum            17.extends                       18.final              19.finally            20.float   

21.for                22.goto                            23.if                   24.implements    25.import    

26.instanceof    27.int                               28.interface       29.long                30.native 

31.new              32.package                      33.private          34.protected        35.public

36.return           37.short                           38.static             39.strictfp           40.super   

41.switch          42.synchronized             43.this                44.throw             45.throws

46.transient       47.try                             48.void               49.volatile           50.while



The keywords const and goto are reserved, even though they are not currently used.

true, false and null might seem like keywords, but they are actually literals, you can not use them as identifiers in programs.

Note: All the keywords should be used in lower case.

Thursday, 10 March 2016

How to create crontab ( cronjobs) entries

Cron is a job scheduling subsystem for linux. . If you wish to schedule  any job, script or command for any particular time , then crontab is very useful.
Each user on machine can have their own entries while you should keep in mind which command or job you are running should be accessible to that user. Otherwise permission denied error will  be shown.
Some basic uses of cron.:-
crontab -l : It will print out the all cronjobs you have scheduled for that user.
crontab -e: This command is used to install / create new cronjobs for that user.
crontab -u username -l : You should be root user to run this command. It will list all cron of that user you asked.
crontab -r: This command is used to remove all cron entries.
crontab -u username -r : Will remove all entries of that user.
Note: Keep in mind ,  crontab -r will remove all entries. So please run carefully.
crontab -l > filename.text : It will have all cron entries in text format  on given location. You can also have other extensions too if required.
crontab -l < filename.text: It will restore all entries of cron jobs .
Syntax for crons:-
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
uses of operators:-
Below are the operators which are very short cuts. There are three operators:
The asterisk (*) :  It specifies all values for that in which field ( min, hour, day, etc) it will be defined.
The comma (,) : This operator specifies a list of values, for example: “1,5,10,15,20, 25”.
The dash (-) : This operator specifies a range of values, for example: “5-15” days , which is equivalent to typing “5,6,7,8,9,….,13,14,15” using the comma operator.
The separator (/) : This operator specifies a step value, for example: “0-23/” can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

Examples & uses of all operators :-
to run a file in each min.

* * * * * /root/test.sh    ( the simplest entry)
To run any particular time limit of each hour
20 * * * * * /root/test.sh     ( will run at each hour :20 )
You can use comma for different minutes as below
20,30,40,50 * * * * *  /root/test.sh 
To run on each hour 
00 * * * * * /root/test.sh
To run on each min of particular hour
* 1,2,5 * * * /root/test.sh

For each field , below are the values.


Field
Range of values
minute
0-59
hour
0-23
day
1-31
month
1-12
day-of-week
0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)