You are on page 1of 18

Objectives

 Basic concept of class and object


 How to create an object
 Concept and access of instance variable
 How to use method in class
 Some related program example

1
Software design Activities

 Communication
 Planning
 Modeling
◦ Analysis of requirements
◦ Design
 Construction
◦ Code generation
◦ Testing
 Deployment
Class and object
 To represent data we need class, and class is the core
concept of OOPS. Class is a blueprint of object that contains
variables and methods. It is logical representation of data.
 Object − Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors – wagging
the tail, barking, eating.An object is an instance of a class.
 Let us now look deep into what are objects. If we consider
the real-world, we can find many objects around us, cars,
dogs, humans, etc. All these objects have a state and a
behavior.
 If we consider a dog, then its state is - name, breed, color, and
the behavior is - barking, wagging the tail, running.
3
General form of a class
class Vehicle{
class classname{
// declare instance variable
int passenger; // number of passenger
Type var1; int fulecap; // fuel capacity
Type var 2; int mpg; // fuel consumption in miles
per gallon
// declare method }
Type method1(parameter)
{
Body of the method
}
Type method 2(parameter)
{
Body of the method
}

4
local variable Variables
Local variables are declared in methods, constructors, or blocks.

Local variables are created when the method, constructor or block is


entered and the variable will be destroyed once it exits the method,
constructor, or block.

Access modifiers cannot be used for local variables.

Local variables are visible only within the declared method, constructor,
or block.

There is no default value for local variables, so local variables should be


declared and an initial value should be assigned before the first use.
local variable: Example
public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]) {
Test test = new Test();
test.pupAge();
}
}
Instance Variable
Instance variables are declared in a class, but outside a method,
constructor or any block.

Instance variables are created when an object is created with the use of
the keyword 'new' and destroyed when the object is destroyed.

Access modifiers can be given for instance variables.

Instance variables have default values. For numbers, the default value is
0, for Booleans it is false. Values can be assigned during the declaration or
within the constructor.
Instance Variable
import java.io.*; }
public class Employee { // This method prints the employee
// this instance variable is visible for details.
any child class. public void printEmp() {
public String name; System.out.println("name : " + name
// salary variable is visible in );
Employee class only. System.out.println("salary :" +
private double salary; salary);
// The name variable is assigned in }
the constructor. public static void main(String args[])
public Employee (String empName) { {
name = empName; Employee empOne = new
} Employee("Ransika");
// The salary variable is assigned a empOne.setSalary(1000);
value. empOne.printEmp();
public void setSalary(double empSal) }
{ }
salary = empSal;
Instance Variable: program Example
public class Add
{
float b;
int a;
boolean c;
public static void main(String args[])
{
Add p = new Add();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
}
}
Access Modifier: public
class Sample{ q.x=10;
int x; q.y= (float) 10.5;
float y;
} System.out.println(q.x);
public class Add1 System.out.println(q.y);
{
float b; }
int a; }
boolean c;
public static void main(String args[])
{
Add1 p = new Add1();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
Sample q=new Sample();
Access Modifier: private
class Sample{ q.x=10;
private int x; q.y= (float) 10.5;
private float y;
} System.out.println(q.x);
public class Add1 System.out.println(q.y);
{
float b; }
int a; }
boolean c;
public static void main(String args[])
{
Add1 p = new Add1();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
Sample q=new Sample();
Class: Program concept
class Vehicle{

int passenger; // number of passenger


int fulecap; // fuel capacity
int mpg; // fuel consumption in miles per gallon
}

Object: Instance of a class

Vehicle minivan = new Vehicle(); // minivan is the instance of vehicle

After creating object, it will hold all the instance variable defined in class.

(.) operator is used to access the instance variable.

Syntax: object.member;
Object creation: Alternate approach
Vehicle minivan; // declares references to object

minivan=new Vehicle();

Vehicle car1=new Vehicle();

Vehicle car2=car1;
Class: Program example
class Vehicle{
int passenger; // number of passenger
int fulecap; // fuel capacity
int mpg; // fuel consumption in miles per gallon
}

Class vehicledemo{
public static void main(String args[]){
Vehicle minivan=new Vehicle();
int range;
minivan.passenger=7;
minivan.fuelcap=16;
minivan.mpg=21;
range=minivan.fuelcap*minivan.mpg;
System.out.println(“minivan can caryy” + minivan.passenger + “with a
range of “ + range);
}
}
Adding method to a class
Parameterized method to a class
User input variable: Program example
import java.util.Scanner;

class vehicle{
int passenger;
}

public class test


{
public static void main(String args[])
{
vehicle p = new vehicle();
Scanner q = new Scanner(System.in);
System.out.println("enter passenger");
int r=q.nextInt();
p.passenger=r;
System.out.println("the number of passenger is " + p.passenger);
}
}
Exercise
1. Declare two classes to return area and parameter of circle and
rectangle. The required parameter should be taken during runtime.

You might also like