Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Study Note on Java - Classes, Objects & Methods

Rating
-
Sold
-
Pages
6
Uploaded on
28-02-2022
Written in
2021/2022

Study Note on Java - Classes, Objects & Methods

Institution
Course

Content preview

Chapter 8: Classes, Objects and Methods
 Anything we wish to represent must be encapsulated in a class that defines the state
and the behaviour of the basic program components known as objects.
 Classes create objects and objects use methods to communicate between them.
 In Java, the data items are called fields and the functions are called methods.
8.1 Defining a Class
 The basic form of a class definition is:

Class classname [extends superclassname]
{
[variable declaration;]
[methods declaration;]
}
 Classname and superclassname are valid Java identifiers
 The keyword extends indicates that the properties of the superclassname class are
extended to the classname class. This is known as inheritance
8.2 Adding Variables
 Data is encapsulated in a class by placing data fields inside the body of the class
definition. These variables are called instance variables because they are created whenever
an object of the class is instantiated.
8.3 Adding Methods
 A class with only data fields and without methods that operate on that data has no life.
 The objects created by such a class cannot respond to any messages.
 We must therefore add methods that are necessary for manipulating the data contained
in the class.
 Methods are declared inside the body of the class but immediately after the declaration
of instance variables.
 The general form of a method declaration is:
Type methodname (parameter-list)
{
Method-body;
}
 Method declarations have four basic parts:
1. the name of the method (methodname)
2. the type of the value the method returns (type)
3. A list of parameters (parameter-list)
4. The body of the method
 The type specifies the type of value the method would return.
 Could be a simple data type such as int as well as any class type.
 It could even be void type if the method does not return any value.
 The methodname is a valid identifier.
 The parameter list is always enclosed in parenthesis.
 This list contains variable names and types of all the values we want to give to the
method as input.
 The variables in the list are separated by commas
 In the case where no input data are required, the declaration must retain the empty
parenthesis.
8.4 Creating Objects

,  An object is a block of memory that contains space to store all the instance variables.
 Creating an object is also referred to as instantiating an object.
 Objects are created using the new operator.
 The new operator creates an object of the specified class and returns a reference to that
object.
 The first statement declares a variable to hold the object reference and the second one
assigns the object reference to the variable.
8.5 Accessing Class Members
 All variables must be assigned values before they are used.
 To access the instance variables and the methods directly outside the class, we use the
concerned object and the dot operator.
Objectname. Variablename
Objectname.methodname (parameter-list)
o Objectname is the name of the object, variablename is the name of the instance
variable inside the object that we wish to access, methodname is the method that
we wish to call, and parameter-list is a comma separated list of “actual values” that
must match in type and number with the parameter list of the methodname
declared in the class.
8.6 Constructors
 Constructor enables an object to initialize itself when it is created.
 Constructors have the same name as the class itself
 They do not specify a return type, not even void. This is because they return the
instance of the class itself.
Eg:
Class Rectangle
{
int length;
int width;
Rectangle (int x, int y) // Constructor method
{
Length = x;
Width = y;
}
Int rectArea ()
{
Return (length * width);
}
}
8.7 Methods Overloading
 It is possible to create methods that have the same name, but different parameter lists
and different definitions. This is called method overloading.
 Is used when objects are required to perform similar tasks but using different input
parameters.
 When we call a method in an object, Java matches up the method name first and then
the number and type of parameters to decide which one of the definitions to execute. This
process is called polymorphism.
8.8 Static Members
 Static members can be defined as follows:

Connected book

Written for

Institution
Course

Document information

Uploaded on
February 28, 2022
Number of pages
6
Written in
2021/2022
Type
Class notes
Professor(s)
Mrs. prabitha
Contains
All classes

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sinithomas

Also available in package deal

Get to know the seller

Seller avatar
sinithomas Study Notes
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
4 year
Number of followers
0
Documents
10
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions