(1)CLASS EXAMPLE FOR CREATING OBJECT & CALLING MEMBER VARIABLE & MEMBER FUNCTIONS.
public class Puppy
{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( )
{
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args)
{
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
Output:
D:\>javac Puppy.java
D:\>java Puppy
Passed Name is :tommy
Puppy's age is :2
Variable Value :2
(2)ARRAY EXAMPLE:
class arrayexample
{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}
}
Output:
D:\>javac arrayexample.java
D:\>java arrayexample
10
20
70
40
50
(3)STRING EXAMPLE:
public class StringExample
{
public static void main(String args[])
{
String s1="java"; //creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch); //converting char array to string
String s3=new String("example"); //creating java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
here is ouput :
D:\>set path="C:\Program Files (x86)\Java\jdk1.8.0_40\bin"
D:\>javac StringExample.java
D:\>java StringExample
java
strings
example
(4)JAVA INHERITANCE EXAMPLE:
save following code Programmer.java note:here is Employee is super class and Programmer is child class :
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
D:\>javac Programmer.java
D:\>java Programmer
Programmer salary is:40000.0
Bonus of Programmer is:10000
(5)JAVA INTERFACE:
(a)create interface and save following code as "Animal.java":
interface Animal {
public void eat();
public void travel();
}
Then complie it
D:\>javac Animal.java
(b) write code for a class that implements this interface and save it as "MammalInt.java":
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Then run it:
D:\>javac MammalInt.java
D:\>java MammalInt
Mammal eats
Mammal travels
(6)PACKAGE EXAMPLE:
(a)package exampel in java:
first create a folder name abc:
(b)write coe for A.java :
package abc;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
(c)compile it :
D:\>javac A.java
then you will get bytecode file A.class
copy this file & paste it into abc folder.
(d)now import this class from abc folder example save following code by name B.java :
import abc.A; // here we are importing class A from package abc
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
now run it:
D:\>javac B.java
D:\>java B
Hello
public class Puppy
{
int puppyAge;
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public void setAge( int age ){
puppyAge = age;
}
public int getAge( )
{
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args)
{
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );
/* Call class method to set puppy's age */
myPuppy.setAge( 2 );
/* Call another class method to get puppy's age */
myPuppy.getAge( );
/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}
Output:
D:\>javac Puppy.java
D:\>java Puppy
Passed Name is :tommy
Puppy's age is :2
Variable Value :2
(2)ARRAY EXAMPLE:
class arrayexample
{
public static void main(String args[])
{
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}
}
Output:
D:\>javac arrayexample.java
D:\>java arrayexample
10
20
70
40
50
(3)STRING EXAMPLE:
public class StringExample
{
public static void main(String args[])
{
String s1="java"; //creating string by java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch); //converting char array to string
String s3=new String("example"); //creating java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
here is ouput :
D:\>set path="C:\Program Files (x86)\Java\jdk1.8.0_40\bin"
D:\>javac StringExample.java
D:\>java StringExample
java
strings
example
(4)JAVA INHERITANCE EXAMPLE:
save following code Programmer.java note:here is Employee is super class and Programmer is child class :
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
D:\>javac Programmer.java
D:\>java Programmer
Programmer salary is:40000.0
Bonus of Programmer is:10000
(5)JAVA INTERFACE:
(a)create interface and save following code as "Animal.java":
interface Animal {
public void eat();
public void travel();
}
Then complie it
D:\>javac Animal.java
(b) write code for a class that implements this interface and save it as "MammalInt.java":
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 0;
}
public static void main(String args[]){
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}
Then run it:
D:\>javac MammalInt.java
D:\>java MammalInt
Mammal eats
Mammal travels
(6)PACKAGE EXAMPLE:
(a)package exampel in java:
first create a folder name abc:
(b)write coe for A.java :
package abc;
public class A
{
public void msg()
{
System.out.println("Hello");
}
}
(c)compile it :
D:\>javac A.java
then you will get bytecode file A.class
copy this file & paste it into abc folder.
(d)now import this class from abc folder example save following code by name B.java :
import abc.A; // here we are importing class A from package abc
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
now run it:
D:\>javac B.java
D:\>java B
Hello
Sign up here with your email
ConversionConversion EmoticonEmoticon