Classes
1.
Class representing a group of related members, grouped to do a specific action.
2.
Class provides a blue print and that consists of all the class member details.
3.
We can also define class is a category which consists of related propertied
members.
Technically, a class consists of data members ,member functions.
Ex:--In
the bellow Example animal represents a category and that consists
Of
all the same propertied members.
Animal
Cat Rat Dog
Cow
In
java a class syntax looks as bellow
Syntax:--
<Access_modifier>
class
<class_name>
{
-----------------
------------------
}
Ex:-
public
class Example
{
public
static void main(String [] args)
{
System.out.println(“Hello”);
}
}
What
are access modifiers in java?
An
access modifier determines the accessibility scope of the Java elements that
are being modified. If we not explicitly use an access modifier with a Java
element, the element implicitly has the default access modifier. The explicit
access modifiers may be used with a class and its members. They cannot be used
with the variables inside a method. The Java has three explicit access
modifiers viz. public, private and protected, and also has a default modifier,
which is used when you do not explicitly specify a modifier.
The following table shows the access to
members permitted by each modifier.
Access
Levels
|
||||
Modifier
|
Class
|
Package
|
Subclass
|
World
|
public
|
Y
|
Y
|
Y
|
Y
|
protected
|
Y
|
Y
|
Y
|
N
|
no
modifier
|
Y
|
Y
|
N
|
N
|
private
|
Y
|
N
|
N
|
N
|
1.
Private:-- Its scope
is within that class only.
2.
Protected:--
Provides the accessibility to other classes also like subclasses.
3.
Public:-- Provides
accessibility to anybody .
4. default/package:-- Provides accessibility to packaged
members.
Note:--If
we don’t mention any accessmodifier, by
default it is package (default)only.
“class”
is a keyword to represent a class. Classname is a userdefined name .
Note:
class can also used as userdefined datatype
Naming
convention while using classnames:--
ü classname
must start with alphabet.
ü first
letter usually capital.
ü do
not include spaces in between classnames.
ü we
can also use digits in className
In
general, class declarations can include these components, in order:
1.
Modifiers such as public, private, and a number of others that you will encounter
later.
2.
The class name, with the initial letter capitalized by convention.
3.
The name of the class's parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
4.
The class body, surrounded by braces, {}.
public class Bicycle {
private int gear;
private int speed;
public int getGear() {
return gear;
}
public void setGear(int newValue) {
gear = newValue;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
What is an Object ?
v Object is an instance of a class.v Object represents a reference of a class.v We can access the members of a class With the help of object only.v In oops lang object is the main role.Syntax:---<classname> <objectname> = new <constructorname>(param-list-if-any);Ex:-- Example e = new Example ();1. Example ---represents a class name.2. e---represents a class reference.3. Class is a user-defined data type so “ e ” is reference for “Example” class.4. “new” is a keyword which is used to allocate memory dynamically for an object.5. Example () ---is a constructor and which is used to construct an object.Every Object consists of three properties:--1. Identity 2.State 3.BehaviourIdentity:--For each object, it generates a 32-bit random number in hexadecimal values Which is also known as hashcode of an object. Each object has its own hashcode.we can get it with hashCode() method.Ex:--Class Ex{Ex e = new Ex();Ex e1 = new Ex();s.o.p(e.hashCode());s.o.p(e);s.o.p(e1.hashCode());s.o.p(e());}}State:--Every object contains its own state, State of an object is represented with the value which is stored in that object.We have two instances:--Class level:--always class level variable intialises with zero,can access anywhere.Method level:--can initialize with something.Behaviour : --The object behavior depends upon the methods which is executing with this object .
No comments:
Post a Comment