Test String: Public Class Public Static Void Int While Out Print Out Print
Test String: Public Class Public Static Void Int While Out Print Out Print
WHILE CONDITION
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
}
}
OUTPUT:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
OUTPUT:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
DO WHILE CONDITION
do {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
OUTPUT:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
USING BREAK
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
OUTPUT:
10
20
USING CONTINUE
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.print( x );
System.out.print("\n");
}
}
}
OUTPUT:
10
20
40
50
ARRAY
OUTPUT:
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
INHERITANCE
class Calculation {
int z;
OUTPUT:
In the given program, when an object to My_Calculation class is created, a copy of the
contents of the superclass is made within it. That is why, using the object of the subclass
you can access the members of a superclass.
The Superclass reference variable can hold the subclass object, but using that variable
you can access only the members of the superclass, so to access the members of both
classes it is recommended to always create reference variable to the subclass.
If you consider the above program, you can instantiate the class as given below. But
using the superclass reference variable ( cal in this case) you cannot call the
method multiplication(), which belongs to the subclass My_Calculation.
Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and nested classes) from
its superclass. Constructors are not members, so they are not inherited by subclasses,
but the constructor of the superclass can be invoked from the subclass.
OUTPUT:
class Superclass {
int age;
Superclass(int age) {
this.age = age;
}
OUTPUT:
The value of the variable named age in super class is: 24
POLYMORPHISM
OUTPUT:
Constructing an Employee
Constructing an Employee
When you compile the above class, it gives you the following error −
Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error
OUTPUT:
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
The public setXXX() and getXXX() methods are the access points of the instance
variables of the EncapTest class. Normally, these methods are referred as getters and
setters. Therefore, any class that wants to access the variables should access them
through these getters and setters.
The variables of the EncapTest class can be accessed using the following program −
/* File name : RunEncap.java */
public class RunEncap {
OUTPUT: