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

Java Classes and Objects, Declaring Objects, Constructors: Dr. Kumud Tripathi

This document discusses Java classes, objects, and constructors. It defines an object as a bundle of variables and methods that represents a real-world entity with state and behavior. A class is a blueprint that defines the common variables and methods for all objects of a certain kind. Constructors are special methods used to initialize an object when it is created. They are called automatically whenever a new object is instantiated. The document outlines different types of constructors like default, parameterized, copy constructors and compares constructors to regular methods.

Uploaded by

SIDHANT BHAGAT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Classes and Objects, Declaring Objects, Constructors: Dr. Kumud Tripathi

This document discusses Java classes, objects, and constructors. It defines an object as a bundle of variables and methods that represents a real-world entity with state and behavior. A class is a blueprint that defines the common variables and methods for all objects of a certain kind. Constructors are special methods used to initialize an object when it is created. They are called automatically whenever a new object is instantiated. The document outlines different types of constructors like default, parameterized, copy constructors and compares constructors to regular methods.

Uploaded by

SIDHANT BHAGAT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Java Classes and Objects,

Declaring objects, Constructors

Dr. Kumud Tripathi


What is an Objects?

● Real world objects are things that have:


○ state
○ behavior
● Example: your dog:
● State – name, color, breed, sits?, barks?, wages tail?, runs?
● Behavior – sitting, barking, running
● A software object is a bundle of variables (state) and methods (operations).
What is an Class?

● A class is a blueprint that defines the variables and methods common to all objects of a certain
kind.
● Example: ‘your dog’ is a object of the class Dog.
● An object holds values for the variables defines in the class.
● An object is called an instance of the Class
Object Creation

● A variable is declared to refer to the objects of type/class String:

String s;

● The value of s is null; it does not yet refer to any object.


● A new String object is created in memory with initial “abc” value:

String s = new String(“abc”);

● Now s contains the address of this new object.


Object Destruction

● A program accumulates memory through its execution.


● Two mechanism to free memory that is no longer need by the program:

1) manual – done in C/C++

2) automatic – done in Java

● In Java, when an object is no longer accessible through any variable, it is eventually removed
from the memory by the garbage collector.
● Garbage collector is parts of the Java Run-Time Environment.
Constructors

● A constructor is a block of codes similar to the method.


● It is called when an instance of the class is created.
● A constructor initializes the instance variables of an object.
● Every time an object is created using the new() keyword, at least one constructor is called.
● It calls a default constructor if there is no constructor available in the class.
● Compiler will not create any constructor, if there is any user defined constructor available in
the class.
Rules for creating constructors

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, and final.
Types of constructors

1. Default Constructor
2. No-arg Constructor
3. Parameterized Constructor
Default Constructor

● A constructor is called "Default Constructor" when it doesn't have any parameter.


● Syntax of default constructor:

<class_name>()

—--------------------

● If there is no constructor in a class, compiler automatically creates a default constructor.


● It is used to provide the default values to the object like 0, null, etc., depending on the type.
Parameterized Constructor

● A constructor which has a specific number of parameters is called a parameterized constructor.


● It is used to provide different values to distinct objects.
Constructor Overloading

● It can also be overloaded like Java methods.


● It is a technique of having more than one constructor with different parameter lists.
● They are arranged in a way that each constructor performs a different task.
Copy Constructor

● Like C++, Java also supports the copy constructor.


● But in C++, it is created by default. While in Java we define copy constructor our own.
● It creates an object using another object of the same Java class. It returns a duplicate copy of
an existing object of the class.
Java Constructor Java Method

A constructor is used to initialize the state of an A method is used to expose the behavior of

object. an object.

A constructor must not have a return type. A method must have a return type.

The constructor is invoked implicitly. The method is invoked explicitly.

The Java compiler provides a default constructor if The method is not provided by the compiler

you don't have any constructor in a class. in any case.

The constructor name must be same as the class The method name may or may not be same

name. as the class name.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy