Creating Class and Object
Simple Class Creating and Object Example
Student Report Card Example
Simple Instance Variable
Another Instance Variable Example
Static Method and variable Example
Simple Instance Method Example
Another Instance Method Example
Example of Overriding
Simple Class Creating and Object Example
public class Demo
{
int id=101;
String name="TechKnow";
public static void main(String[] args)
{
Demo obj=new Demo();//obj is instance of Demo class
System.out.println("id"+" "+obj.id);
System.out.println("name"+" "+obj.name);
}
}
Output
id 101 name TechKnow
Student Report Card Example
Student Report Card Example
class Student
{
int rollno;
String name;
int m1 , m2 , m3;
int tot;
float per;
String grade;
void input(int rollno , String name, int m1 , int m2 , int m3)
{
this.rollno = rollno;
this.name = name;
this.m1 = m1;
this.m2 = m2;
this.m3= m3;
}
void cal()
{
tot = m1+ m2 + m3;
per = tot/3;
if(per>90)
grade= "A";
else
grade="B";
}
void print()
{
System.out.println("** Student Report Card **");
System.out.println("Rollno "+rollno);
System.out.println("Name "+name);
System.out.println("Marks1 "+m1);
System.out.println("Marks2 "+m2);
System.out.println("Marks3 "+m3);
System.out.println("Total "+tot);
System.out.println("Per "+per);
System.out.println("Grade "+grade);
}
public static void main(String args[])
{
Student obj = new Student();
obj.input(1001, "Ram",90, 89 , 78);
obj.cal();
obj.print();
}
}
Output
** Student Report Card ** Rollno 1001 Name Ram Marks1 90 Marks2 89 Marks3 78 Total 257 Per 85.0 Grade B
Simple Instance Variable
Simple Instance Variable
class Emp
{
int empno; // Instance variable
String name;
float sal;
Emp() // This is Default Cons, Constructor Call Automatically
//It intialize the instance Var
{
System.out.println("This is Default Cons ");
empno = 1001;
name = "Ram";
sal = 9090.0f;
}
Emp(int empno , String name , float sal)
{
this.empno = empno;
this.name = name;
this.sal = sal;
}
public void print()
{
System.out.println("Empno "+empno +" Name "+ name +" Sal "+sal);
}
public static void main(String agrs[])
{
Emp e = new Emp();
e.print();
Emp e1 = new Emp(1002,"Shyam", 8883.3f);
e1.print();
}
}
Output
This is Instance Variable Empno 1001 Name Ram Sal 9090.0 Empno 1002 Name Shyam Sal 8883.3
Another Instance Variable Example
Another Instance Variable Example
public class Demo
{
int num;
Demo()
{
num++;
}
public static void main(String[] args)
{
Demo d1 = new Demo();
System.out.println("No. of instances for d1 : "+ d1.num);
Demo d2 = new Demo();
System.out.println("No. of instances for d1 : "+ d1.num);
System.out.println("No. of instances for d2 : "+ d2.num);
Demo d3 = new Demo();
System.out.println("No. of instances for d1 : "+ d1.num);
System.out.println("No. of instances for d2 : "+ d2.num);
System.out.println("No. of instances for d3 : "+ d3.num);
}
}
Output
No. of instances for d1 : 1 No. of instances for d1 : 1 No. of instances for d2 : 1 No. of instances for d1 : 1 No. of instances for d2 : 1 No. of instances for d3 : 1
Static Method and variable Example
Static Method and variable Example
class Demo
{
int length = 10;
int breadth = 10;
int height = 10;
public static int num = 0; // static variable
public static int NoOfCubes() //static method
{
return num;
}
public Demo()
{
num++;
}
public static void main(String args[])
{
System.out.println("Number of Cube objects = " + Demo.num);
System.out.println("Number of Cube objects = "+ Demo.NoOfCubes());
}
}
Output
Number of Cube objects = 0 Number of Cube objects = 0
Simple Instance Method Example
Simple Instance Method Example
class Check
{
void display(String name,int id)//Instance Method
{
System.out.println("Name"+" "+name);
System.out.println("Id"+" "+id);
}
public static void main(String args[])
{
Check cc=new Demo();
cc.display("Techknow",101);
}
}
Output
Name Techknow Id 101
Another Instance Method Example
Another Instance Method Example
class Demo
{
static int id;
void display()
{
System.out.println("id="+id);
id++;
}
void display1()
{
System.out.println("id="+id);
id++;
}
void display2()
{
System.out.println("id="+id);
id++;
}
public static void main(String args[])
{
Demo d1=new Demo();
d1.display();
d1.display1();
d1.display2();
}
}
Output
id=0 id=1 id=2
Encapsulation Example
Example of EncapsulatonExample of Encapsulaton
package com.encapsule;
public class Demo
{
private String name;
private String idNum;
private int age;
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
public String getIdNum()
{
return idNum;
}
public void setAge( int newAge)
{
age = newAge;
}
public void setName(String newName)
{
name = newName;
}
public void setIdNum( String newId)
{
idNum = newId;
}
}
Output
Name : Shivank Age : 23
Inheritance Example
Example of InheritanceExample of Inheritance
package inherit;
class Person
{
String name;
int age;
public void input(String name, int age)
{
this.name=name;
this.age=age;
}
public void show()
{
System.out.println("Calling from Person");
System.out.println("NAME : "+name);
System.out.println("AGE : "+age);
}
}
class Student extends Person
{
String standard;
float m1,m2,m3,total,percentage;
int stdno;
public void input(String standard,float m1,float m2,float m3,int
stdno)
{
this.standard=standard;
this.m1=m1;
this.m2=m2;
this.m3=m3;
this.stdno=stdno;
}
public void show()
{
System.out.println("Calling from Student");
System.out.println("standard : "+standard);
System.out.println("marks in physics :"+m1);
System.out.println("marks in chemistry :"+m2);
System.out.println("marks in maths :"+m3);
System.out.println("Student number :"+stdno);
System.out.println("total marks :"+total);
System.out.println("percentage marks :"+percentage);
}
public void marks()
{
total=m1+m2+m3;
percentage=total*100/300.0f;
}
}
class Employee extends Student
{
int empno;
float salary;
float hra,gs;
public void input(int empno,float salary)
{
this.empno=empno;
this.salary=salary;
}
public void show()
{
System.out.println("Calling from Employee");
System.out.println("EMPLOYEE NUMBER : "+empno);
System.out.println("salary : "+salary);
System.out.println("HRA : "+hra);
System.out.println("GS : "+gs);
}
public void calculate()
{
hra= 0.3f*salary;
gs=salary+hra;
}
}
Inheritance.java
package inherit;
class Inheritance
{
public static void main(String args[])
{
Person p=new Person();
p.input("abhishek",22);
p.show();
p=new Student();
Student s= new Student();
s.input("btech",90,92,93,06);
s.marks();
p.show();
s.show();
p=new Employee();
Employee e= new Employee();
e.input(5751, 80000);
e.calculate();
e.show();
s.show();
p.show();
}
}
Output
Calling from Person NAME : abhishek AGE : 22 Calling from Student standard : null marks in physics :0.0 marks in chemistry :0.0 marks in maths :0.0 Student number :0 total marks :0.0 percentage marks :0.0 Calling from Student standard : btech marks in physics :90.0 marks in chemistry :92.0 marks in maths :93.0 Student number :6 total marks :275.0 percentage marks :91.666664 Calling from Employee EMPLOYEE NUMBER : 5751 salary : 80000.0 HRA : 24000.0 GS : 104000.0 Calling from Student standard : btech marks in physics :90.0 marks in chemistry :92.0 marks in maths :93.0 Student number :6 total marks :275.0 percentage marks :91.666664 Calling from Employee EMPLOYEE NUMBER : 0 salary : 0.0 HRA : 0.0 GS : 0.0
Examples of Polymorphism
Example of OverloadingExample of Overloading
public class Test
{
public void add(int i, int j)
{
System.out.println(i + j);
}
public void add(String p,String r)
{
System.out.println(p + r);
}
public void add( char a, char b)
{
System.out.println(a + b);
}
public static void main(String [] args)
{
Test ol = new Test();
ol.add(15,20);
ol.add("Tech", "know");
ol.add('g','h');
}
}
Output
35 Techknow 207
Example of Overriding
Example of Overriding
class Animal
{
public void move()
{
System.out.println("Animals can move");
}
}
class Dog extends Animal
{
public void move()
{
System.out.println("Dogs can walk and run");
}
}
public class Test
{
public static void main(String args[])
{
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
}
Output
Animals can move Dogs can walk and run


