Class and Objects
Class and Objects
Class and Objects
Class is the template or blueprint from which object is created and Object is the
instance of class.
Class can only be accessed from outside via its instance
Note- We rarely used int and float in class, generally we prefer string in java for
storing the value of variables.
i. java.lang.String
ii. java.lang.System
iii. java.lang.Exception
iv. java.lang.Object
v. java.lang.Class
vi. java.util.Date
vii. java.util.HashMap
viii. java.util.ArrayList
ix. java.util.Iterator
x. java.lang.Thread
}
Java class is generally consists of following elements as
1. Variable
They are declared within the body of the class. The general
syntax to declare a class variable is given below:
Example-
public class Student {
2. Methods-
A method in Java is a collection of a statement which
determines the behavior of a class object.
3. Constructor
4. Blocks
5. Nested classes
Class within other class called as Nested class in java.
1. A Java class must have the class keyword followed by the class
name.
2. The class name must start with a capital letter and if you are using
more than one word to define a class name, every first letter of the
words should be made capital. Example- StudentData
3. There should not be any spaces or special characters used in a
class name except the dollar symbol($) and underscore(_).
4. A Java class can only have public or default access specifier.
5. It can extend only one parent class. By default, all the classes
extend java.lang.Object directly or indirectly.
6. A class may optionally implement any number of interfaces
separated by commas.
7. The class’s members must be always declared within a set of curly
braces {}.
8. Class containing the main() method is known as the Main class as it
will act as the entry point to your program.
For Example, Mobile is an object. Its name is Samsung; color is black known as its
state. It is used for calling, texting message so its behavior.
When we create an object (instance) of class then space is reserved in heap
memory.
Output
Welcome to Java Programming..