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

Lab 06 - Classes, Objects & Constructors: Objective

The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for creating objects with common properties and methods. A class declaration includes modifiers, the class name, superclass, interfaces, and body. Constructors initialize new objects, fields provide state, and methods implement behavior. An object represents a real-world entity with state, behavior, and identity. The document provides an example Dog class with properties, constructor, methods, and demonstrates creating a Dog object. It also lists the main ways to create objects in Java. The lab tasks involve creating classes for students, triangles, rectangles, and demonstrating the use of the this keyword.

Uploaded by

AHSAN AZIZ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Lab 06 - Classes, Objects & Constructors: Objective

The document discusses classes and objects in object-oriented programming. It defines a class as a blueprint for creating objects with common properties and methods. A class declaration includes modifiers, the class name, superclass, interfaces, and body. Constructors initialize new objects, fields provide state, and methods implement behavior. An object represents a real-world entity with state, behavior, and identity. The document provides an example Dog class with properties, constructor, methods, and demonstrates creating a Dog object. It also lists the main ways to create objects in Java. The lab tasks involve creating classes for students, triangles, rectangles, and demonstrating the use of the this keyword.

Uploaded by

AHSAN AZIZ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Department of Electrical Engineering Military College of Signals NUST

Lab 06 – CLASSES, OBJECTS & CONSTRUCTORS


Objective:
In this lab we will understand concepts of Classes and Objects in OOP.

Class
A class is a user defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type. In general, class declarations can
include these components, in order:

1. Modifiers: A class can be public or has default access.


2. class keyword: class keyword is used to create a class.
3. Class name: The name should begin with an initial letter (capitalized by convention).
4. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded
by the keyword implements. A class can implement more than one interface.
6. Body: The class body surrounded by braces, { }.

Constructors are used for initializing new objects. Fields are variables that provides the state of the class
and its objects, and methods are used to implement the behavior of the class and its objects.
There are various types of classes that are used in real time applications such as nested classes,
anonymous classes, lambda expressions.

Object
It is a basic unit of Object-Oriented Programming and represents the real life entities. A typical Java
program creates many objects, which as you know, interact by invoking methods. An object consists of :

1. State: It is represented by attributes of an object. It also reflects the properties of an object.


2. Behavior: It is represented by methods of an object. It also reflects the response of an object with
other objects.
3. Identity: It gives a unique name to an object and enables one object to interact with other objects.

Example
// Class Declaration

public class Dog


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

// Constructor Declaration of Class


Object Oriented Programming
Department of Electrical Engineering Military College of Signals NUST

public Dog(String name, String breed,


int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = 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;
}

public String toString()


{
return("Hi my name is "+ this.getName()+
".\nMy breed,age and color are " +
this.getBreed()+"," + this.getAge()+
","+ this.getColor());
}

public static void main(String[] args)


{
Dog tuffy = new Dog("tuffy","papillon", 5, "white");
System.out.println(tuffy.toString());
}
}

Object Oriented Programming


Department of Electrical Engineering Military College of Signals NUST

Ways to create object of a class

There are four ways to create objects in java.Strictly speaking there is only one way(by
using new keyword),and the rest internally use new keyword.
 Using new keyword: It is the most common and general way to create object in java. Example:
// creating object of class Test
Test t = new Test();

Lab Tasks:

Task 1:

Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Assign the value
of roll_no as '2' and that of name as "John" by creating an object of the class Student.

Task 2:

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating
a class named 'Triangle' without any parameter in its constructor.

Task 3:

Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by creating
a class named 'Rectangle' with a method named 'Area' which returns the area and length and breadth
passed as parameters to its constructor.

Task 4:

Write a program to print the area of a rectangle by creating a class named 'Area' taking the values of its
length and breadth as parameters of its constructor and having a method named 'returnArea' which
returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.

Lab Tasks:
Explain with the examples this keyword uses.

Object Oriented Programming

You might also like