Classes and Objects
Classes and Objects
All non-primitive data types are simply called objects which are created by instantiating a
class.
How to Declare Non-primitive type Data type?
• An object reference variable is declared just like we declare a
primitive variable.
• Here, DT9 is the name of a class, and “s” is the name of a reference variable.
No object has yet been created.
• Create an object of a class using new keyword (used for creating the
memory in java.).
• creates an object of a class DT9 and assigns it to the reference variable “s”.
• DT9() ➞ Constructor of the class.
• The constructor of a class is generally used to initialize an object.
Key Points
• The default value of any reference variable is null.
• Whenever a non-primitive data type is passed to a method,
we are actually passing an address of that object where
data is stored.
Memory Allocation of Object & Object
Reference Variable
• A variable whose type is a
class, does not actually hold SITE
an object
• It holds the memory location of
an object.
• Creating an object means
storing data in memory.
• So, s is an reference variable
which refers an object
Declaring reference variable for Box object
Assigning Object Reference Variables
• Object reference variables act differently than you
might expect when an assignment takes place
Parameter or Formal
arguments
Argument or actual
arguments
Exercise
• Write a java program to find the square root of the given number
using classes and objects
Constructors
• constructor initializes an object immediately upon creation
• It has the same name as the class in which it resides and is
syntactically similar to a method
• Once defined, the constructor is automatically called immediately
after the object is created, before the new operator completes
• The implicit return type of a class’ constructor is the class type itself
• Constructor’s job to initialize the internal state of an object
• the code creating an instance will have a fully initialized, usable object
immediately.
This is the reason
why the parentheses
are needed after the
class name
Rules for Writing Constructors in Java
• The name of the constructor must be the same as the name of its
class.
• A constructor must have no return type.
• It can not have not even void as its return type.
• We can use the access modifiers with a constructor to control its
access so that other classes can call the constructor.
• We can not declare a constructor as final, abstract, abstract and
synchronized.
Default constructor
The default constructor
automatically initializes
all instance variables to
zero.
Parameter less
constructor
Parameterized
constructor
Creates box
with specified
dimensions
Creates boxes
with different
dimensions
The this Keyword
• A method will need to refer to the object that invoked it.
• this can be used inside any method to refer to the current object.
• this is always a reference to the object on which the method was
invoked.
• this can be used anywhere a reference to an object of the current
class’ type is permitted.
When this?? To avoid Instance Variable Hiding
• Names of the Local variables, including formal parameters to
methods can overlap with the names of the class’ instance variables.
• when a local variable has the same name as an instance variable,
the local variable hides the instance variable
So use different names for Use this keyword to refer
local variables, parameters
and instance variables
OR directly to the object
invoking the method,
• The Java run time calls that method whenever it is about to recycle
an object of that class.
• Inside the finalize( ) method, specify those actions that must be
performed before an object is destroyed.
• The garbage collector runs periodically, checking for objects that are
no longer referenced by any running state or indirectly through other
referenced objects.
• Right before an asset is freed, the Java run time calls the finalize( )
method on the object.
Method Overloading
• Java allows to define two or more methods within the same class that
share the same name, as long as their parameter declarations are
different
• Method overloading is one of the ways that Java supports
polymorphism
• When an overloaded method is invoked, Java uses the type and/or
number of arguments to determine which version of the overloaded
method to actually call.
Overloaded methods must
differ in the type and/or
number of their
parameters.
Overloading Constructors
• Constructors can be
overloaded with
different arguments
Using Objects as Parameters to methods
• It is common in java to pass objects to methods
Test object
is passed
as
parameter
This will
create a copy
of a2 in a3
Passing objects to constructor
This will
create a copy
of a2 in a1[2]
Returning Objects
• method can return
any type of data,
including class
types
Recursion
• Java supports recursion.
• Recursion is the process of defining something in terms of itself.
• As it relates to Java programming, recursion is the attribute that
allows a method to call itself.
• A method that calls itself is said to be recursive
fact() calls itself until
• Here, classname is the name of the class in which the static method is
declared
Final keyword
• A field can be declared as final to prevents its contents from being
modified, making it, essentially, a constant.
• So it must be initialized when it is declared in any of these ways:
• Give it a value when it is declared.
• Assign it a value within a constructor
• Its common coding convention to choose all uppercase identifiers for
final fields
Accessing final field
Nested and Inner Classes
• Nested Class: A class within another class;
• The scope of a nested class is bounded by the scope of its enclosing class
• A nested class has access to the members, including private members, of the
class in which it is nested
• However, the enclosing class does not have access to the members of the nested class
• A nested class that is declared directly within its enclosing class scope is a
member of its enclosing class.
• It is also possible to declare a nested class that is local to a block.
Two types of nested classes:Static vs non-static
Static class Non-static class
• A static nested class is one that • An inner class is a non-static
has the static modifier applied. nested class.
• It must access the non-static • Has access to all of the variables
members of its enclosing class and methods of its outer class
through an object.
• That is, it cannot refer to non-
directly
static members of its enclosing • inner classes can be declared
class directly. within any block scope
• Because of this restriction, static
nested classes are seldom used.
An instance of Inner
can be created only
within the scope of
class Outer.
Replace
Inner class inside a block
Inheritance
Inheritance
• Inheritance allows the creation of hierarchical classifications.
• A class that is inherited is called a superclassParent class
• The class that does the inheriting is called a subclassChild class
• A specialized version of a superclass.
• It inherits all of the members defined by the superclass and adds its own,
unique elements.
Inheriting a class
• Inherit a class using extends keyword
• Any class that contains one or more abstract methods must also be
declared abstract as follows
• To disallow a method from being overridden, specify final as a modifier at the start of its
declaration
• Final methods provides performance enhancement
• The compiler is free to inline calls to them because it “knows” they will not be overridden by a subclass.
• When a small final method is called, often the Java compiler can copy the bytecode for the subroutine directly
inline with the compiled code of the calling method, thus eliminating the costly overhead associated with a
method call.
• Inlining is an option only with final methods.
• Normally, Java resolves calls to methods dynamically, at run timelate binding.
• However, since final methods cannot be overridden, a call to one can be resolved at compile
timeearly binding.
Using final to Prevent Overriding
Using final to Prevent Inheritance
• Precede the class declaration with final.
• Declaring a class as final implicitly declares all of its methods as final, too
Points to remember
• It is illegal to declare a class as both abstract and final since an
abstract class is incomplete by itself and relies upon its subclasses to
provide complete implementations.