SlideShare una empresa de Scribd logo
1 de 20
UNIT - III
CLASSES, INTERFACES AND
PACKAGES
Class and object:
• Defining class
• Constructor
• Method overloading
• Static members
• Nesting of methods
• this keyword
• Command line argument
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
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, { }.
Syntax to declare a class:
access_modifier class<class_name>
{
data member;
data method;
}
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.
Classes and objects
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);
}
}
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];
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.
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
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();
}
}
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();
} }
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.
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.
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.
• Method1Method2…………..> methodn
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(); } }
“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.
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();
}}
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]);
}
}

Más contenido relacionado

Similar a UNIT - IIInew.pptx

class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxDrYogeshDeshmukh1
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMambikavenkatesh2
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 

Similar a UNIT - IIInew.pptx (20)

Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Class and object
Class and objectClass and object
Class and object
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 

Último

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Último (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

UNIT - IIInew.pptx

  • 1. UNIT - III CLASSES, INTERFACES AND PACKAGES
  • 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. • Method1Method2…………..> 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]); } }