0% found this document useful (0 votes)
29 views38 pages

Classes and Objects Example Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views38 pages

Classes and Objects Example Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Creation of an Object

public class Main22 {


int x = 5;
public static void main(String[] args) {
Main22 myObj = new Main22();
[Link](myObj.x);
}
}
Output:

//Multiple Objects Cretion


public class Main23 {
int x = 5;

public static void main(String[] args) {


Main23 myObj1 = new Main23(); //
Object 1
Main23 myObj2 = new Main23(); //
Object 2
[Link](myObj1.x);
[Link](myObj2.x);
}
}
Output:

//Using Multiple Classes


public class Main24 {
int x = 5;
}
class Second {
public static void main(String[] args) {
Main24 myObj = new Main24();
[Link](myObj.x);
}
}

Output:
Javac [Link]
Javac [Link]
Java Second

//Example: Java Class and Objects


class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;

// method to turn on the light


void turnOn() {
isOn = true;
[Link]("Light on? " + isOn);

// method to turnoff the light


void turnOff() {
isOn = false;
[Link]("Light on? " + isOn);
}
}

class Main25 {
public static void main(String[] args) {

// create objects led and halogen


Lamp led = new Lamp();
Lamp halogen = new Lamp();

// turn on the light by


// calling method turnOn()
[Link]();

// turn off the light by


// calling method turnOff()
[Link]();
}
}
Output:

//Example: Create objects inside the same


class
class Lamp {

// stores the value for light


// true if light is on
// false if light is off
boolean isOn;

// method to turn on the light


void turnOn() {
isOn = true;
[Link]("Light on? " + isOn);

public static void main(String[] args) {

// create an object of Lamp


Lamp led = new Lamp();

// access method using object


[Link]();
}
}
Output:
//Java Program to illustrate how to define a
class and fields
//Defining a Student class.
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:

//Java Program to demonstrate having the


main method in
//another class
//Creating Student class.
class Student12{
int id;
String name;
}
//Creating another class TestStudent1
which contains the main method
class TestStudent1{
public static void main(String args[]){
Student12 s1=new Student12();
[Link]([Link]);
[Link]([Link]);
}
}
Output:

//1) Object and Class Example:


Initialization through reference
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
[Link]=101;
[Link]="Sonoo";
[Link]([Link]+"
"+[Link]);//printing members with a
white space
}
}
Output:

class Student{
int id;
String name;
}
//Object and Class Example: Initialization
through reference
class TestStudent3{
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:

//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](r
ollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
[Link](111,"Karan");
[Link](222,"Aryan");
[Link]();
[Link]();
}
}
Output:

//3) Object and Class Example:


Initialization through a constructor
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 TestEmployee {
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:

//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 TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
[Link](11,5);
[Link](3,15);
[Link]();
[Link]();
}
}
Output:

//Creating multiple objects by one type


only
//Java Program to illustrate the use of
Rectangle class which
//has length and width data members
class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void
calculateArea(){[Link](length
*width);}
}
class TestRectangle2{
public static void main(String args[]){
Rectangle r1=new Rectangle(),r2=new
Rectangle();//creating two objects
[Link](11,5);
[Link](3,15);
[Link]();
[Link]();
}
}
Output:
//Real World Example: Account
//Java Program to demonstrate the working
of a banking-system
//where we deposit and withdraw amount
from our account.
//Creating an Account class which has
deposit() and withdraw() methods
class Account{
int acc_no;
String name;
float amount;
//Method to initialize object
void insert(int a,String n,float amt){
acc_no=a;
name=n;
amount=amt;
}
//deposit method
void deposit(float amt){
amount=amount+amt;
[Link](amt+" deposited");
}
//withdraw method
void withdraw(float amt){
if(amount<amt){
[Link]("Insufficient Balance");
}else{
amount=amount-amt;
[Link](amt+" withdrawn");
}
}
//method to check the balance of the
account
void
checkBalance(){[Link]("Balan
ce is: "+amount);}
//method to display the values of an object
void
display(){[Link](acc_no+"
"+name+" "+amount);}
}
//Creating a test class to deposit and
withdraw amount
class TestAccount{
public static void main(String[] args){
Account a1=new Account();
[Link](832345,"Ankit",1000);
[Link]();
[Link]();
[Link](40000);
[Link]();
[Link](15000);
[Link]();
}}
Output:

// Class Declaration

public class Dog {


// Instance Variables
String name;
String breed;
int age;
String color;

// Constructor Declaration of Class


public Dog(String name, String breed,
int age,
String color)
{
[Link] = name;
[Link] = breed;
[Link] = age;
[Link] = color;
}

// method 1
public String getName() { return name;
}

// method 2
public String getBreed() { return breed;
}
// method 3
public int getAge() { return age; }

// method 4
public String getColor() { return color; }

@Override public String toString()


{
return ("Hi my name is " +
[Link]()
+ ".\nMy breed,age and color are
"
+ [Link]() + "," +
[Link]()
+ "," + [Link]());
}

public static void main(String[] args)


{
Dog tuffy
= new Dog("tuffy", "papillon", 5,
"white");
[Link]([Link]());
}
}

Output:

public class GFG {


// sw=software
static String sw_name;
static float sw_price;
static void set(String n, float p)
{
sw_name = n;
sw_price = p;
}

static void get()


{
[Link]("Software name is:
" + sw_name);
[Link]("Software price is:
"
+ sw_price);
}

public static void main(String args[])


{
[Link]("Visual studio", 0.0f);
[Link]();
}
}
Output:
Class Object

Class is the blueprint of an object. It is


An object is an instance of the class.
used to create objects.

No memory is allocated when a class is Memory is allocated as soon as an object


declared. is created.

An object is a real-world entity such as a


A class is a group of similar objects.
book, car, etc.

Class is a logical entity. An object is a physical entity.

Objects can be created many times as per


A class can only be declared once.
requirement.

Objects of the class car can be BMW,


An example of class can be a car.
Mercedes, Ferrari, etc.

public class Puppy {


int puppyAge;

public Puppy(String name) {


// This constructor has one parameter,
name.
[Link]("Name chosen is :"
+ name );
}

public void setAge( int age ) {


puppyAge = age;
}

public int getAge( ) {


[Link]("Puppy's age is :" +
puppyAge );
return puppyAge;
}

public static void main(String []args) {


/* Object creation */
Puppy myPuppy = new Puppy(
"tommy" );
/* Call class method to set puppy's age
*/
[Link]( 2 );

/* Call another class method to get


puppy's age */
[Link]( );

/* You can access instance variable as


follows as well */
[Link]("Variable Value :"
+ [Link] );
}
}
Output:
import [Link].*;
public class Employee {

String name;
int age;
String designation;
double salary;

// This is the constructor of the class


Employee
public Employee(String name) {
[Link] = name;
}
// Assign the age of the Employee to the
variable age.
public void empAge(int empAge) {
age = empAge;
}

/* Assign the designation to the variable


designation.*/
public void empDesignation(String
empDesig) {
designation = empDesig;
}

/* Assign the salary to the variable


salary.*/
public void empSalary(double
empSalary) {
salary = empSalary;
}

/* Print the Employee details */


public void printEmployee() {
[Link]("Name:"+ name );
[Link]("Age:" + age );
[Link]("Designation:" +
designation );
[Link]("Salary:" + salary);
}
}
import [Link].*;
public class EmployeeTest {

public static void main(String args[]) {


/* Create two objects using constructor
*/
Employee empOne = new
Employee("James Smith");
Employee empTwo = new
Employee("Mary Anne");

// Invoking methods for each object


created
[Link](26);
[Link]("Senior
Software Engineer");
[Link](1000);
[Link]();

[Link](21);
[Link]("Software
Engineer");
[Link](500);
[Link]();
}
}

Output:

public class Main59 {


// Static method
static void myStaticMethod() {
[Link]("Static methods can
be called without creating objects");
}

// Public method
public void myPublicMethod() {
[Link]("Public methods
must be called by creating objects");
}

// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static
method
// myPublicMethod(); This would
compile an error
Main59 myObj = new Main59(); //
Create an object of Main
[Link](); // Call the
public method on the object
}
}
Output:

You might also like