[go: up one dir, main page]

0% found this document useful (0 votes)
37 views5 pages

CSE 215lab - 6

Uploaded by

Astonish Boy
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)
37 views5 pages

CSE 215lab - 6

Uploaded by

Astonish Boy
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
You are on page 1/ 5

CSE 215: Programming Language II Lab

Sec – 8, Faculty - MUO


Lab Officer: Tanzina Tazreen
Lab – 6
Class & Object
Objective:
To learn more about:
• OOP
• Class
• Object
• Attributes
• Method
• Constructor

OOP stands for Object-Oriented Programming.

Everything in Java is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
Object oriented programming allows us to view the entities of the world as objects. For example,
a student, a circle, a car, a book, all of them can be thought of as objects with some properties.

A Class is like an object constructor, or a "blueprint" for creating objects.


Create a Class

To create a class, use the keyword class:

<access_modifier> class <identifier>


{
// attributes
// methods
}

<identifier>: The Name of the Class.


<access_modifier>: defining who can see and access this class (access its attributes and
methods). We will come back to this in details in the next Lab.

For Example:
public class Dog
{
String breed;
int age;
String color;

void barking() {
}

void hungry() {
}

void sleeping() {
}
}

Create an Object

In Java, an object is created from a class.

An object of a class can be created within the same class or in a different class.

In Java, the new keyword is used to create an object.

public class Dog


{
String breed;
int age;
String color;

void barking() {
}

void hungry() {
}

void sleeping() {
}

//with in the same class


public static void main(String[] args)
{
Dog Obj = new Dog();
}
}

// within a different class


public class Main
{
public static void main(String[] args) {
Dog Obj = new Dog();
}
}

Remember that the name of the java file should match the class name.
Here we have 2 classes. So, corresponding to that, we have 2 files also with the same name in the
same directory/folder.
• Dog.java
• Main.java

Class Attributes & Method:


Attributes = Variable (within a class).
Method = Function (you already learn about this)

How to access them?


You can access attributes and methods of a class by creating an object of that class, and by
using the dot syntax (.)

public class Dog


{
String breed;
int age;
String color;
void barking() {
}

void hungry() {
}

void sleeping() {
}

//with in the same class


public static void main(String[] args)
{
Dog Obj = new Dog();
Obj.color = “RED”;
Obj.barking();

//multiple Objects
Dog Obj2 = new Dog();
}
}

Constructors:
• A constructor in Java is a special method with no return type and same method name as
the class.
• The constructor is called when an object of a class is created.
• It can be used to set initial values for object attributes
• It can have zero to multiple parameters

public class Dog


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

// Default Constructor Declaration with no param


public Dog()
{
breed = “NA”;
age = 0;
color = “white”;
}
// Constructor Declaration of Class
public Dog(String breed, int age, String color)
{
this.breed = breed;
this.age = age;
this.color = color;
}

public static void main(String[] args)


{
Dog obj = new Dog();
Dog obj2 = new Dog("papillon", 5, "white");

}
}

Task:

1. Write a program to print the area and perimeter of a circle having radius of 5 units by
creating a class named 'Circle' without any parameter in its constructor. Give another
attribute named “color” then print the value of color in the console.

2. Write a program to print the area and perimeter of a circle having radius of 5 units by
creating a class named 'Circle' with parameters in its constructor. Give another attribute
named “color” then print the value of color in the console.

You might also like