Class in Java
order:
1. Modifiers : A class can be
public or has default access (Refer this for details).
2. Class name: The name should begin
with a initial letter (capitalized by convention).
3. Superclass(if any): The name of the
class’s parent (superclass), if any, preceded by the keyword extends. A class
can only extend (subclass) one parent.
4. Interfaces(if any): A comma-separated
list of interfaces implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
5. Body: The class
body surrounded by braces, { }.
Constructors are used for initializing new
objects. Fields are variables that provides the state of the class and its
objects, and methods are used to implement the behavior of the class and its
objects.
There are various types of
classes that are used in real time applications such as nested
classes, anonymous classes, lambda expressions.
A class is a group of objects which have
common properties. It is a template or blueprint from which objects are
created. It is a logical entity. It can't be physical.
A class in Java can contain:
- Fields
- Methods
- Constructors
- Blocks
- Nested class and interface
class in java programming |
class in java programming
Syntax:
class<class_name>
{
field;
methods;
}
· Using new keyword : It is the most common and
general way to create object in java.
Example:
//creating object of the classTest
Test t=new test();
· Using Class.forName(String className) method : There is a pre-defined
class in java.lang package with name Class. The forName(String className)
method returns the Class object associated with the class with the given string
name.We have to give the fully qualified name for a class. On calling new
Instance() method on this Class object returns new instance of the class with
the given string name.
Example:
//creating object of public class Test
//consider class test present in com.pI package
Test obj=(Test)Class.forname(“com.pi.Test”).newInstance();
No comments:
Post a Comment