SlideShare a Scribd company logo
1 of 24
Classes and Objects


Pre-assessment Questions
    1.   Predict the output of the expression, 16 % 3?
         a. 5
         b. 0
         c. 1
         d. 4
    2.   What is the default value of the float data type?
         a. 0.0
         b. 0
         c. 1.0
         d. 1




 ©NIIT                    Java Fundamentals                  Lesson 2B / Slide 1 of 24
Classes and Objects


Pre-assessment Questions (Contd.)
    3.   Consider the statements:
         Statement A: The name of a variable can begin with a digit.
         Statement B: The name of a variable can contain white spaces.

         Identify the correct option.
         a. Statement A is true and statement B is false.
         b. Statement A is false and statement B is true.
         c. Both, statements, A and B, are true.
         d. Both, statements, A and B, are false.




 ©NIIT                    Java Fundamentals                 Lesson 2B / Slide 2 of 24
Classes and Objects


Pre-assessment Questions (Contd.)
    4.   _______variables are the local variables that are accessed by the function in
         which the variables are declared.
         a. Static
         b. Automatic
         c. Instance
         d. Class.



    8.   _______ literals are enclosed in single quotes.
         a. String
         b. Character
         c. Boolean
         d. Integer



 ©NIIT                    Java Fundamentals                  Lesson 2B / Slide 3 of 24
Classes and Objects


Solutions to Pre-assessment
  Questions
    1.   c.   1
    2.   a.   0.0
    3.   d.   Both, statements A and B, are false
    4.   a.   Static
    5.   b.    Character




 ©NIIT                      Java Fundamentals       Lesson 2B / Slide 4 of 24
Classes and Objects


Objectives
    •    In this lesson, you will learn about:
           • Structure of a Java program
           • Access specifiers and modifiers
           • Creating a Java application




 ©NIIT                       Java Fundamentals   Lesson 2B / Slide 5 of 24
Classes and Objects


Structure of Java Application
    •    Creating Classes and Objects
           • The components of a class consists of
                • Data members (Attributes)
                • Methods




 ©NIIT                     Java Fundamentals         Lesson 2B / Slide 6 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Creating Classes in Java
           • The statements written in a Java class must end with a semicolon, ;.
          class ClassName
          {
          //Declaration of data members
          //Declaration of methods
          }


    •    Double slash, //, are comment entries. Comments are ignored by compiler.




 ©NIIT                      Java Fundamentals                 Lesson 2B / Slide 7 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Creating Objects of Classes
           • An object is an instance of a class having a unique identity.
           • To create an object, you need to perform the following steps:
                • Declaration
                • Instantiation or creation




 ©NIIT                      Java Fundamentals                 Lesson 2B / Slide 8 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Accessing Data Members of a Class
           • Assign values to data members of the object before using them.
           • You can access the data members of a class outside the class by
             specifying the object name followed by the dot operator and the data
             member name.
                e1.employeeName="John";
                e2.emmployeeName="Andy";

               e1.employeeID=1;
               e2.employeeID=2;
               e1.employeeDesignation = “Manager”;
               e2.employeeDesignation = “Director”;




 ©NIIT                     Java Fundamentals                 Lesson 2B / Slide 9 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Adding Methods to a Class
         • Accessing data members directly overrules the concept of
              encapsulation.
         • Advantages of using methods in a Java program:
              • Reusability
              • Reducing Complexity
              • Data Hiding




 ©NIIT                   Java Fundamentals             Lesson 2B / Slide 10 of 24
Classes and Objects


Structure of Java Application (Contd.)
         •   The syntax to define a method:
             void methodName()
             {
                  // Method body.
             }




 ©NIIT                  Java Fundamentals     Lesson 2B / Slide 11 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Declaring the main() Method:
         • The syntax to declare the main()method:
              public static void main(String args[])
              {
              // Code for main() method
              }
         • public: method can be accessed from any object in a Java program.
         • static : associates the method with its class.
         • void: signifies that the main() method returns no value.
    •    The main() method can be declared in any class but the name of the file and
         the class name in which the main() method is declared should be the same.
    •    The main() method accepts a single argument in the form of an array of
         elements of type String.



 ©NIIT                   Java Fundamentals                Lesson 2B / Slide 12 of 24
Classes and Objects


Structure of Java Application (Contd.)

    •    Following code snippet creates objects for four employees of an
         organization:

          Employee   e1   =new   Employee();
          Employee   e2   =new   Employee();
          Employee   e3   =new   Employee();
          Employee   e4   =new   Employee();




 ©NIIT                     Java Fundamentals              Lesson 2B / Slide 13 of 24
Classes and Objects


Structure of Java Application (Contd.)
    •    Defining Constructors
         • A constructor is a method with the same name as the class name.
         • A constructor of a class is automatically invoked every time an instance
              of a class is created.
    •    Characteristics of Constructors
         • There is no return type for a constructor.
         •     A constructor returns the instance of the class instead of a value.
         • A constructor is used to assign values to the data members of each
              object created from a class.




 ©NIIT                   Java Fundamentals                Lesson 2B / Slide 14 of 24
Classes and Objects


Access Specifiers and Modifiers
    •    Access Specifiers
         • public
         • private
         • protected
         • friendly
    •    The public access specifier
         • Class members with public specifier can be accessed anywhere in the
              same class, package in which the class is created, or a package other
              than the one in which the class is declared.
         • The public keyword is used to declare a member as public.
              public <data type> <variable name>;




 ©NIIT                    Java Fundamentals                Lesson 2B / Slide 15 of 24
Classes and Objects


Access Specifiers and Modifiers
    •    The private access specifier
         • A data member of a class declared private is accessible at the class
              level only in which it is defined.
         • The private keyword is used to declare a member as private.
              private float <variableName>; // Private data member of float
                                                  //type
              private methodName();                // Private method
    •    The protected access specifier
         • The variables and methods are accessible only to the subclasses of the
              class in which they are declared.
         • The protected keyword is used to declare a member as protected.
              protected <data type> <name of the variable>;
    •    The friendly or package access specifier
         • If you do not specify any access specifier, the scope of data members
              and methods is friendly.

 ©NIIT                   Java Fundamentals              Lesson 2B / Slide 16 of 24
Classes and Objects


Access Specifiers and Modifiers(Contd.)
    •    Types of Permitted Modifiers
         • Modifiers determine or define how the data members and methods are
             used in other classes and objects.
             • static
             • final
             • abstract
             • native
             • synchronized




 ©NIIT                  Java Fundamentals             Lesson 2B / Slide 17 of 24
Classes and Objects

Access Specifiers and Modifiers(Contd.)
         •   static
             • Used to define class variables and methods that belong to a
                   class and not to any particular instance of the class.
             • Associates the data members with a class and not the objects of
                   the class.
         •   final
             • Indicates that the data member cannot be modified.
             • Does not allow the class to be inherited.
             • A final method cannot be modified in the subclass.
             • All the methods and data members in a final class are implicitly
                   final.
         •   abstract
             • Used to declare classes that define common properties and
                   behavior of other classes.
             • An abstract class is used as a base class to derive specific
                   classes of the same type.
 ©NIIT             Java Fundamentals               Lesson 2B / Slide 18 of 24
Classes and Objects

Access Specifiers and Modifiers(Contd.)
         •   native
             • Used only with methods.
             • Inform the compiler that the method has been coded in a
                  programming language other than Java, such as C or C++ .
             • The native keyword with a method indicates that the method
                  lies outside the Java Runtime Environment (JRE).
             public native void nativeMethod(var1, var2, . . .) ;
         •   synchronized
             • controls the access to a block of code in a multithreaded
                  programming environment.
             • Java supports multithreaded programming and each thread
                  defines a separate path of execution.




 ©NIIT             Java Fundamentals              Lesson 2B / Slide 19 of 24
Classes and Objects


Compiling an Application
    •    Compiling an Application
         • Save the code with a file name that is exactly the same as that of the
            class name, with a .java extension.
         • The steps to compile the Java file:
            • Open the command window.
            • Set the PATH variable to the bin directory of the installation
                  directory by using the following command at the command
                  prompt:
            • PATH=C:j2sdk1.4.1_02bin




 ©NIIT                   Java Fundamentals                Lesson 2B / Slide 20 of 24
Classes and Objects

Compiling an Application
         •   Change to the directory where you have saved the .java file. You can
             also give the complete path of the .java file while compiling it .
         •   Compile the application
         •   Execute the Bytecode.




 ©NIIT                   Java Fundamentals                Lesson 2B / Slide 21 of 24
Classes and Objects


Summary
    In this lesson, you learned:


    •    A class includes data members and methods. Methods can include
         expressions that evaluate to a value.
    •    Creating a class includes declaration of class members. Data members of
         same type can be declared in a single line.
    •    You can declare a class without data variables and methods.
    •    A class with only data members is of no practical use.
    •    You can interact directly with data members inside the class only.
    •    Object creation includes declaration of class object variable and assigning
         memory to object variable.
    •    You can access data members outside the class through class objects.
    •    A constructor is invoked on every creation of an object of a class type. The
         constructor can be used to initialize data members of newly created object.


 ©NIIT                    Java Fundamentals                 Lesson 2B / Slide 22 of 24
Classes and Objects


Summary(Contd.)
    •    There are two types of constructors the default constructor and the
         overloaded constructor.
    •    A constructor returns no value and has the same name as the class itself.
    •    A Java application requires a main() method to start execution.
    •    The Java interpreter first of all looks for the main() method. The class name
         and the file name in which the main() method is declared are same.
    •    You can use access specifiers to restrict the access to class members by
         other classes.
    •    There are private, public, and protected access specifiers. Private access
         specifier gives the most restricted access among all the access specifiers.




 ©NIIT                    Java Fundamentals                 Lesson 2B / Slide 23 of 24
Classes and Objects


Summary(Contd.)
    •    Class members with public specifier can be accessed anywhere in the same
         class, same package, or a different package.
    •    A modifier determines the use of the data members and methods in other
         classes and objects.
    •    The various modifiers available in Java are static, final, abstract, native, and
         synchronized .
    •    You must set the PATH variable of the operating system to the bin directory
         of the installation directory.
    •    You need to use the javac fileName.java command to compile the
         fileName.java file.
    •    You need to use the java fileName to execute the fileName.class file.




 ©NIIT                     Java Fundamentals                  Lesson 2B / Slide 24 of 24

More Related Content

What's hot

Java session01
Java session01Java session01
Java session01Niit Care
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questionsSynergisticMedia
 
3 f6 8_databases
3 f6 8_databases3 f6 8_databases
3 f6 8_databasesop205
 
10265 developing data access solutions with microsoft visual studio 2010
10265 developing data access solutions with microsoft visual studio 201010265 developing data access solutions with microsoft visual studio 2010
10265 developing data access solutions with microsoft visual studio 2010bestip
 
Chapter 2 slides
Chapter 2 slidesChapter 2 slides
Chapter 2 slideslara_ays
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-onhomeworkping7
 
3 f6 9_distributed_systems
3 f6 9_distributed_systems3 f6 9_distributed_systems
3 f6 9_distributed_systemsop205
 
Dot Net Interview Questions - Part 1
Dot Net Interview Questions - Part 1Dot Net Interview Questions - Part 1
Dot Net Interview Questions - Part 1ReKruiTIn.com
 
Innovation In Standards Publishing
Innovation In Standards PublishingInnovation In Standards Publishing
Innovation In Standards PublishingJonathan Griffin
 
Fragility in evolving software
Fragility in evolving softwareFragility in evolving software
Fragility in evolving softwarekim.mens
 
Deep neural networks for Youtube recommendations
Deep neural networks for Youtube recommendationsDeep neural networks for Youtube recommendations
Deep neural networks for Youtube recommendationsAryan Khandal
 

What's hot (20)

Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Java session01
Java session01Java session01
Java session01
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
 
Brouchure
BrouchureBrouchure
Brouchure
 
3 f6 8_databases
3 f6 8_databases3 f6 8_databases
3 f6 8_databases
 
10265 developing data access solutions with microsoft visual studio 2010
10265 developing data access solutions with microsoft visual studio 201010265 developing data access solutions with microsoft visual studio 2010
10265 developing data access solutions with microsoft visual studio 2010
 
Dtacs
DtacsDtacs
Dtacs
 
Chapter 2 slides
Chapter 2 slidesChapter 2 slides
Chapter 2 slides
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
 
3 f6 9_distributed_systems
3 f6 9_distributed_systems3 f6 9_distributed_systems
3 f6 9_distributed_systems
 
Dot Net Interview Questions - Part 1
Dot Net Interview Questions - Part 1Dot Net Interview Questions - Part 1
Dot Net Interview Questions - Part 1
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 
Dacj 1-1 c
Dacj 1-1 cDacj 1-1 c
Dacj 1-1 c
 
Innovation In Standards Publishing
Innovation In Standards PublishingInnovation In Standards Publishing
Innovation In Standards Publishing
 
7494611
74946117494611
7494611
 
Fragility in evolving software
Fragility in evolving softwareFragility in evolving software
Fragility in evolving software
 
Deep neural networks for Youtube recommendations
Deep neural networks for Youtube recommendationsDeep neural networks for Youtube recommendations
Deep neural networks for Youtube recommendations
 
Introducing MDSD
Introducing MDSDIntroducing MDSD
Introducing MDSD
 

Viewers also liked

Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11Niit Care
 
Ado.net session10
Ado.net session10Ado.net session10
Ado.net session10Niit Care
 
Ado.net session05
Ado.net session05Ado.net session05
Ado.net session05Niit Care
 
Technology Based learning at NIIT University
Technology Based learning at NIIT University Technology Based learning at NIIT University
Technology Based learning at NIIT University I Love Science
 
04 asp.net session05
04 asp.net session0504 asp.net session05
04 asp.net session05Niit Care
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02Niit Care
 
08 ds and algorithm session_11
08 ds and algorithm session_1108 ds and algorithm session_11
08 ds and algorithm session_11Niit Care
 
03 ds and algorithm session_04
03 ds and algorithm session_0403 ds and algorithm session_04
03 ds and algorithm session_04Niit Care
 
01 ds and algorithm session_01
01 ds and algorithm session_0101 ds and algorithm session_01
01 ds and algorithm session_01Niit Care
 

Viewers also liked (12)

Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
 
Ado.net session10
Ado.net session10Ado.net session10
Ado.net session10
 
Ado.net session05
Ado.net session05Ado.net session05
Ado.net session05
 
Dacj 1-2 a
Dacj 1-2 aDacj 1-2 a
Dacj 1-2 a
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Technology Based learning at NIIT University
Technology Based learning at NIIT University Technology Based learning at NIIT University
Technology Based learning at NIIT University
 
04 asp.net session05
04 asp.net session0504 asp.net session05
04 asp.net session05
 
Ds 1
Ds 1Ds 1
Ds 1
 
02 ds and algorithm session_02
02 ds and algorithm session_0202 ds and algorithm session_02
02 ds and algorithm session_02
 
08 ds and algorithm session_11
08 ds and algorithm session_1108 ds and algorithm session_11
08 ds and algorithm session_11
 
03 ds and algorithm session_04
03 ds and algorithm session_0403 ds and algorithm session_04
03 ds and algorithm session_04
 
01 ds and algorithm session_01
01 ds and algorithm session_0101 ds and algorithm session_01
01 ds and algorithm session_01
 

Similar to Dacj 1-2 b

Similar to Dacj 1-2 b (20)

chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
9 cm604.14
9 cm604.149 cm604.14
9 cm604.14
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Introduction to OOP with java
Introduction to OOP with javaIntroduction to OOP with java
Introduction to OOP with java
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Chap01
Chap01Chap01
Chap01
 
DAY_1.4.pptx
DAY_1.4.pptxDAY_1.4.pptx
DAY_1.4.pptx
 
Java session02
Java session02Java session02
Java session02
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 
Inheritance
Inheritance Inheritance
Inheritance
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Ppt chapter05
Ppt chapter05Ppt chapter05
Ppt chapter05
 
BIT211_3.pdf
BIT211_3.pdfBIT211_3.pdf
BIT211_3.pdf
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
 
Vb.net session 04
Vb.net session 04Vb.net session 04
Vb.net session 04
 
Core Java for Selenium
Core Java for SeleniumCore Java for Selenium
Core Java for Selenium
 
Constructors and Method Overloading
Constructors and Method OverloadingConstructors and Method Overloading
Constructors and Method Overloading
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 

More from Niit Care

More from Niit Care (16)

Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 
Dacj 1-3 a
Dacj 1-3 aDacj 1-3 a
Dacj 1-3 a
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
Dacj 1-1 a
Dacj 1-1 aDacj 1-1 a
Dacj 1-1 a
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
 
Dacj 2-2 a
Dacj 2-2 aDacj 2-2 a
Dacj 2-2 a
 
Dacj 2-1 c
Dacj 2-1 cDacj 2-1 c
Dacj 2-1 c
 
Dacj 2-1 b
Dacj 2-1 bDacj 2-1 b
Dacj 2-1 b
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Dacj 1-2 b

  • 1. Classes and Objects Pre-assessment Questions 1. Predict the output of the expression, 16 % 3? a. 5 b. 0 c. 1 d. 4 2. What is the default value of the float data type? a. 0.0 b. 0 c. 1.0 d. 1 ©NIIT Java Fundamentals Lesson 2B / Slide 1 of 24
  • 2. Classes and Objects Pre-assessment Questions (Contd.) 3. Consider the statements: Statement A: The name of a variable can begin with a digit. Statement B: The name of a variable can contain white spaces. Identify the correct option. a. Statement A is true and statement B is false. b. Statement A is false and statement B is true. c. Both, statements, A and B, are true. d. Both, statements, A and B, are false. ©NIIT Java Fundamentals Lesson 2B / Slide 2 of 24
  • 3. Classes and Objects Pre-assessment Questions (Contd.) 4. _______variables are the local variables that are accessed by the function in which the variables are declared. a. Static b. Automatic c. Instance d. Class. 8. _______ literals are enclosed in single quotes. a. String b. Character c. Boolean d. Integer ©NIIT Java Fundamentals Lesson 2B / Slide 3 of 24
  • 4. Classes and Objects Solutions to Pre-assessment Questions 1. c. 1 2. a. 0.0 3. d. Both, statements A and B, are false 4. a. Static 5. b. Character ©NIIT Java Fundamentals Lesson 2B / Slide 4 of 24
  • 5. Classes and Objects Objectives • In this lesson, you will learn about: • Structure of a Java program • Access specifiers and modifiers • Creating a Java application ©NIIT Java Fundamentals Lesson 2B / Slide 5 of 24
  • 6. Classes and Objects Structure of Java Application • Creating Classes and Objects • The components of a class consists of • Data members (Attributes) • Methods ©NIIT Java Fundamentals Lesson 2B / Slide 6 of 24
  • 7. Classes and Objects Structure of Java Application (Contd.) • Creating Classes in Java • The statements written in a Java class must end with a semicolon, ;. class ClassName { //Declaration of data members //Declaration of methods } • Double slash, //, are comment entries. Comments are ignored by compiler. ©NIIT Java Fundamentals Lesson 2B / Slide 7 of 24
  • 8. Classes and Objects Structure of Java Application (Contd.) • Creating Objects of Classes • An object is an instance of a class having a unique identity. • To create an object, you need to perform the following steps: • Declaration • Instantiation or creation ©NIIT Java Fundamentals Lesson 2B / Slide 8 of 24
  • 9. Classes and Objects Structure of Java Application (Contd.) • Accessing Data Members of a Class • Assign values to data members of the object before using them. • You can access the data members of a class outside the class by specifying the object name followed by the dot operator and the data member name. e1.employeeName="John"; e2.emmployeeName="Andy"; e1.employeeID=1; e2.employeeID=2; e1.employeeDesignation = “Manager”; e2.employeeDesignation = “Director”; ©NIIT Java Fundamentals Lesson 2B / Slide 9 of 24
  • 10. Classes and Objects Structure of Java Application (Contd.) • Adding Methods to a Class • Accessing data members directly overrules the concept of encapsulation. • Advantages of using methods in a Java program: • Reusability • Reducing Complexity • Data Hiding ©NIIT Java Fundamentals Lesson 2B / Slide 10 of 24
  • 11. Classes and Objects Structure of Java Application (Contd.) • The syntax to define a method: void methodName() { // Method body. } ©NIIT Java Fundamentals Lesson 2B / Slide 11 of 24
  • 12. Classes and Objects Structure of Java Application (Contd.) • Declaring the main() Method: • The syntax to declare the main()method: public static void main(String args[]) { // Code for main() method } • public: method can be accessed from any object in a Java program. • static : associates the method with its class. • void: signifies that the main() method returns no value. • The main() method can be declared in any class but the name of the file and the class name in which the main() method is declared should be the same. • The main() method accepts a single argument in the form of an array of elements of type String. ©NIIT Java Fundamentals Lesson 2B / Slide 12 of 24
  • 13. Classes and Objects Structure of Java Application (Contd.) • Following code snippet creates objects for four employees of an organization: Employee e1 =new Employee(); Employee e2 =new Employee(); Employee e3 =new Employee(); Employee e4 =new Employee(); ©NIIT Java Fundamentals Lesson 2B / Slide 13 of 24
  • 14. Classes and Objects Structure of Java Application (Contd.) • Defining Constructors • A constructor is a method with the same name as the class name. • A constructor of a class is automatically invoked every time an instance of a class is created. • Characteristics of Constructors • There is no return type for a constructor. • A constructor returns the instance of the class instead of a value. • A constructor is used to assign values to the data members of each object created from a class. ©NIIT Java Fundamentals Lesson 2B / Slide 14 of 24
  • 15. Classes and Objects Access Specifiers and Modifiers • Access Specifiers • public • private • protected • friendly • The public access specifier • Class members with public specifier can be accessed anywhere in the same class, package in which the class is created, or a package other than the one in which the class is declared. • The public keyword is used to declare a member as public. public <data type> <variable name>; ©NIIT Java Fundamentals Lesson 2B / Slide 15 of 24
  • 16. Classes and Objects Access Specifiers and Modifiers • The private access specifier • A data member of a class declared private is accessible at the class level only in which it is defined. • The private keyword is used to declare a member as private. private float <variableName>; // Private data member of float //type private methodName(); // Private method • The protected access specifier • The variables and methods are accessible only to the subclasses of the class in which they are declared. • The protected keyword is used to declare a member as protected. protected <data type> <name of the variable>; • The friendly or package access specifier • If you do not specify any access specifier, the scope of data members and methods is friendly. ©NIIT Java Fundamentals Lesson 2B / Slide 16 of 24
  • 17. Classes and Objects Access Specifiers and Modifiers(Contd.) • Types of Permitted Modifiers • Modifiers determine or define how the data members and methods are used in other classes and objects. • static • final • abstract • native • synchronized ©NIIT Java Fundamentals Lesson 2B / Slide 17 of 24
  • 18. Classes and Objects Access Specifiers and Modifiers(Contd.) • static • Used to define class variables and methods that belong to a class and not to any particular instance of the class. • Associates the data members with a class and not the objects of the class. • final • Indicates that the data member cannot be modified. • Does not allow the class to be inherited. • A final method cannot be modified in the subclass. • All the methods and data members in a final class are implicitly final. • abstract • Used to declare classes that define common properties and behavior of other classes. • An abstract class is used as a base class to derive specific classes of the same type. ©NIIT Java Fundamentals Lesson 2B / Slide 18 of 24
  • 19. Classes and Objects Access Specifiers and Modifiers(Contd.) • native • Used only with methods. • Inform the compiler that the method has been coded in a programming language other than Java, such as C or C++ . • The native keyword with a method indicates that the method lies outside the Java Runtime Environment (JRE). public native void nativeMethod(var1, var2, . . .) ; • synchronized • controls the access to a block of code in a multithreaded programming environment. • Java supports multithreaded programming and each thread defines a separate path of execution. ©NIIT Java Fundamentals Lesson 2B / Slide 19 of 24
  • 20. Classes and Objects Compiling an Application • Compiling an Application • Save the code with a file name that is exactly the same as that of the class name, with a .java extension. • The steps to compile the Java file: • Open the command window. • Set the PATH variable to the bin directory of the installation directory by using the following command at the command prompt: • PATH=C:j2sdk1.4.1_02bin ©NIIT Java Fundamentals Lesson 2B / Slide 20 of 24
  • 21. Classes and Objects Compiling an Application • Change to the directory where you have saved the .java file. You can also give the complete path of the .java file while compiling it . • Compile the application • Execute the Bytecode. ©NIIT Java Fundamentals Lesson 2B / Slide 21 of 24
  • 22. Classes and Objects Summary In this lesson, you learned: • A class includes data members and methods. Methods can include expressions that evaluate to a value. • Creating a class includes declaration of class members. Data members of same type can be declared in a single line. • You can declare a class without data variables and methods. • A class with only data members is of no practical use. • You can interact directly with data members inside the class only. • Object creation includes declaration of class object variable and assigning memory to object variable. • You can access data members outside the class through class objects. • A constructor is invoked on every creation of an object of a class type. The constructor can be used to initialize data members of newly created object. ©NIIT Java Fundamentals Lesson 2B / Slide 22 of 24
  • 23. Classes and Objects Summary(Contd.) • There are two types of constructors the default constructor and the overloaded constructor. • A constructor returns no value and has the same name as the class itself. • A Java application requires a main() method to start execution. • The Java interpreter first of all looks for the main() method. The class name and the file name in which the main() method is declared are same. • You can use access specifiers to restrict the access to class members by other classes. • There are private, public, and protected access specifiers. Private access specifier gives the most restricted access among all the access specifiers. ©NIIT Java Fundamentals Lesson 2B / Slide 23 of 24
  • 24. Classes and Objects Summary(Contd.) • Class members with public specifier can be accessed anywhere in the same class, same package, or a different package. • A modifier determines the use of the data members and methods in other classes and objects. • The various modifiers available in Java are static, final, abstract, native, and synchronized . • You must set the PATH variable of the operating system to the bin directory of the installation directory. • You need to use the javac fileName.java command to compile the fileName.java file. • You need to use the java fileName to execute the fileName.class file. ©NIIT Java Fundamentals Lesson 2B / Slide 24 of 24