0% found this document useful (0 votes)
35 views42 pages

Lec 6 Classes

The document discusses classes, objects, and static members in Java. It defines a class as a blueprint for objects and explains that objects are instances of classes. It also describes the differences between objects and classes, and provides examples to illustrate class and object concepts in Java.

Uploaded by

Odur Morish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views42 pages

Lec 6 Classes

The document discusses classes, objects, and static members in Java. It defines a class as a blueprint for objects and explains that objects are instances of classes. It also describes the differences between objects and classes, and provides examples to illustrate class and object concepts in Java.

Uploaded by

Odur Morish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

CLASSES

LECTURE 6
Class Fundamentals

■ A class is an entity that determines how an object will behave and what the object will
contain. In other words, it is a blueprint or a set of instruction to build a specific type
of object. .
■ Java uses a class specification to construct objects. Objects are instances of a class.
■ Thus, a class is essentially a set of plans that specify how to build an object. A class is a
logical abstraction. It is not until an object of that class has been created that a physical
representation of that class exists in memory.
What is an Object?

■ An object is nothing but a self-contained component which consists of methods and


properties to make a particular type of data useful. Object determines the behavior of
the class. When you send a message to an object, you are asking the object to invoke or
execute one of its methods
What is the Difference Between Object & Class?

■ A class is a blueprint or prototype that defines the variables and the methods
(functions) common to all objects of a certain kind.
■ An object is a specimen of a class. Software objects are often used to model real-world
objects you find in everyday life.
Understand the concept of Java Classes and
Objects with an example.

■ Let's take an example of developing a pet


management system, specially meant for
dogs. You will need various information
about the dogs like different breeds of the
dogs, the age, size, etc.
■ You need to model real-life beings, i.e.,
dogs into software entities.
■ You can see the picture of three different breeds of dogs below.
■ Some of the differences you might have listed out maybe breed, age, size, color, etc. If
you think for a minute, these differences are also some common characteristics shared
by these dogs. These characteristics (breed, age, size, color) can form a data members
for your object.
Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. So these will be
the actions of our software objects
So far we have defined following things,
■ Class - Dogs
■ Data members or objects- size, age,
color, breed, etc.
■ Methods- eat, sleep, sit and run.
Now, for different values of data members (breed size, age, and color) in Java class, you
will get different dog objects.
Naming convention
■ Java naming convention is a rule to follow as you decide what to name your identifiers
such as class, package, variable, constant, method, etc.
■ But, it is not forced to follow. So, it is known as convention not rule. These conventions
are suggested by several Java communities such as Sun Microsystems and Netscape.
■ All the classes, interfaces, packages, methods and fields of Java programming language
are given according to the Java naming convention. If you fail to follow these
conventions, it may generate confusion or erroneous code.
The following are the key rules that must be followed by
every identifier

■ The name must not contain any white spaces.


■ The name should not start with special characters like & (ampersand), $ (dollar), _
(underscore).
class
■ It should start with the uppercase letter.
■ It should be a noun such as Color, Button, System, Thread, etc.
■ Use appropriate words, instead of acronyms.
Method
■ It should start with lowercase letter.
■ It should be a verb such as main(), print(), println().
■ If the name contains multiple words, start it with a lowercase letter followed by an uppercase
letter such as actionPerformed().
Variable
■ It should start with a lowercase letter such as id, name.
■ It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
■ If the name contains multiple words, start it with the lowercase letter followed by an uppercase
letter such as firstName, lastName.
■ Avoid using one-character variables such as x, y, z
Package
■ It should be a lowercase letter such as java, lang.
■ If the name contains multiple words, it should be separated by dots (.) such as
java.util, java.lang.
Constant
■ It should be in uppercase letters such as RED, YELLOW.
■ If the name contains multiple words, it should be separated by an underscore(_) such
as MAX_PRIORITY.
■ It may contain digits but not as the first letter.
Defining a Class
■ To illustrate classes we will develop a class that encapsulates information about
vehicles, such as cars, vans, and trucks. This class is called Vehicle, and it will store
three items of information about a vehicle: the number of passengers that it can carry, its
fuel capacity, and its average fuel consumption (in miles per gallon).
■ It defines three instance variables: passengers, fuelcap, and mpg. Notice that Vehicle
does not contain any methods. Thus, it is currently a data-only class
■ Remember that a class declaration is only a type description; it does not create an actual
object. Thus, the preceding code does not cause any objects of type Vehicle to come into
existence. To actually create a Vehicle object, you will use a statement like the following
Vehicle minivan = new Vehicle(); // create a Vehicle object called minivan

■ After this statement executes, minivan will be an instance of Vehicle. Each time you
create an instance of a class, you are creating an object that contains its own copy of
each instance variable defined by the class. Thus, every Vehicle object will contain its
own copies of the instance variables passengers, fuelcap, and mpg. To access these
variables, you will use the dot (.) operator. The dot operator links the name of an object
with the name of a member. The general form of the dot operator is shown here
object.member
■ Thus, the object is specified on the left, and the member is put on the right. For
example, to assign the fuelcap variable of minivan the value 16, use the following
statement:
minivan.fuelcap = 16;
■ In general, you can use the dot operator to access both instance variables and methods.
Here is a complete program that uses the Vehicle class
3 Ways to initialize object

There are 3 ways to initialize object in java.


■ By reference variable
■ By method
■ By constructor
Initialization through reference

■ Initializing an object means storing data into the object. Let's see a simple example
where we are going to initialize the object through a reference variable
■ We can also create multiple objects and store information in it through reference
variable.

See class Vechicle class


Initialization through method

See class student exam class

We can also have a method returning something declared like this


public double computePay() { return salary/52; }
Constructors
In the preceding examples, the instance variables of each Vehicle object had to be set
manually using a sequence of statements, such as
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
An approach like this would never be used in professionally written Java code. Aside from
being error prone (you might forget to set one of the fields), there is simply a better way to
accomplish this task: the constructor.
Constructors
■ A constructor initializes an object when it is created. It has the same name as its class and is
syntactically similar to a method.
■ However, constructors have no explicit return type. Typically, you will use a constructor to give
initial values to the instance variables defined by the class, or to perform any other startup
procedures required to create a fully formed object
■ All classes have constructors, whether you define one or not, because Java automatically
provides a default constructor that initializes all member variables to zero. However, once you
define your own constructor, the default constructor is no longer used. Here is a simple example
that uses a constructor
Initialization through a constructor

■ Java constructor: A constructor in Java is a method which is used to initialize objects.


■ Constructor method of a class has the same name as that of the class, they are called or
invoked when an object of a class is created and can't be called explicitly. Attributes of
an object may or may not be available while creating objects, if no attribute is available
then default constructor is called, some of the attributes may be known initially.
■ It is optional to write constructor method(s) in a class but due to their utility they are
used.
Difference between constructor and method

■ Constructor is used to initialize an object whereas method is used to exhibits


functionality of an object.
■ Constructors are invoked implicitly whereas methods are invoked explicitly.
■ Constructor does not return any value where the method may/may not return a value.
■ In case constructor is not present, a default constructor is provided by java compiler. In
the case of a method, no default method is provided.
■ Constructor should be of the same name as that of class. Method name should not be of
the same name as that of class.
static

■ In Java, a static member is a member of a class that isn’t associated with an instance of a
class. Instead, the member belongs to the class itself. As a result, you can access the
static member without first creating a class instance. The value of a static field is the
same across all instances of the class.
■ To create such a member, precede its declaration with the keyword static. When a
member is declared static, it can be accessed before any objects of its class are created,
and without reference to any object. You can declare both methods and variables to be
static
Static cont’d

■ The most common example of a static member is main( ). main( ) is declared as static
because it must be called by the operating system when your program begins (i.e.,
before any objects exist.). Outside the class, to use a static member, you need only
specify the name of its class followed by the dot operator.
■ No object needs to be created. The static keyword in Java is used for memory
management mainly (i.e it saves memory).
Summary

■ Java Class is an entity that determines how an object will behave and what the object
will contain
■ A Java object is a self-contained component which consists of methods and properties to
make certain type of data useful
■ A class system allows the program to define a new class (derived class) in terms of an
existing class (superclass) by using a technique like inheritance, overriding and
augmenting.
POLYMORPHISM
Polymorphism

■ Polymorphism in Java is a concept by which we can perform a single action in


different ways. ... So polymorphism means many forms.
■ There are two types of polymorphism in Java: compile-time polymorphism and
runtime polymorphism.
■ We can perform polymorphism in java by method overloading and method overriding.
Method Overloading in Java

■ If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
■ If we have to perform only one operation, having same name of the methods increases
the readability of the program.
■ Suppose you have to perform addition of the given numbers but there can be any
number of arguments, if you write the method such as a(int,int) for two parameters, and
b(int,int,int) for three parameters then it may be difficult for you as well as other
programmers to understand the behavior of the method because its name differs.
■ So, we perform method overloading to figure out the program quickly.
Advantage of method overloading
■ Method overloading increases the readability of the program.

Different ways to overload the method


There are two ways to overload the method in java
■ By changing number of arguments
■ By changing the data type
1) Method Overloading: changing no. of
arguments

■ n this example, we have created two methods, first add() method performs addition of
two numbers and second add method performs addition of three numbers.
■ In this example, we are creating static methods so that we don't need to create instance
for calling methods.
2) Method Overloading: changing data type of
arguments

■ In this example, we have created two methods that differs in data type. The first add
method receives two integer arguments and second add method receives two double
arguments.
THE END

You might also like