Lec 6 Classes
Lec 6 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?
■ 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.
■ 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
■ 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.
■ 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
■ 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.
■ 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