Java oops programming examples

 coding-zon-java-oops-examples

 

 

 Java oops concepts code examples

 

Table of Contents

 

 

Class Methods


example:

class Employee{

  String name;
  String address;	

  void displayName(){
    name="John";
    System.out.println(name);
}

  void displayAddr(){
    address="Delhi";
    System.out.println(address);

} 
  public static void main(String args[])
     { 
 
 Employee emp1=new Employee();

  emp1.displayName();
  emp1.displayAddr();
  
Employee emp2=new Employee();
  emp2.displayName();


Employee emp3=new Employee();
  emp3.displayAddr();
 
  }
 
 }

Result:

C:\javaOO>java Employee
John
Delhi
John
Delhi

 

 

example: 
Person.java

class Person{

      String pname;
      String address;

      void setData()
      {
         pname="John";
         address="Delhi";

    }

     void getData()
      {
        System.out.println("Name="+pname);
        System.out.println("Address="+address);
      }


    public static void main(String args[])
     {
           Person p1= new Person();
           p1.setData();
           p1.getData();
     }
}
 
Result
C:\javaOO>java Person
Name=John
Address=Delhi 
 
 
example:
Addition.java 

 

 

 class Addition

{
int a;
int b;     //declaration of variables
int c;
void init()
{
a = 98;     //initializing variables
b = 78;
}
void add()
{
c = a + b;     //adding variables
}
void print()
{
System.out.println("result = " + c); //printing result
}
public static void main(String args[])
{
Addition  a1 = new Addition();
a1.init();
a1.add();
a1.print();
}
}

 Result:

C:\javaOO>java Addition
result = 176

 

example:
Student.java
 
class Student
{
int sub1;
int sub2;
int sub3;
int tot;
void init(int m1, int m2, int m3)
{
sub1 = m1;
sub2 = m2;
sub3 = m3;
}
void total()
{
tot = sub1 + sub2 + sub3;
}
void display()
{
System.out.println(tot);
}
public static void main(String args[])
{
Student s1 = new Student(); 
 
    s1.init(67,87,98);
    s1.total();
    s1.display();
    Student s2 = new Student();
    s2.init(98,76,80);
    s2.total();
    s2.display(); 
 
 }
}
 
Result:
C:\javaOO>java Student
252
254 
 

 

 Class Constructors

  •  Constructor used to create, initialize the objects.
  •  Constructors nothing but a method but having class name.
  •  Called at the time of object creation with new operator.
  •  Constructor  doesn't have return type void…etc.
  •  Each class will have one default constructor.
example:
 
 
class Test
{
int a;
int b;
int c;
int d;
public Test()
{
a = 0;
b = 0;
c = 0;
d = 0;
}
public Test(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public Test(int a, int b)
{
this.a = a;
this.b = b;
}
void add3()
{
d = a + b + c;
}
void add2()
{
add3();
d = a + b;
}
void print()
{
System.out.println(d);
}
public static void main(String args[])
{
Test t = new Test(87,23,90);
t.add3();
t.print();
}
}
Result:
C:\javaOO>java Test
200 

 

Using 'this' keyword in Constructor

  • 'this' indicates the current object variables. 

  • It is used when class variable and parameters will have the same name.

 

 example:
 
class Student
{
	int sub1;
	int sub2;
	int sub3;
	int tot;
	public Student(){
		sub1 = 0;
		sub2 = 0;
		sub3 = 0;
		tot = 0;
	}
	public Student(int sub1, int sub2, int sub3)
	{
		this.sub1 = sub1;
		this.sub2 = sub2;
		this.sub3 = sub3;
	}
	void total()
	{
		tot = sub1 + sub2 + sub3;
	}
	void display()
	{
		System.out.println(tot);
	}
	public static void main(String args[])
	{
		Student s1 = new Student(78,98,56);
		s1.total();     //clculates total
		s1.display();    // displas tot
		Student s2 = new Student();
		s2.total();
		s2.display();
	}
	
}

Result:

H:\JavaNotes\javaOO>java Student
 

232   // s1 total
0      // s2 total

Calling different constructors by sending different values with object.

example:

 

 
class Construct
{
	int a;
	float b;
	double d;
	char ch;
	String s1;
	public Construct(int a)
	{
		this.a = a;
		System.out.println("int constructor invoked " + a);
	}
	public Construct(float b)
	{
		this.b = b;
		System.out.println("float constructor invoked " + b);
	}
	public Construct(double d)
	{
		this.d = d;
		System.out.println(" double con invoked");
	}
	public Construct(char ch)
	{
		this.ch = ch;
		System.out.println("character con invoked");
	}
	public static void main(String args[])
	{
		Construct ob1 = new Construct (87.5f);
		Construct ob2 = new Construct ('a');
	}
}

 Result:

C:\javaOO>java  Construct
float constructor  invoked 87.5
character constructor invoked

Using object of one class in another class.

Write below given two classes(class A, class B) in the same file and save it as B.java.

as shown in the below image

 

 

coding-zons-class-obj2

 

 

 

 
example:
 
B.java
 
class A
{
	int a;
	A( int a)
	{
		this.a = a;
	}
	int square()
	{
		return (a*a);
	}
}
public class B
{
	public static void main(String args[]){
		A objA = new A(5);
             int x = objA.square();
		System.out.print("sqare = " + x);
	}
}

 

C:\javaOO>javac B.java

C:\javaOO>java B
sqare = 25
H:\JavaNotes\javaOO
>

example 2:

 

One class object can be created in another class. Both the files 

A.java and B.java save in the same folder. And run the B.java.

 
A.java


class A
{
	int a;
	A ( int a)
	{
		this.a = a;
	}
	int square()
	{
		return (a*a);
	}
} 
 
 
B.java
 
public class B
{
	public static void main(String args[]){
		A objA = new A(5);
		int x = objA.square();
		System.out.print("sqare = " + x);
	}
}

 

Result:

C:\javaOO>javac B.java

C:\javaOO>java B
sqare = 25


coding-zon-class-obj2

 

 

 Method Overloading

 

A single class can have multiple methods with same name and different parameters list
and different return types.
 

example:
OverloadMethod.java 

 

class OverloadMethod
{
int add(int x,int y)
{
return (x + y);
}
String add(String s1,String s2)
{
return (s1 + s2);
}
 
public static void main(String args[])
{ 
 
OverloadMethod om = new OverloadMethod(); 
 
    System.out.println(om.add("abc","xyz"));
    System.out.println(om.add(5,6));
 
 }
} 
 

 Result:

 

C:\javaOO>java OverloadMethod


abcxyz
11
 

 
 
*** always hightest datatype constructors are invoked.

 

 Access modifiers 

  1. static and
  2. final

 

static keyword


  • It can be used with variable and methods.  
  • Static variable/method available with the class itself, No need to create an Object.
  • Static variable used only with in the static methods or in static blocks.

 

example:

 

class StaticDemo
{
	static int a;
 
   StaticDemo()
	{
		a = 10;
	}
	static void display()
	{
		System.out.println("a = " + a);
	}
	public static void main(String args[])
	{
		StaticDemo d = new StaticDemo();
		StaticDemo.display();  
	}
}
 
C:\javaOO>javac StaticDemo.java
C:\javaOO>java StaticDemo
a = 10
 
 
 
 
example2: 
 
class StaticDemo2
{
	static int c; 
 
       StaticDemo2()
	{
		c++;
		System.out.println("c = " + c);
	}
	public static void main(String args[])
	{
		StaticDemo2 ob1 = new StaticDemo2 ();
		StaticDemo2 ob2 = new StaticDemo2 ();
		StaticDemo2 ob3 = new StaticDemo2 ();
	}
}
 
Result
 
 
C:\javaOO>javac StaticDemo2.java
C:\javaOO>java StaticDemo2
c = 1
c = 2
c = 3 
 
 
 Menu
example3: 
 
class StaticDemo3{
	static String college, name;
	static int rno;
	 

public StaticDemo3()
{
	name = null;
	rno = 0;
	college = null;
}
public StaticDemo3(String n,int rn)
{
	name = n;
	rno = rn;
}
void display()
{
	System.out.println("Name = " + name);
	System.out.println("Rno = " + rno);
	System.out.println("College = " + college);
}
static void change()
{
	college = "NJ";
}
public static void main(String args[])
{
	StaticDemo3 ob1 = new StaticDemo3();
	ob1.display();
	
	StaticDemo3 ob2 = new StaticDemo3();
	StaticDemo3.change();
	ob2.display();
StaticDemo3 ob3 = new StaticDemo3("John",100);
	StaticDemo3.change();
	ob3.change();
	ob3.display();
}
}
 

Result:

 

C:\javaOO>java StaticDemo3

 

Name = null
Rno = 0

College = null

 

Name = null
Rno = 0

College = NJ

 

Name = John

Rno = 100

College = NJ

 

final keyword

final can be used with variable, methods, class

  •  final variable ----the values of variable can't be changed
  •  final methods --- can't be redefined in child class
  •  final class ---- can't be extended
example:

 

public class FinalDemo
{
	final float PI = 3.14f;
	int r;
	float ac = 0.0f;
	void area(){
		r = 6;
		ac = PI*r*r;
	}
	void display()
	{
		System.out.println("ac = " + ac);
	}
	public static void main(String args[]){
		FinalDemo fd = new FinalDemo();
		fd.area();
		fd.display();
	}
} 
 
Output
ac = 113.04

 

 

 


 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Recently viewed list in PHP Mysql

Import CSV File Into MySQL Using PHP

Shoppingcart using PHP Sessions - Miniproject