Example : A simple java program to explain the concept of class & object using constant/                           fixed/static values.
import java.io.*;
class Simple
{            
	int x,y,z;	
	void input()
	{
	x=24;
	y=65;
	}		
	void process()
	{
	z=x+y;
	}			
	void display ()
	{
	System.out.println(z);
	}
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();
        obj1.input();
        obj1.process();
	obj1.display();
	}
}

Example : A simple java program to explain the concept of class & object using static                           method procedure.

import java.io.*;
import java.util.Scanner;
class Simple
{		
		static int num1,num2,result;	
		public static void input()
		{
			Scanner sc= new Scanner(System.in);
			System.out.println("Enter two integer values");
			num1=sc.nextInt();
			num2=sc.nextInt();
		}		
		public static void process()
		{
			result=num1+num2;
		}			
		public static void display ()
		{
			System.out.println(result);
		}
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();
          obj1.input();
          obj1.process();		
          obj1.display();        
	}
}

Example : A simple java program to explain the concept of class & object using dynamic/user given value to check odd or even.

import java.io.*;
import java.util.Scanner;
class Simple
{
	int num;
	Scanner Sc=new Scanner(System.in);	
	void input()
	{
		System.out.println("Enter the value:");
		num=Sc.nextInt();
	}
	void process()
	{
		if(num%2==0)
		{
			System.out.println("The no.is Even");
		}
		else
		{
			System.out.println("The no.is Odd");
		}
	}	
	public static void main(String args[])
		{
			Simple obj1=new Simple();			
			obj1.input();
			obj1.process();			
		}
}

//  ------------  OR  ------------
import java.io.*;
import java.util.Scanner;
class Simple
{
	int num;
	Scanner Sc=new Scanner(System.in);	
	void input()
	{
		System.out.println("Enter the value:");
		num=Sc.nextInt();
	}
	void process()
	{
		if(num%2==0)
		{
			System.out.println("The no.is Even");
		}
		else
		{
			System.out.println("The no.is Odd");
		}
	}	
	public static void main(String args[])
		{
			Simple obj1=new Simple();
			
			obj1.input();
			obj1.process();

                        obj1.input();
			obj1.process();			
		}
}

//  ------------  OR  ------------
import java.io.*;
import java.util.Scanner;
class Simple
{
	int num;
	Scanner Sc=new Scanner(System.in);	
	void input()
	{
		System.out.println("Enter the value:");
		num=Sc.nextInt();
	}
	void process()
	{
		if(num%2==0)
		{
			System.out.println("The no.is Even");
		}
		else
		{
			System.out.println("The no.is Odd");
		}
	}	
	public static void main(String args[])
		{
			Simple obj1=new Simple();			
			obj1.input();
			obj1.process();

                        Simple obj2=new Simple();
                        obj2.input();
			obj2.process();			
		}
}

//  ------------  OR  ------------
import java.io.*;
import java.util.Scanner;
class Simple
{
	int num;
	Scanner Sc=new Scanner(System.in);	
	void input()
	{
		System.out.println("Enter the value:");
		num=Sc.nextInt();
	}
	void process()
	{
		if(num%2==0)
		{
			System.out.println("The no.is Even");
		}
		else
		{
			System.out.println("The no.is Odd");
		}
	}	
	public static void main(String args[])
		{
			Simple obj1=new Simple();
			for(int i=1;i<=5;i++)
			{
			  obj1.input();
			  obj1.process();
			}
		}
}

Example : A simple java program to explain the concept of class & object using two or                        more separate class.

import java.io.*;
import java.util.Scanner;
class Simple
{ 	
	int num1,num2,result;	
	Scanner sc= new Scanner(System.in);
	
		void input()
		{
		System.out.println("Enter two integer values");
		num1=sc.nextInt();
		num2=sc.nextInt();
		}		
		void process()
		{
		result=num1+num2;
		}			
		void display ()
		{
		System.out.println(result);
		}
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();
        obj1.input();
        obj1.process();
	obj1.display();
	}
}

Save as = Simple.java
Compile = javac Simple.java
Run/Interpret = java Complex     //Use main method class name

Example : A simple java program to explain the concept of class & object using parameterized methods.

import java.io.*;
import java.util.Scanner;

class Simple
{ 	
	int num1,num2,result;	
	void input(int x, int y)
	{
		num1=x;
		num2=y;
	}		
	void process()
	{
		result=num1+num2;
	}			
	void display ()
	{
		System.out.println(result);
	}
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();
        
	Scanner sc= new Scanner(System.in);
	System.out.println("Enter two integer values");
	int m=sc.nextInt();
	int n=sc.nextInt();
		
        obj1.input(m,n);
        obj1.process();
	obj1.display();
	}
} 
Example : A simple java program to explain the concept of class & object using Nesting                      of methods.
import java.io.*;
import java.util.Scanner;
class Simple
{ 	
		int num1,num2,result;	
		Scanner sc= new Scanner(System.in);	
		void input()
		{
			System.out.println("Enter two integer values");
			num1=sc.nextInt();
			num2=sc.nextInt();
		}		
		void process()
		{
			result=num1+num2;
		}			
		void display ()
		{
			input();
			process();

			System.out.println(result);
		}
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();		
        obj1.display();        
	}
}
Example : A simple java program to explain the concept of class & object having return                      type output.
import java.io.*;
import java.util.Scanner;

class Simple
{		
	int num1,num2,result;	
	void input()
	{
		Scanner sc= new Scanner(System.in);
		System.out.println("Enter two integer values");
		num1=sc.nextInt();
		num2=sc.nextInt();
	}		
	int process()
	{
		result=num1+num2;
		return result;
	}			
	void display ()		
	{			
		process();
		System.out.println(result);
	}
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();		
        obj1.input();
	obj1.display();        
	}
}

//  ------------  OR  --------------
import java.io.*;
import java.util.Scanner;
class Simple
{		
	int num1,num2,result;	
	void input()
	{
	Scanner sc= new Scanner(System.in);
	System.out.println("Enter two integer values");
	num1=sc.nextInt();
	num2=sc.nextInt();
	}		
	int process()
	{
	result=num1+num2;
	return result;
	}		
}
class Complex
{		
	public static void main(String args[]) 
	{
        Simple obj1 = new Simple();		
        obj1.input();
	int output=obj1.process();
	System.out.println(output);
	}
}
Example : A simple java program to explain the concept of class & object with Method                        Overloading.
(Method Overloading is a feature of java in which a class may have more than one method with same name, but different number and datatype of arguments.)

// ----------------------------------------------------
(I) A Method overloading concept having different number of arguments in the list - 

import java.io.*;
class Example
	{  
	   void display(char c)  // Single Argument
	   {
            System.out.println(c);
	   }
	   void display(char c, int num)   // Two Arguments 
	   {
            System.out.println(c +" "+num);
	   }
           void display(char c, int num, float val)   // Three Arguments 
	   {
            System.out.println(c +" "+num +" "+val);
	   }
	   void display(char c, int num, float val, double y)   // four Arguments 
	   {
            System.out.println(c +" "+num +" "+val+" "+y);
	   }
	}
class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display('X');
       obj.display('Y',800);
       obj.display('P',1400,23.5412f);
       obj.display('P',1400,23.5412f,543.0123);
   }
}
Output :
X
Y 800
P 1400 23.5412
P 1400 23.5412 543.0123

//  --------------------------------------------------
(II) A Method overloading concept having different datatypes as arguments in the list - 

import java.io.*;
class Example
	{  
	   void display(char c)  
	   {
            System.out.println(c);
	   }

	   void display(int num)    
	   {
            System.out.println(num);
	   }

           void display(double val)   
	   {
            System.out.println(val);
	   }

	   void display(String str)   
	   {
            System.out.println(str);
	   }
	}
class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display('X');
       obj.display(800);
       obj.display(23.12);
       obj.display("codershelpline");
   }
}
Output :
X
800
23.12
codershelpline

// ---------------------------------------------------------
(III) A Method overloading concept having different sequence of datatype as argument in the list - 

import java.io.*;
class Example
	{  
	   void display(int num,char c,double val)  
	   {
            System.out.println(c +" "+num +" "+val);
	   }

	   void display(double val,int num,char c)   
	   {
            System.out.println(c +" "+num +" "+val);
	   }

           void display(char c, int num, double val)    
	   {
            System.out.println(c +" "+num +" "+val);
	   }
	}
class Overload
{
   public static void main(String args[])
   {
       Example obj = new Example();
       obj.display(1400,'P',23.12);
	   obj.display(23.12,1400,'P');
       obj.display('P',1400,23.12);
   }
}
Output :
P 1400 23.12
P 1400 23.12
P 1400 23.12
Example : A Java program to explain the concept of class & object having final variable.
import java.io.*;
class Program1
{
  final int x = 200;
  public static void main(String[] args) 
  {
    Program1 Obj1 = new Program1();
    Obj1.x = 210; 
    System.out.println(Obj1.x); 
  }
}

Output :
final.java:9: error: cannot assign a value to final variable x
Obj1.x = 210;  // will generate an error because final variable value can't changed.
        ^
1 error

/*  -----------------  OR   --------------------  */
import java.io.*;
class Program1
{
  final int val=54;
   void display()
   {  
      val=59;
   }  
   public static void main(String args[])
   {  
      Program1 obj=new  Program1();  
      obj.display();  
   }  
}
Output :
final.java:7: error: cannot assign a value to final variable val
      val=59;
      ^
1 error	
NB: we cannot assign any value,even previous similar value, to the final variable.

/*  -----------------  OR   ------------------  */
import java.io.*;
class Program1
{
  final int val;
   void display()
   {  
      val=54;
   }  
   public static void main(String args[])
   {  
      Program1 obj=new  Program1();  
      obj.display();  
   }  
}
Output :
final.java:7: error: cannot assign a value to final blank variable val
      val=54;
      ^
1 error

/*  -------------------  OR   --------------------  */
import java.io.*;
class Program1
{
  final int rollno;
	 
   Program1(int rn)
   {      
      rollno=rn;
   }
   void display()
   {  
      System.out.println("The Assigned roll no is: "+rollno);
   }  
   public static void main(String args[])
   {  
      Program1 x=new  Program1(123045);  
      x.display();  
   }  
}
Output :
G:\javaprg>javac final.java
G:\javaprg>java Program1
The Assigned roll no is: 123045
NB : In blank final variable, value is assigned through constructor concept only.

/*  -----------------  OR   ------------------  */
import java.io.*;
class Program1
{
  static final int rollno;

   static
   { 
      rollno=401587;
   }  
   public static void main(String args[])
   {  
      System.out.println(Program1.rollno);  
   }     
}
NB : In static final variable, value is assigned through static block.
Example : A simple java program to explain the concept of class & object having final                                  method.
(A final method of parent class in java inheritance cannot be overridden though a child class can call the final method of parent class without any issues but it cannot override it.)

import java.io.*;
class Parent
	{  
		final void display()
		{
		  System.out.println("Parent Class Method is Executed");
		}  
	}	     
class child extends Parent
{  
   void display()
   {
      System.out.println("Child Class Method is Executed");
   }	     
   public static void main(String args[])
   {  
      child obj= new child();  
      obj.display();  
   }  
}
Output :
G:\javaprg>javac final.java
final.java:11: error: display() in child cannot override display() in Parent
   void display()
        ^
  overridden method is final
1 error
 
/*  -----------------  OR   -----------------  */
import java.io.*;
class Parent
	{  
	   final void display()
             {
		System.out.println("Parent Class Method is Executed");
	     }  
	}	     
class child extends Parent
{          
   public static void main(String args[])
   {  
      child obj= new child();  
      obj.display();  
   }  
}    
Output :
G:\javaprg>java child
Parent Class Method is Executed  

NB : In the above example we use the parent class final method in sub class without any problem because here we are not overriding the final method.
Example : A simple java program to explain the concept of class & object having final class.
(When a class is defined final then that class will never inherited)
import java.io.*;
final class Parent
	{  
	   void display()
		{
		  System.out.println("Parent Class Method is Executed");
		}  
	}	     
class child extends Parent
{          
   public static void main(String args[])
   {  
      child obj= new child();  
      obj.display();  
   }  
}    

Output :
G:\javaprg>javac final.java
final.java:9: error: cannot inherit from final Parent
class child extends Parent
                    ^
1 error
Example : A simple java program to explain the concept of class & object to copy an object                        elements/values into another object using Object Reference.
import java.io.*;
import java.util.*;
class Person
{
	String name;
	int age;	
	String address;
	
	void display()
	{
		System.out.println("Person information are:"+name+"("+age+")"+"\n"+address);
	}
}	
	class Objref
	{
		public static void main(String[]args)
		{
		Person p=new Person();
		Person q=new Person();
		p.name="Mr.Nadeem";
		p.age=25;
		p.address="Gandhi Maidan Patna";
		
		System.out.println();
		p.display();
		System.out.println();
		
		q=p;
		
		q.display();
		System.out.println();
		
		q.name="Mr.Khurana";
		q.address="Gol Chakkar Ranchi";
		
		p.display();
		System.out.println();
		
		q.display();		
		}
	}

Run as :
C:\>javac Person.java
C:\>java Objref

Output :

Person information are:Mr.Nadeem(25)
Gandhi Maidan Patna

Person information are:Mr.Nadeem(25)
Gandhi Maidan Patna

Person information are:Mr.Khurana(25)
Gol Chakkar Ranchi

Person information are:Mr.Khurana(25)
Gol Chakkar Ranchi
Example : A simple class object java program to show the concept of static block.
import java.io.*;
public class Program1 
{    
  static{
         System.out.println("Static Block Code is Executed");
        }  
  public static void main(String args[]) 
  {
      System.out.println("Main Method Executed"); 
  }
}

Output :
Static Block Code is Executed
Main Method Executed 
Example : An example of java program to show static method.
import java.io.*;
class Program1 
{     
  static int num1;
  static String num2;
  
  static void display()
  {
      System.out.println("The first value  is: "+num1);
      System.out.println("The Second value is: "+num2);
  }
  public static void main(String args[]) 
  {
      display();
  }
}

Output :  
The first value  is: 0
The Second value is: null

/*  ----------------  OR   ----------------  */
import java.io.*;
import java.util.*;
class Program1 
{     
  static int num1=12;
  static String num2="Codershelpline";
  
  static void display()
  {
      System.out.println("The first value  is: "+num1);
      System.out.println("The Second value is: "+num2);
  }
  public static void main(String args[]) 
  {
      display();
  }
}  

Output :
The first value  is: 12
The Second value is: Codershelpline

/*  ------------------  OR   ------------------  */
import java.io.*;
public class Program1 
{      
  static int cube(int num)
  {  
    return num*num*num;  
  }  
  public static void main(String args[]) 
  {
        int result=Program1.cube(7);  
        System.out.println(result);  
  }
}
Output :
343 
Example : An example of java program to show static variable.
import java.io.*;
public class Program1 
{   
    int rollno;
    String stuname;
    static String collegename ="IGNOU";

  Program1(int roll,String name)
  {
    rollno = roll;
    stuname = name;
  }
  void display ()
  {
    System.out.println(rollno+" "+stuname+" "+collegename);
  }
  public static void main(String args[]) 
  {
        Program1 obj1 = new Program1(500,"Monali");
        Program1 obj2 = new Program1(400,"Raman");

        obj1.display();
        obj2.display();
  }
}  

Output : 
500 Monali IGNOU
400 Raman IGNOU
Example : An example of java program to show this keyword.
class Thisexample
{ 
    int num1; 
    int num2;      
    
    Thisexample(int num1, int num2)     //constructor
    { 
        num1 = num1; 
        num2 = num2; 
    }  
    void output()
    {        
        System.out.println("num1 = " + num1 + "  num2 = " + num2); 
    }  
    public static void main(String[] args) 
    { 
        Thisexample obj= new Thisexample(60,65); 
        obj.output(); 
	Thisexample obj1= new Thisexample(33,46); 
        obj1.output(); 
    } 
}
Output :
num1 = 0  num2 = 0
num1 = 0  num2 = 0

/*  ----------------  OR   ------------------  */
class Thisexample
{ 
    int num1; 
    int num2;
    
    Thisexample(int num1, int num2)     //constructor
    { 
        this.num1 = num1; 
        this.num2 = num2; 
    }  
    void output()
    {        
        System.out.println("num1 = " + num1 + "  num2 = " + num2); 
    }  
    public static void main(String[] args) 
    { 
        Thisexample obj= new Thisexample(60,65); 
        obj.output(); 
	Thisexample obj1= new Thisexample(33,46); 
        obj1.output(); 
    } 
}
Output :
num1 = 60  num2 = 65
num1 = 33  num2 = 46

/*  -------------------  OR   ---------------------  */
class Thisexample
{ 
    int num1; 
    int num2; 

	Thisexample() 
    { 
        this(70,90);
	System.out.println("First Value is = " + num1 + "  Second Value is = " + num2); 
    }  	
    
    Thisexample(int num1, int num2) 
    { 
        this.num1 = num1; 
        this.num2 = num2; 
    }  
    void output()
    {        
        System.out.println("First Value is = " + num1 + "  Second Value is = " + num2); 
    }  
    public static void main(String[] args) 
    { 
        Thisexample obj= new Thisexample(); 
	Thisexample obj1= new Thisexample(60,65); 
        obj1.output(); 
    } 
} 
Output :
First Value is = 70  Second Value is = 90
First Value is = 60  Second Value is = 65

/*  ------------------  OR   ------------------  */
class Thisexample
{ 
    int num1; 
    int num2;

    Thisexample() 
    { 
        this(70,90);
	System.out.println("First Value is = " + num1 + "  Second Value is = " + num2); 
    }  	
    
    Thisexample(int num1, int num2) 
    { 
        this.num1 = num1; 
        this.num2 = num2; 
    }  
    void output()
    {        
        System.out.println("First Value is = " + num1 + "  Second Value is = " + num2); 
    }  
    public static void main(String[] args) 
    { 
        Thisexample obj= new Thisexample();		
        obj.output(); 
    } 
}
Output :
First Value is = 70  Second Value is = 90
First Value is = 70  Second Value is = 90

 603 total views,  1 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.