SlideShare ist ein Scribd-Unternehmen logo
1 von 16
JAVA CLASSES, OBJECTS AND
CONSTRUCTORS
JAVA LECTURE
BY: LJRABOY
Object-Oriented Approach
• Class
– Instance variables – attributes of an object
– Methods – functions that manipulates objects
• Are subprograms
• Divide-and-conquer model
• One large programs – subdivide the program into smaller
subprograms
• Process needed in the program
• Methods are usually named using a action words such as
computeSum(), setValueOfX(), getValueOfY() and etc
– A user-defined method can be used after the declaration
of main() as a static method.
– Static method – does not need an object to access its
methods and instance variables.
Common example of methods
• main()
• System.out.println();
• JOptionPane.showMessageDialog();
• String str = JOptionPane.showInputDialog();
• Int y = Integer.parseInt(str);
The Class
• The class contains both the variables and its methods
• Class is represented by its own filename
• example of a declaration:
public class MyClass{
public static void main(String args *[])
{
}
}
The Class (cont)
• Is a general category of an object
• it contains the very common behavior of an
object.
• A class does not necessarily needs main()
because a class can be used in terms of using
objects. Together with its instance variables
and instance methods.
The Object
• Instantiate an object using a new reserved
word.
• So all the instance variables and instance
methods of the class can now be accessed by
an objects.
• This instance variables and methods can be
accessed using the dot “.” operator.
ObjectName.instance_Variable
ObjectName.instance_method()
Getting Started
• Creating a class
– Instance variables
– Instance methods
Example:
• Start with creating a new Java Basic
Application
• Name your class:
– Example: MyClassMain
– Select File, New  File
Select Java Class
Provide another filename for your
class
Two files for the project
Inside MyClassMain class
Inside MyOwnClass class
• Public  access modifier ,
can be accessed anywhere
in the package
• Reserved word class
• MyOwnClass  User-
defined class name
• public MyOwnClass(){
}  constructor
Constructor – used for
initialization
Modifying MyOwnClass
MyClassMain class
Format of using a method
public return_type methodname(parameters)
{
// body of the method
}
Where:
public is an access modifier
Return_type – any primitive type/class type
Methodname – user-defined method name
Parameters – optional, values to be passed to the method

Weitere ähnliche Inhalte

Was ist angesagt?

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Was ist angesagt? (20)

[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Java Methods
Java MethodsJava Methods
Java Methods
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Object and class
Object and classObject and class
Object and class
 
Oops in java
Oops in javaOops in java
Oops in java
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 

Andere mochten auch

Java class 5
Java class 5Java class 5
Java class 5
Edureka!
 

Andere mochten auch (13)

Java lec constructors
Java lec constructorsJava lec constructors
Java lec constructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
OCA JAVA - 1 Packages and Class Structure
 OCA JAVA - 1 Packages and Class Structure OCA JAVA - 1 Packages and Class Structure
OCA JAVA - 1 Packages and Class Structure
 
Java class 5
Java class 5Java class 5
Java class 5
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and destructor in c++
Constructor and destructor in c++Constructor and destructor in c++
Constructor and destructor in c++
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 

Ähnlich wie Java lec class, objects and constructors

New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
myrajendra
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
Allan Mangune
 

Ähnlich wie Java lec class, objects and constructors (20)

Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
VB.net&OOP.pptx
VB.net&OOP.pptxVB.net&OOP.pptx
VB.net&OOP.pptx
 
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
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
Unit 3
Unit 3Unit 3
Unit 3
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Java2
Java2Java2
Java2
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
 
Object-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & ProgrammingObject-oriented Analysis, Design & Programming
Object-oriented Analysis, Design & Programming
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
Java
JavaJava
Java
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
Constructors and Method Overloading
Constructors and Method OverloadingConstructors and Method Overloading
Constructors and Method Overloading
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Java lec class, objects and constructors

  • 1. JAVA CLASSES, OBJECTS AND CONSTRUCTORS JAVA LECTURE BY: LJRABOY
  • 2. Object-Oriented Approach • Class – Instance variables – attributes of an object – Methods – functions that manipulates objects • Are subprograms • Divide-and-conquer model • One large programs – subdivide the program into smaller subprograms • Process needed in the program • Methods are usually named using a action words such as computeSum(), setValueOfX(), getValueOfY() and etc – A user-defined method can be used after the declaration of main() as a static method. – Static method – does not need an object to access its methods and instance variables.
  • 3. Common example of methods • main() • System.out.println(); • JOptionPane.showMessageDialog(); • String str = JOptionPane.showInputDialog(); • Int y = Integer.parseInt(str);
  • 4. The Class • The class contains both the variables and its methods • Class is represented by its own filename • example of a declaration: public class MyClass{ public static void main(String args *[]) { } }
  • 5. The Class (cont) • Is a general category of an object • it contains the very common behavior of an object. • A class does not necessarily needs main() because a class can be used in terms of using objects. Together with its instance variables and instance methods.
  • 6. The Object • Instantiate an object using a new reserved word. • So all the instance variables and instance methods of the class can now be accessed by an objects. • This instance variables and methods can be accessed using the dot “.” operator. ObjectName.instance_Variable ObjectName.instance_method()
  • 7. Getting Started • Creating a class – Instance variables – Instance methods
  • 8. Example: • Start with creating a new Java Basic Application • Name your class: – Example: MyClassMain – Select File, New  File
  • 10. Provide another filename for your class
  • 11. Two files for the project
  • 13. Inside MyOwnClass class • Public  access modifier , can be accessed anywhere in the package • Reserved word class • MyOwnClass  User- defined class name • public MyOwnClass(){ }  constructor Constructor – used for initialization
  • 16. Format of using a method public return_type methodname(parameters) { // body of the method } Where: public is an access modifier Return_type – any primitive type/class type Methodname – user-defined method name Parameters – optional, values to be passed to the method