Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Classes and Objects in Java

The document explains the concepts of Classes and Objects in Java, which are fundamental to Object-Oriented Programming. It details the structure of a class, including its components such as modifiers, constructors, fields, and methods, as well as the characteristics of an object, including state, behavior, and identity. Additionally, it provides a simple example of a Java program demonstrating object instantiation and usage of a class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Classes and Objects in Java

The document explains the concepts of Classes and Objects in Java, which are fundamental to Object-Oriented Programming. It details the structure of a class, including its components such as modifiers, constructors, fields, and methods, as well as the characteristics of an object, including state, behavior, and identity. Additionally, it provides a simple example of a Java program demonstrating object instantiation and usage of a class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Classes and Objects in Java

Classes and Objects are basic concepts of Object-Oriented Programming which revolve around the real-life
entities.

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 (Refer this for details).
2. class keyword: class keyword is used to create a class.
3. Class name: The name should begin with a 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.

Declaring Objects (Also called instantiating a class)


When an object of a class is created, the class is said to be instantiated. All the instances share the attributes
and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A
single class may have any number of instances.

Initializing an object
The new operator instantiates a class by allocating memory for a new object and returning a reference to that
memory. The new operator also invokes the class constructor.

INSTRUCTOR: MR. S. MAVUCHI


CONTACT: 0718585317
EMAIL: smavuchi@hit.ac.zw
A Simple class
Here is a complete program that uses the Box class:
/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}

// This class declares an object of type Box.


class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box(); // object instantiation
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

INSTRUCTOR: MR. S. MAVUCHI


CONTACT: 0718585317
EMAIL: smavuchi@hit.ac.zw

You might also like