0% found this document useful (0 votes)
23 views

Object and Class Example: Main Within The Class

This document provides examples of object-oriented programming concepts in Java including classes, objects, inheritance, polymorphism, abstraction and interfaces. It demonstrates how to define classes with fields and methods, create objects, initialize objects, extend classes to implement inheritance, define abstract classes and methods, implement interfaces, and use different types of inheritance like single, multilevel and hierarchical inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Object and Class Example: Main Within The Class

This document provides examples of object-oriented programming concepts in Java including classes, objects, inheritance, polymorphism, abstraction and interfaces. It demonstrates how to define classes with fields and methods, create objects, initialize objects, extend classes to implement inheritance, define abstract classes and methods, implement interfaces, and use different types of inheritance like single, multilevel and hierarchical inheritance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

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
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}

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();
System.out.println(s1.id);
System.out.println(s1.name);
}
}

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();
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

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()
{
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

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()
{
System.out.println(id+" "+name+" "+salary);
}
}
public class Employee1 {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}

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

// using protected and private

class Animal

protected String type;

private String color;

public void eat()

System.out.println("Eating");

public void sleep()

System.out.println("Sleeping");

public String getcolor()

{
return color;

public void setcolor(String col)

color=col;

class Dog extends Animal

public void displayinfo(String c)

System.out.println("Iam a" + type);

System.out.println("My color is" + c);

public void bark()

System.out.println("I can bark");

public class Main

public static void main(String args[])

Dog d1=new Dog();

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

//Example for Abstract class

abstract class Animal //abstarct class

public abstract void animalsound(); // abstract method(does not have body)

public void sleep() //normal method

System.out.println("Sleeping");

}
}

class cat extends Animal

public void animalsound()

// body of the abstract class

System.out.println("Tha cat says: Meow meow");

public class abstractexample

public static void main(String args[])

cat c=new cat();

c.animalsound();

c.sleep();

Output:

Tha cat says: Meow meow

Sleeping

//Multilevel inheritance
class Animal //Base classor super class

void eat()

System.out.println("Eating");

class Dog extends Animal //Deriver class or sub class

void bark()

System.out.println("Barking");

class puppy extends Dog

void sleep()

System.out.println("Sleeping");

public class multilevel

{
public static void main(String args[])

puppy p=new puppy();

p.sleep();

p.bark();

p.eat();

Output:

Sleeping

Barking

Eating

//hierarchical inheritance

// 2 or more classes inherits froma single class

class Animal //Base classor super class

void eat()

System.out.println("Eating");

class Dog extends Animal //Deriver class or sub class


{

void bark()

System.out.println("Barking");

class Cat extends Animal

void sound()

System.out.println("meow");

public class hierarchical

public static void main(String args[])

Cat c=new Cat();

c.sound();

c.eat();

Dog d=new Dog();

d.bark();

d.eat();

}
}

Output:

meow

Eating

Barking

Eating

// Interface Example

interface myinterface

public void method1();

public void method2();

public class interfacedemo implements myinterface

public void method1()

System.out.println("Implementation of method1");

public void method2()

System.out.println("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()

System.out.println("This is method1");

public void method2()

System.out.println("This is method2");

public static void main(String args[])

inf2 obj=new demo();

obj.method2();

obj.method1();

Output:

This is method2

This is method1

You might also like