Object and Class Example: Main Within The Class
Object and Class Example: Main Within The Class
Output:
0
null
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();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Output:
Null
1. By reference variable
2. By method
3. By constructor
class Student
{
int id;
String name;
}
class Student2
{
public static void main(String args[])
{
Student s1=new Student();
s1.id=100;
s1.name="Vijay";
System.out.println(s1.id+" "+s1.name);//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
s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data
System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
Output:
101 Sonoo
102 Amit
class Student
{
int rollno;
String name;
void insertRecord(int r, String n)
{
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);}
}
public class student1
{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
Output:
111 Karan
222 Aryan
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()
{
System.out.println(length*width);
}
}
class Rectangle1
{
public static void main(String args[])
{
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
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;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[])
{
new Calculation().fact(5);//calling method with anonymous object
}
}
Output:
Factorial is 120
// Inheritance example
class Animal
System.out.println("Eating");
System.out.println("Sleeping");
{
return color;
color=col;
d1.eat();
d1.sleep();
d1.bark();
d1.type="Mammal";
d1.setcolor("brown");
d1.displayinfo(d1.getcolor());
Output:
Eating
Sleeping
I can bark
Iam aMammal
My color isbrown
System.out.println("Sleeping");
}
}
c.animalsound();
c.sleep();
Output:
Sleeping
//Multilevel inheritance
class Animal //Base classor super class
void eat()
System.out.println("Eating");
void bark()
System.out.println("Barking");
void sleep()
System.out.println("Sleeping");
{
public static void main(String args[])
p.sleep();
p.bark();
p.eat();
Output:
Sleeping
Barking
Eating
//hierarchical inheritance
void eat()
System.out.println("Eating");
void bark()
System.out.println("Barking");
void sound()
System.out.println("meow");
c.sound();
c.eat();
d.bark();
d.eat();
}
}
Output:
meow
Eating
Barking
Eating
// Interface Example
interface myinterface
System.out.println("Implementation of method1");
System.out.println("Implementation of method2");
}
obj.method1();
obj.method2();
Output
Implementation of method1
Implementation of method2
//interface example
interface inf1
System.out.println("This is method1");
System.out.println("This is method2");
obj.method2();
obj.method1();
Output:
This is method2
This is method1