Showing posts with label Java-Tutorials. Show all posts
Showing posts with label Java-Tutorials. Show all posts

Monday, October 26, 2015

For Loop

Syntax For Loop :

for (initialization; condition ; increment/decrement(iteration)) {
    Statement1
    Statement2
    .
    .
    .
    StatementN
 }
 
Example:
public class JavaApplication16Java {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int n;
      for( n=10; n> 0; n-- ){
          System.out.println(n);

}
    }
  
}
-----------------------------output--------------------------
 Here this is decrement.

Java Interface

An interface in Java is essentially abstract class- all members are abstract. Java doesn't allow you to extend multiple classes. But Java does allow you to implement multiple interfaces. Actually interfaces are about polymorphism, whereas, inheriting methods are about code reuse. Interface can be extended.
 (1st page)
package iacday6;

public class IACDay6 {

    public static void main(String[] args) {
       
       
        Calculator c = new Calculator();
       
        System.out.println("Sum "+c.sum(10, 15));
        System.out.println("Sub " + c.subtract(10, 5));
        System.out.println("Mul " + c.mul(10, 5));
        System.out.println("Div "+ c.div(10, 5));
       
    }

}

Java Inheritance

Inheritance is very important in Java and PHP programming. Here I will discuss inheritance in Java and PHP language. I hope this will help you much.
Inheritance is the extension of a class. To inherit a class, you use the definition of one class into another by 'extends' keyword. It allows a class to inherit members from another class. However, without any example it is difficult to understand it. So let's start with one of those.
This is the code of inheritance.You can practice with this code.
-----------------------------------------------------------------------------------CODE------------
package javaapplication10;

/**
 *
 * @author juwel
 */
public class JavaApplication10 {
public static void main(String[] args) {
 class A {
 int i, j;
 void showij(){
     System.out.println("i and j" + i + ""+j);
           
        // TODO co  // TODO code application logic here
    }
     void sum(){
         System.out.println("i and j:"+(i+j));
     }
 }
 class B extends A{
     int k;
     void showk(){
         System.out.println("k: "+k);
      
     }
     void sum(){
         System.out.println("i+j+k:"+(i+j+k));
     }
 }