Object and Class Example: main within the class
public class Student
{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
[Link]([Link]);//accessing member through reference variable
[Link]([Link]);
}
}
Output:
0
null
Object and Class Example: main outside the class
class Student
{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
public class Student1{
public static void main(String args[]){
Student s1=new Student();
[Link]([Link]);
[Link]([Link]);
}
}
Output:
Null
3 Ways to initialize object
There are 3 ways to initialize object in Java.
1. By reference variable
2. By method
3. By constructor
1) Object and Class Example: Initialization through reference
class Student
{
int id;
String name;
}
class Student2
{
public static void main(String args[])
{
Student s1=new Student();
[Link]=100;
[Link]="Vijay";
[Link]([Link]+" "+[Link]);//printing members with a white space
}
}
Output:
100 Vijay
create multiple objects and store information in it through reference variable.
class Student
{
int id;
String name;
}
class student1
{
public static void main(String args[])
{
//Creating objects
Student s1=new Student();
Student s2=new Student();
//Initializing objects
[Link]=101;
[Link]="Sonoo";
[Link]=102;
[Link]="Amit";
//Printing data
[Link]([Link]+" "+[Link]);
[Link]([Link]+" "+[Link]);
}
}
Output:
101 Sonoo
102 Amit
2) Object and Class Example: Initialization through method
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
[Link](rollno+" "+name);}
}
public class student1
{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link]();
}
}
Output:
111 Karan
222 Aryan
Object and Class Example: Employee
class Employee
{
int id;
String name;
float salary;
void insert(int i, String n, float s)
{
id=i;
name=n;
salary=s;
}
void display()
{
[Link](id+" "+name+" "+salary);
}
}
public class Employee1 {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
[Link](101,"ajeet",45000);
[Link](102,"irfan",25000);
[Link](103,"nakul",55000);
[Link]();
[Link]();
[Link]();
}
}
Output:
101 ajeet 45000.0
102 irfan 25000.0
103 nakul 55000.0
Object and Class Example: Rectangle
class Rectangle
{
int length;
int width;
void insert(int l, int w)
{
length=l;
width=w;
}
void calculateArea()
{
[Link](length*width);
}
}
class Rectangle1
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
[Link](11,5);
[Link](3,15);
[Link]();
[Link]();
}
}
Output:
55
45
Anonymous Objects:
Anonymous simply means nameless. An object which has no reference is known as an
anonymous object. It can be used at the time of object creation only.
class Calculation
{
void fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
[Link]("factorial is "+fact);
}
public static void main(String args[])
{
new Calculation().fact(5);//calling method with anonymous object
}
}
Output:
Factorial is 120
// Inheritance example
// using protected and private
class Animal
protected String type;
private String color;
public void eat()
[Link]("Eating");
public void sleep()
[Link]("Sleeping");
public String getcolor()
{
return color;
public void setcolor(String col)
color=col;
class Dog extends Animal
public void displayinfo(String c)
[Link]("Iam a" + type);
[Link]("My color is" + c);
public void bark()
[Link]("I can bark");
public class Main
public static void main(String args[])
Dog d1=new Dog();
[Link]();
[Link]();
[Link]();
[Link]="Mammal";
[Link]("brown");
[Link]([Link]());
Output:
Eating
Sleeping
I can bark
Iam aMammal
My color isbrown
//Example for Abstract class
abstract class Animal //abstarct class
public abstract void animalsound(); // abstract method(does not have body)
public void sleep() //normal method
[Link]("Sleeping");
}
}
class cat extends Animal
public void animalsound()
// body of the abstract class
[Link]("Tha cat says: Meow meow");
public class abstractexample
public static void main(String args[])
cat c=new cat();
[Link]();
[Link]();
Output:
Tha cat says: Meow meow
Sleeping
//Multilevel inheritance
class Animal //Base classor super class
void eat()
[Link]("Eating");
class Dog extends Animal //Deriver class or sub class
void bark()
[Link]("Barking");
class puppy extends Dog
void sleep()
[Link]("Sleeping");
public class multilevel
{
public static void main(String args[])
puppy p=new puppy();
[Link]();
[Link]();
[Link]();
Output:
Sleeping
Barking
Eating
//hierarchical inheritance
// 2 or more classes inherits froma single class
class Animal //Base classor super class
void eat()
[Link]("Eating");
class Dog extends Animal //Deriver class or sub class
{
void bark()
[Link]("Barking");
class Cat extends Animal
void sound()
[Link]("meow");
public class hierarchical
public static void main(String args[])
Cat c=new Cat();
[Link]();
[Link]();
Dog d=new Dog();
[Link]();
[Link]();
}
}
Output:
meow
Eating
Barking
Eating
// Interface Example
interface myinterface
public void method1();
public void method2();
public class interfacedemo implements myinterface
public void method1()
[Link]("Implementation of method1");
public void method2()
[Link]("Implementation of method2");
}
public static void main(String args[])
myinterface obj=new interfacedemo();
obj.method1();
obj.method2();
Output
Implementation of method1
Implementation of method2
//interface example
//class extends from another class
//interface extends from another interface
//class implements from interface
interface inf1
public void method1();
interface inf2 extends inf1
public void method2();
public class demo implements inf2
{
public void method1()
[Link]("This is method1");
public void method2()
[Link]("This is method2");
public static void main(String args[])
inf2 obj=new demo();
obj.method2();
obj.method1();
Output:
This is method2
This is method1