2. Class and object:
• Defining class
• Constructor
• Method overloading
• Static members
• Nesting of methods
• this keyword
• Command line argument
3. Defining a class
• Class is a set of objects which shares common
characteristics/ behavior and common properties/
attributes.
• Class is not a real world entity. It is just a template or
blueprint or prototype from which objects are created.
• Class does not occupy memory.
• Class is a group of variables of different data types and
group of methods.
• A class in java can contain:
• data member
• method
• constructor
• nested class and
• interface
4. Defining a Class in Java
• Java provides a reserved keyword class to define a class. The
keyword must be followed by the class name. Inside the
class, we declare methods and variables.
• In general, class declaration includes the following in the
order as it appears:
• Modifiers: A class can be public or has default access.
• class keyword: The class keyword is used to create a class.
• Class name: The name must begin with an initial letter
(capitalized by convention).
• 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.
• 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.
• Body: The class body surrounded by braces, { }.
5. Syntax to declare a class:
access_modifier class<class_name>
{
data member;
data method;
}
6. Creating objects
It is a basic unit of Object-Oriented Programming and
represents real life entities
An object consists of :
State: It is represented by attributes of an object. It also reflects
the properties of an object.
Behavior: It is represented by methods of an object. It also
reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one
object to interact with other objects.
8. Accessing class members:
obj_name.var_name;
obj_name.method_name();
Example:
class Student
{
int id;//data member (also instance variable)
String name; //data member (also instance variable)
public static void main(String args[])
{
Student s1=new Student();
//creating an object of Student
System.out.println(s1.id);
System.out.println(s1.name);
}
}
9. Creating Array of objects
• Using arrays, more than one object for the
same class can be created.
Syntax:
class_name object[]=new class_name[size];
(or)
class_name [] object=new class_name[size];
10. Constructor
• In Java, a constructor is a block of codes similar
to the method. It is called when an instance of
the class is created. At the time of calling
constructor, memory for the object is allocated
in the memory.
• It is a special type of method which is used to
initialize the 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.
11. Rules for creating Java constructor
There are two rules defined for the constructor.
• Constructor name must be the same as its class
name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static,
final, and synchronized
There are two types of constructors in Java:
• Default constructor (no-arg constructor)
• Parameterized constructor
12. Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
<class_name>(){}
Example:
class Bike1{
//creating a default constructor
Bike1(){
System.out.println("Bike is created");
}
//main method
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
13. Java Parameterized Constructor
A constructor which has a specific number of parameters is called a parameterized
constructor.
The parameterized constructor is used to provide different values to distinct
objects.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
} }
14. Static members
• The members of the classes are
(a) Member variables or fields
(b) member functions or methods
• In Java, static members are those which
belongs to the class and can access these
members without instantiating the class.
• The static keyword can be used with methods,
fields, classes, blocks.
15. General form:
static datatype var1,var2…………………varn;
static returntype methodname(arguments)
{
------------
------------
}
Static variables belong to a class and is common for
all objects of the class. Therefore no separate memory
area for static members. Static variables behave like
global variables and can be accessed by all created
objects.
16. Nesting of methods
A method can be called by using only its
name by another method of the same class that
is called Nesting of Methods.
A method of a class can be called only by an
object of that class using the dot operator.
When a method in java calls another
method in the same class, it is called Nesting of
methods.
• Method1Method2…………..> methodn
17. class demo {
private int m, n;
demo(int x, int y)
{ m = x;
n = y; }
int largest()
{
if (m > n) return m;
else return n; }
void display()
{ int large=largest();
System.out.println("The Greatest Number is : "+large); } }
public class nested_method {
public static void main(String args[]) {
demo o =new demo(10,20);
o.display(); } }
18. “this” keyword in Java:
• In Java, this is a reference variable that refers to
the current object.
• “this” is an implicit pointer to every method in a
class
• “this” pointer contains the address of the object
which calls the method
• The this keyword refers to the current object in a
method or constructor.
• The most common use of the this keyword is to
eliminate the confusion between class attributes
and parameters with the same name.
19. class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
20. Command line argument
• The java command-line argument is an argument
i.e. passed at the time of running the java program.
• The arguments passed from the console can be
received in the java program and it can be used as
an input.
class CommandLineExample{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}