Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
nandiaditi2010
177 views
10 slides
Aug 12, 2024
Slide 1 of 10
1
2
3
4
5
6
7
8
9
10
About This Presentation
...
Size: 834.63 KB
Language: en
Added: Aug 12, 2024
Slides: 10 pages
Slide Content
1
Class in Java, Declaring a Class,
Declaring a Member in a Class
Prepared By:
Aditi Nandi Tokder
Assistant Professor
ECE Department
Techno Main Salt Lake
Content
1. Topic Name 1
2. Introduction to Java Classes 3
3. Declaring a Class 4
4. Class Syntax 5
5. Defining Class Member 6
6. Accessing Class Member 7
7. Encapsulation and Information Hiding 8
8. Conclusion 9
9. Thank You 10
2
Introduction to Java Classes
A class in Java is a set of objects which shares common characteristics/ behavior and common properties/ attributes. It is a user-defined
blueprint or prototype from which objects are created. For example, Student is a class while a particular student named Ravi is an object.
Properties of Java Classes
1.Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.
2.Class does not occupy memory.
3.Class is a group of variables of different data types and a group of methods.
4.A Class in Java can contain:
•Data member
•Method
•Constructor
•Nested Class
•Interface
4
➢A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we
may declare a class by using the class keyword. Class members and functions are declared simply within the class.
Classes are required for the creation of Java programs. The object-oriented paradigm (OOP) allows users to
describe real-world objects. Also, a class is sometimes known as a user-defined data type. The following
components make up a class declaration:
Modifiers
Class name
Keywords
The class body within curly brackets {}.
•
•
•
•
➢Syntax :-
public class ClassName {
// class body
}
•The ‘public’ keyword specifies that the class is accessible by any other class.
•The ‘class’ keyword is used to create a class.
•‘ClassName’ is the name of the class.
DeclaringaClass
Class Syntax
5
The syntax of
a class
declaration
includes the
class keyword,
the class
name, access
modifiers, and
the class body
containing
members.
Access
Modifiers
Control the
visibility of the
class and its
members
(e.g., public,
private).
Class Name
A unique
identifier that
follows Java
naming
conventions.
Class Body
Encloses the
class
members,
such as
variables and
methods.
Defining Class Member
6
Class members define the
attributes and actions
associated with objects
created from the class. They
can include instance
variables, methods, and
constructors. The system
allocates memory for a class
variable the first time it
encounters the class.
All instances of that
class share the same
copy of the class’s
class variables.
You can access class
variables either
through an instance or
through the class
itself.
Similarly, class
methods can be
invoked on the class
or through an instance
reference.
Note that when the
program changes the
value of classVariable
, its value changes for
all instance .
Instance Variables
Data fields that store
the state of an object.
Class Methods
Functions that define
the behavior of an
object.
Constructors
Special methods used
to initialize objects.
Accessing Class Member
7
Class members can be accessed using the dot (.) operator. The object's name followed by the dot
operator and the member name is used for accessing.
Instance Variables:-
Accessible within the class and through objects created from the class.
Syntax:-
ClassName objectName = new ClassName(parameters);
Example:-
Dog myDog = new Dog("Labrador", 3);
Explanation:-
‘mydog’ is an object of ‘Dog’ class.
Methods:-
Invoked using the object's name followed by the method name and arguments.
Accessing variables:-
myDog.breed;
myDog.age;
Calling methods:-
myDog.bark();
Encapsulation and Information Hiding
8
Encapsulation is a key
principle of object-
oriented programming
that combines data and
methods within a class. It
protects data from direct
access, enhancing code
maintainability and
security.
Data Hiding
Using the "private" access
modifier to restrict access
to instance variables from
outside the class.
Getters and Setters
Methods that provide
controlled access to
private data members.
Conclusion
9
In Java, a class is a blueprint for creating objects, encapsulating data and methods.
Declaring a class involves defining its structure, while declaring members includes
specifying variables and methods within the class. This fundamental concept
underpins Java's object-oriented programming, enabling organized, reusable, and
modular code.