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

Introduction To Classes and OBJECTS

The document introduces classes and objects in Java. A class defines common properties and behaviors of objects through variables and methods. Objects are specific instances of a class and inherit its properties and behaviors. The document provides examples of defining classes with variables and methods, and creating objects from classes.

Uploaded by

Grace Name
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)
27 views

Introduction To Classes and OBJECTS

The document introduces classes and objects in Java. A class defines common properties and behaviors of objects through variables and methods. Objects are specific instances of a class and inherit its properties and behaviors. The document provides examples of defining classes with variables and methods, and creating objects from classes.

Uploaded by

Grace Name
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/ 12

INTRODUCTION TO CLASSES AND

OBJECTS
What is Class??

• A Class is a template/blueprint.
• A Class defines the form of an object
• An object is a specific instance of a
class
EXAMPLE OF CLASS AND OBJECT
Object Vios Object Accent
Length = 16 feet
class Car Length = 13 feet
Passenger Capacity = 4
Top Speed = 230 mph
Passenger Capacity = 4
Top Speed = 220 mph

Length Color = red Color = gray

Passenger Capacity
Object Brio Object Innova
Top Speed Length = 16 feet
Passenger Capacity = 5
Length = 17 feet
Passenger Capacity = 7
Color Top Speed = 250 mph
Color = red
Top Speed = 210 mph
Color = black
• A Class defines the template
• Objects are specific instance of the
template
• Objects are physical thing of the Class
Classes can also contain methods
• Methods can also be members of the Class
• Class methods operate on the Class variables
• Classes should be organized with logical
groupings of variables and methods
Methods - are used to divide and sort functionalities within a class
so that the code will be readable even if it’s long. (sometimes called functions)
Methods are declared within a class, and they are used to perform
certain actions:
class car {
Length
Instance Passenger Capacity
variables Top Speed
Color

void calculateTopSpeed() {
METHOD System.out.println(“This is the Top Speed”);
}
}
Methods - are used to divide and sort functionalities within a class
so that the code will be readable even if it’s long. (sometimes called functions)
Methods are declared within a class, and they are used to perform
certain actions:

CREATING METHODS

modifiers retuntype methodName() {


//statements
}
CREATING METHODS

modifiers retuntype methodName() {


//statements
}

CREATING METHODS

public static void printHelllo() {


System.out.println(“Hello”);
}
Defining a Class
• class ClassName {
• Declare Instance Variables
• Declare Method 1()
• Declare Method 2()
}
VARIABLE TYPES IN JAVA
A class can contain any of the following variable types.
• Instancevariables − Instance variables are variables within a class but outside any
method. These variables are initialized when the class is instantiated. Instance variables
can be accessed from inside any method, constructor or blocks of that particular class.
• Local variables − Variables defined inside methods, constructors or blocks are called
local variables. The variable will be declared and initialized within the method and the
variable will be destroyed when the method has completed.
• Classvariables − Class variables are variables declared within a class, outside any
method, with the static keyword.
DEFINING A CLASS AND
CREATING OBJECTS IN JAVA
CREATING OBJECTS
As mentioned previously, a class provides the blueprints for objects. So basically, an
object is created from a class. In Java, the new keyword is used to create new objects.
There are three steps when creating an object from a class −

• Declaration − A variable declaration with a variable name with an


object type.
• Instantiation − The 'new' keyword is used to create the object.
• Initialization − The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.

You might also like