SlideShare une entreprise Scribd logo
1  sur  24
Java Programming Language
Objectives


                In this session, you will learn to:
                   Create final classes, methods, and variables
                   Create and use enumerated types
                   Use the static import statement
                   Create abstract classes and methods
                   Create and use an interface
                   Define exceptions
                   Use try, catch, and finally statements
                   Describe exception categories
                   Identify common exceptions
                   Develop programs to handle your own exceptions
                   Use assertions
                   Distinguish appropriate and inappropriate uses of assertions
                   Enable assertions at runtime

     Ver. 1.0                        Session 7                            Slide 1 of 24
Java Programming Language
The final Keyword


                The final keyword is used for security reasons.
                It is used to create classes that serve as a standard.
                It implements the following restrictions:
                 –   You cannot subclass a final class.
                 –   You cannot override a final method.
                 –   A final variable is a constant.
                 –   All methods and data members in a final class are implicitly
                     final.
                You can set a final variable once only, but that
                assignment can occur independently of the declaration;
                this is called a blank final variable.




     Ver. 1.0                         Session 7                           Slide 2 of 24
Java Programming Language
Blank Final Variables


                A final variable that is not initialized in its declaration; its
                initialization is delayed:
                   A blank final instance variable must be assigned in a
                   constructor.
                   A blank final local variable can be set at any time in the body of
                   the method.
                It can be set once only.




     Ver. 1.0                        Session 7                              Slide 3 of 24
Java Programming Language
Enumerated Types


               • An enum type field consist of a fixed set of constants.
               • You can define an enum type by using the enum keyword.
                 For example, you would specify a days-of-the-week enum
                 type as:
                  public enum Day { SUNDAY, MONDAY, TUESDAY,
                  WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
               • The enum class body can include methods and other fields.
               • The compiler automatically adds some special methods
                 when it creates an enum.
               • All enums implicitly extend from java.lang.Enum. Since
                 Java does not support multiple inheritance, an enum cannot
                 extend anything else.



    Ver. 1.0                        Session 7                       Slide 4 of 24
Java Programming Language
Static Imports


                Imports the static members from a class:
                 import static
                 <pkg_list>.<class_name>.<member_name>;
                OR
                 import static <pkg_list>.<class_name>.*;
                Imports members individually or collectively:
                 import static cards.domain.Suit.SPADES;
                OR
                 import static cards.domain.Suit.*;
                There is no need to qualify the static constants:
                 PlayingCard card1 = new PlayingCard(SPADES,
                 2);


     Ver. 1.0                   Session 7                  Slide 5 of 24
Java Programming Language
Abstract Classes


                • An abstract class is declared with abstract access
                  specifier and it may or may not include abstract methods.
                • Abstract classes cannot be instantiated, but they can be
                  subclassed. For example:



                                                  Shape




                       Circle                Rectangle          Hexagon




     Ver. 1.0                         Session 7                       Slide 6 of 24
Java Programming Language
Abstract Classes (Contd.)


                An abstract class defines the common properties and
                behaviors of other classes.
                It is used as a base class to derive specific classes of the
                same type. For example:
                  abstract class Shape
                  {
                  public abstract float calculateArea();
                  }

                The preceding abstract method, calculateArea, is inherited
                by the subclasses of the Shape class. The subclasses
                Rectangle, Circle, and Hexagon implement this method in
                different ways.


     Ver. 1.0                       Session 7                          Slide 7 of 24
Java Programming Language
Abstract Classes (Contd.)


                A simple example of implementation of Abstract Method:
                 public class Circle extends Shape
                 {
                      float radius;
                      public float calculateArea()
                      {
                      return ((radius * radius)* (22/7));
                      }
                 }
                In the preceding example, the calculateArea() method has
                been overridden in the Circle class.



     Ver. 1.0                      Session 7                       Slide 8 of 24
Java Programming Language
Interfaces


                A public interface is a contract between client code and the
                class that implements that interface.
                A Java interface is a formal declaration of such a contract in
                which all methods contain no implementation.
                Many unrelated classes can implement the same interface.
                A class can implement many unrelated interfaces.
                Syntax of a Java class declaration with interface
                implementation is as follows:
                 <modifier> class <name> [extends
                 <superclass>]
                 [implements <interface> [,<interface>]* ]
                 { <member_declaration>*
                 }

     Ver. 1.0                       Session 7                          Slide 9 of 24
Java Programming Language
Interfaces (Contd.)


                Interfaces are used to define a behavior protocol (standard
                behavior) that can be implemented by any class anywhere
                in the class hierarchy. For example:
                Consider the devices TV and VDU. Both of them require a
                common functionality as far as brightness control is
                concerned. This functionality can be provided by
                implementing an interface called BrightnessControl which is
                applicable for both the devices.
                Interfaces can be implemented by classes that are not
                related to one another.
                Abstract classes are used only when there is a kind-of
                relationship between the classes.




     Ver. 1.0                      Session 7                        Slide 10 of 24
Java Programming Language
Interfaces (Contd.)


                Uses of Interfaces:
                   Declaring methods that one or more classes are expected to
                   implement
                   Determining an object’s programming interface without
                   revealing the actual body of the class
                   Capturing similarities between unrelated classes without
                   forcing a class relationship
                   Simulating multiple inheritance by declaring a class that
                   implements several interfaces




     Ver. 1.0                         Session 7                        Slide 11 of 24
Java Programming Language
Exceptions and Assertions


                Exceptions are a mechanism used to describe what to do
                when something unexpected happens. For example:
                   When a method is invoked with unacceptable arguments
                   A network connection fails
                   The user asks to open a non-existent file
                Assertions are a way to test certain assumptions about the
                logic of a program. For example:
                   To test that the value of a variable at a particular point is
                   always positive




     Ver. 1.0                         Session 7                               Slide 12 of 24
Java Programming Language
Exceptions


                Conditions that can readily occur in a correct program are
                checked exceptions.
                 – These are represented by the Exception class.
                Severe problems that normally are treated as fatal or
                situations that probably reflect program bugs are unchecked
                exceptions.
                 – Fatal situations are represented by the Error class.
                Probable bugs are represented by the RuntimeException
                class.
                The API documentation shows checked exceptions that can
                be thrown from a method.




     Ver. 1.0                        Session 7                            Slide 13 of 24
Java Programming Language
Exceptions (Contd.)


                  Consider the following code snippet:
                   public void myMethod(int num1 , int num2)
                   {
                      int result;
                      result = num2 / num1;
                      System.out.println(“Result:” + result);
                   }
                • In the preceding piece of code an exception
                  java.lang.ArithmeticException is thrown when the
                  value of num1 is equal to zero. The error message
                  displayed is:
                  Exception in thread “main”
                  java.lang.ArithmeticException: / by zero at
                  <classname>.main(<filename>)


     Ver. 1.0                     Session 7                   Slide 14 of 24
Java Programming Language
The try-catch Statement


                The try-catch block:
                 – The try block governs the statements that are enclosed within
                   it and defines the scope of the exception-handlers associated
                   with it.
                 – A try block must have at least one catch block that follows it
                   immediately.
                 – The catch statement takes the object of the exception class
                   that refers to the exception caught, as a parameter.
                 – Once the exception is caught, the statements within the catch
                   block are executed.
                 – The scope of the catch block is restricted to the statements in
                   the preceding try block only.




     Ver. 1.0                        Session 7                           Slide 15 of 24
Java Programming Language
The try-catch Statement (Contd.)


                An example of try-catch block:
                 public void myMethod(int num1 , int num2)
                 {
                   int result;
                   try{
                     result = num2 / num1;
                   }
                   catch(ArithmeticException e)
                   {
                     System.out.println(“Error…division by
                     zero”);
                   }
                   System.out.println(“Result:” + result);
                 }



     Ver. 1.0                 Session 7                Slide 16 of 24
Java Programming Language
Call Stack Mechanism


                If an exception is not handled in the current try-catch block,
                it is thrown to the caller of the method.
                If the exception gets back to the main method and is not
                handled there, the program is terminated abnormally.




     Ver. 1.0                       Session 7                          Slide 17 of 24
Java Programming Language
The finally Clause


                • The characteristics of the finally clause:
                   – Defines a block of code that always executes, regardless of
                     whether an exception is thrown.
                   – The finally block follows the catch blocks.
                   – It is not mandatory to have a finally block.




     Ver. 1.0                          Session 7                           Slide 18 of 24
Java Programming Language
Creating Your Own Exceptions


                Characteristics of user-defined exceptions:
                 – Created by extending the Exception class.
                 – The extended class contains constructors, data members and
                   methods.
                 – The throw and throws keywords are used while
                   implementing user-defined exceptions.




     Ver. 1.0                       Session 7                         Slide 19 of 24
Java Programming Language
Demonstration


               Let see how to create a custom Exception class, and use it in a
               Java program.




    Ver. 1.0                         Session 7                        Slide 20 of 24
Java Programming Language
Assertions


                  Syntax of an assertion is:
                    assert <boolean_expression> ;
                    assert <boolean_expression> :
                    <detail_expression> ;
                • If <boolean_expression> evaluates false, then an
                  AssertionError is thrown.
                • The second argument is converted to a string and used as
                  descriptive text in the AssertionError message.




     Ver. 1.0                        Session 7                      Slide 21 of 24
Java Programming Language
Assertions (Contd.)


                Recommended Uses of Assertions:
                   Use assertions to document and verify the assumptions and
                   internal logic of a single method:
                       Internal invariants
                       Control flow invariants
                       Postconditions and class invariants
                Inappropriate Uses of Assertions:
                   Do not use assertions to check the parameters of a public
                   method.
                   Do not use methods in the assertion check that can cause
                   side-effects.




     Ver. 1.0                         Session 7                         Slide 22 of 24
Java Programming Language
Summary


               In this session, you learned that:
                – final classes cannot be subclassed,final methods cannot
                  be overriden, and final variables are constant.
                – An enum type is a type whose fields consist of a fixed set of
                  constants.
                – An abstract class defines the common properties and
                  behaviors of other classes. It is used as a base class to derive
                  specific classes of the same type:
                    • Abstract classes allow implementation of a behavior in different
                      ways. The implementation is done in subclasses.
                    • An abstract class cannot be instantiated.
                    • Subclasses must override the abstract methods of the super class.
                – Interfaces are used to define a behavior protocol that can be
                  implemented by any class anywhere in the class hierarchy.



    Ver. 1.0                         Session 7                               Slide 23 of 24
Java Programming Language
Summary (Contd.)


               An exception is an abnormal event that occurs during the
               program execution and disrupts the normal flow of instructions.
               You can implement exception handling in your program by
               using the following keywords:
                •   try
                •   catch
                •   throws/throw
                •   finally
               Assertions can be used to document and verify the
               assumptions and internal logic of a single method:
                    Internal invariants
                    Control flow invariants
                    Postconditions and class invariants




    Ver. 1.0                       Session 7                         Slide 24 of 24

Contenu connexe

Tendances

Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun Vasanth
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2eeShiva Cse
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for vivaVipul Naik
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java CertificationVskills
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For SyntaxPravinYalameli
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 
Questions of java
Questions of javaQuestions of java
Questions of javaWaseem Wasi
 
OOPs difference faqs-3
OOPs difference faqs-3OOPs difference faqs-3
OOPs difference faqs-3Umar Ali
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview QuestionsArc & Codementor
 
[JWPA-1]의존성 주입(Dependency injection)
[JWPA-1]의존성 주입(Dependency injection)[JWPA-1]의존성 주입(Dependency injection)
[JWPA-1]의존성 주입(Dependency injection)Young-Ho Cho
 

Tendances (20)

Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
Questions of java
Questions of javaQuestions of java
Questions of java
 
OOPs difference faqs-3
OOPs difference faqs-3OOPs difference faqs-3
OOPs difference faqs-3
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview Questions
 
Dacj 2-1 c
Dacj 2-1 cDacj 2-1 c
Dacj 2-1 c
 
Java interview question
Java interview questionJava interview question
Java interview question
 
[JWPA-1]의존성 주입(Dependency injection)
[JWPA-1]의존성 주입(Dependency injection)[JWPA-1]의존성 주입(Dependency injection)
[JWPA-1]의존성 주입(Dependency injection)
 

En vedette

15 ooad uml-20
15 ooad uml-2015 ooad uml-20
15 ooad uml-20Niit Care
 
Vb.net session 09
Vb.net session 09Vb.net session 09
Vb.net session 09Niit Care
 
11 ds and algorithm session_16
11 ds and algorithm session_1611 ds and algorithm session_16
11 ds and algorithm session_16Niit Care
 
09 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_1309 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_13Niit Care
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01Niit Care
 
14 ooad uml-19
14 ooad uml-1914 ooad uml-19
14 ooad uml-19Niit Care
 
Deawsj 7 ppt-2_c
Deawsj 7 ppt-2_cDeawsj 7 ppt-2_c
Deawsj 7 ppt-2_cNiit Care
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItAzul Systems Inc.
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06Niit Care
 
Chapter 21 c language
Chapter 21 c languageChapter 21 c language
Chapter 21 c languageHareem Aslam
 

En vedette (20)

15 ooad uml-20
15 ooad uml-2015 ooad uml-20
15 ooad uml-20
 
Vb.net session 09
Vb.net session 09Vb.net session 09
Vb.net session 09
 
Dacj 2-2 c
Dacj 2-2 cDacj 2-2 c
Dacj 2-2 c
 
Oops recap
Oops recapOops recap
Oops recap
 
11 ds and algorithm session_16
11 ds and algorithm session_1611 ds and algorithm session_16
11 ds and algorithm session_16
 
09 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_1309 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_13
 
OOP Java
OOP JavaOOP Java
OOP Java
 
Rdbms xp 01
Rdbms xp 01Rdbms xp 01
Rdbms xp 01
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01
 
14 ooad uml-19
14 ooad uml-1914 ooad uml-19
14 ooad uml-19
 
Deawsj 7 ppt-2_c
Deawsj 7 ppt-2_cDeawsj 7 ppt-2_c
Deawsj 7 ppt-2_c
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
Ds 8
Ds 8Ds 8
Ds 8
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About It
 
Aae oop xp_06
Aae oop xp_06Aae oop xp_06
Aae oop xp_06
 
Dacj 1-1 a
Dacj 1-1 aDacj 1-1 a
Dacj 1-1 a
 
Chapter 21 c language
Chapter 21 c languageChapter 21 c language
Chapter 21 c language
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 

Similaire à

Java session01
Java session01Java session01
Java session01Niit Care
 
Java session05
Java session05Java session05
Java session05Niit Care
 
Java session02
Java session02Java session02
Java session02Niit Care
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview QuestionsKuntal Bhowmick
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)Shaharyar khan
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Simplilearn
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics PresentationOmid Sohrabi
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedyearninginjava
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedGaurav Maheshwari
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaAjay Sharma
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 

Similaire à (20)

Java session01
Java session01Java session01
Java session01
 
Java session05
Java session05Java session05
Java session05
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Java session02
Java session02Java session02
Java session02
 
Java intro
Java introJava intro
Java intro
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
 
Basics of java 1
Basics of java 1Basics of java 1
Basics of java 1
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
 
01slide
01slide01slide
01slide
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
01slide
01slide01slide
01slide
 
01slide
01slide01slide
01slide
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 

Plus de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 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 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 

Dernier

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Dernier (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

  • 1. Java Programming Language Objectives In this session, you will learn to: Create final classes, methods, and variables Create and use enumerated types Use the static import statement Create abstract classes and methods Create and use an interface Define exceptions Use try, catch, and finally statements Describe exception categories Identify common exceptions Develop programs to handle your own exceptions Use assertions Distinguish appropriate and inappropriate uses of assertions Enable assertions at runtime Ver. 1.0 Session 7 Slide 1 of 24
  • 2. Java Programming Language The final Keyword The final keyword is used for security reasons. It is used to create classes that serve as a standard. It implements the following restrictions: – You cannot subclass a final class. – You cannot override a final method. – A final variable is a constant. – All methods and data members in a final class are implicitly final. You can set a final variable once only, but that assignment can occur independently of the declaration; this is called a blank final variable. Ver. 1.0 Session 7 Slide 2 of 24
  • 3. Java Programming Language Blank Final Variables A final variable that is not initialized in its declaration; its initialization is delayed: A blank final instance variable must be assigned in a constructor. A blank final local variable can be set at any time in the body of the method. It can be set once only. Ver. 1.0 Session 7 Slide 3 of 24
  • 4. Java Programming Language Enumerated Types • An enum type field consist of a fixed set of constants. • You can define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } • The enum class body can include methods and other fields. • The compiler automatically adds some special methods when it creates an enum. • All enums implicitly extend from java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else. Ver. 1.0 Session 7 Slide 4 of 24
  • 5. Java Programming Language Static Imports Imports the static members from a class: import static <pkg_list>.<class_name>.<member_name>; OR import static <pkg_list>.<class_name>.*; Imports members individually or collectively: import static cards.domain.Suit.SPADES; OR import static cards.domain.Suit.*; There is no need to qualify the static constants: PlayingCard card1 = new PlayingCard(SPADES, 2); Ver. 1.0 Session 7 Slide 5 of 24
  • 6. Java Programming Language Abstract Classes • An abstract class is declared with abstract access specifier and it may or may not include abstract methods. • Abstract classes cannot be instantiated, but they can be subclassed. For example: Shape Circle Rectangle Hexagon Ver. 1.0 Session 7 Slide 6 of 24
  • 7. Java Programming Language Abstract Classes (Contd.) An abstract class defines the common properties and behaviors of other classes. It is used as a base class to derive specific classes of the same type. For example: abstract class Shape { public abstract float calculateArea(); } The preceding abstract method, calculateArea, is inherited by the subclasses of the Shape class. The subclasses Rectangle, Circle, and Hexagon implement this method in different ways. Ver. 1.0 Session 7 Slide 7 of 24
  • 8. Java Programming Language Abstract Classes (Contd.) A simple example of implementation of Abstract Method: public class Circle extends Shape { float radius; public float calculateArea() { return ((radius * radius)* (22/7)); } } In the preceding example, the calculateArea() method has been overridden in the Circle class. Ver. 1.0 Session 7 Slide 8 of 24
  • 9. Java Programming Language Interfaces A public interface is a contract between client code and the class that implements that interface. A Java interface is a formal declaration of such a contract in which all methods contain no implementation. Many unrelated classes can implement the same interface. A class can implement many unrelated interfaces. Syntax of a Java class declaration with interface implementation is as follows: <modifier> class <name> [extends <superclass>] [implements <interface> [,<interface>]* ] { <member_declaration>* } Ver. 1.0 Session 7 Slide 9 of 24
  • 10. Java Programming Language Interfaces (Contd.) Interfaces are used to define a behavior protocol (standard behavior) that can be implemented by any class anywhere in the class hierarchy. For example: Consider the devices TV and VDU. Both of them require a common functionality as far as brightness control is concerned. This functionality can be provided by implementing an interface called BrightnessControl which is applicable for both the devices. Interfaces can be implemented by classes that are not related to one another. Abstract classes are used only when there is a kind-of relationship between the classes. Ver. 1.0 Session 7 Slide 10 of 24
  • 11. Java Programming Language Interfaces (Contd.) Uses of Interfaces: Declaring methods that one or more classes are expected to implement Determining an object’s programming interface without revealing the actual body of the class Capturing similarities between unrelated classes without forcing a class relationship Simulating multiple inheritance by declaring a class that implements several interfaces Ver. 1.0 Session 7 Slide 11 of 24
  • 12. Java Programming Language Exceptions and Assertions Exceptions are a mechanism used to describe what to do when something unexpected happens. For example: When a method is invoked with unacceptable arguments A network connection fails The user asks to open a non-existent file Assertions are a way to test certain assumptions about the logic of a program. For example: To test that the value of a variable at a particular point is always positive Ver. 1.0 Session 7 Slide 12 of 24
  • 13. Java Programming Language Exceptions Conditions that can readily occur in a correct program are checked exceptions. – These are represented by the Exception class. Severe problems that normally are treated as fatal or situations that probably reflect program bugs are unchecked exceptions. – Fatal situations are represented by the Error class. Probable bugs are represented by the RuntimeException class. The API documentation shows checked exceptions that can be thrown from a method. Ver. 1.0 Session 7 Slide 13 of 24
  • 14. Java Programming Language Exceptions (Contd.) Consider the following code snippet: public void myMethod(int num1 , int num2) { int result; result = num2 / num1; System.out.println(“Result:” + result); } • In the preceding piece of code an exception java.lang.ArithmeticException is thrown when the value of num1 is equal to zero. The error message displayed is: Exception in thread “main” java.lang.ArithmeticException: / by zero at <classname>.main(<filename>) Ver. 1.0 Session 7 Slide 14 of 24
  • 15. Java Programming Language The try-catch Statement The try-catch block: – The try block governs the statements that are enclosed within it and defines the scope of the exception-handlers associated with it. – A try block must have at least one catch block that follows it immediately. – The catch statement takes the object of the exception class that refers to the exception caught, as a parameter. – Once the exception is caught, the statements within the catch block are executed. – The scope of the catch block is restricted to the statements in the preceding try block only. Ver. 1.0 Session 7 Slide 15 of 24
  • 16. Java Programming Language The try-catch Statement (Contd.) An example of try-catch block: public void myMethod(int num1 , int num2) { int result; try{ result = num2 / num1; } catch(ArithmeticException e) { System.out.println(“Error…division by zero”); } System.out.println(“Result:” + result); } Ver. 1.0 Session 7 Slide 16 of 24
  • 17. Java Programming Language Call Stack Mechanism If an exception is not handled in the current try-catch block, it is thrown to the caller of the method. If the exception gets back to the main method and is not handled there, the program is terminated abnormally. Ver. 1.0 Session 7 Slide 17 of 24
  • 18. Java Programming Language The finally Clause • The characteristics of the finally clause: – Defines a block of code that always executes, regardless of whether an exception is thrown. – The finally block follows the catch blocks. – It is not mandatory to have a finally block. Ver. 1.0 Session 7 Slide 18 of 24
  • 19. Java Programming Language Creating Your Own Exceptions Characteristics of user-defined exceptions: – Created by extending the Exception class. – The extended class contains constructors, data members and methods. – The throw and throws keywords are used while implementing user-defined exceptions. Ver. 1.0 Session 7 Slide 19 of 24
  • 20. Java Programming Language Demonstration Let see how to create a custom Exception class, and use it in a Java program. Ver. 1.0 Session 7 Slide 20 of 24
  • 21. Java Programming Language Assertions Syntax of an assertion is: assert <boolean_expression> ; assert <boolean_expression> : <detail_expression> ; • If <boolean_expression> evaluates false, then an AssertionError is thrown. • The second argument is converted to a string and used as descriptive text in the AssertionError message. Ver. 1.0 Session 7 Slide 21 of 24
  • 22. Java Programming Language Assertions (Contd.) Recommended Uses of Assertions: Use assertions to document and verify the assumptions and internal logic of a single method: Internal invariants Control flow invariants Postconditions and class invariants Inappropriate Uses of Assertions: Do not use assertions to check the parameters of a public method. Do not use methods in the assertion check that can cause side-effects. Ver. 1.0 Session 7 Slide 22 of 24
  • 23. Java Programming Language Summary In this session, you learned that: – final classes cannot be subclassed,final methods cannot be overriden, and final variables are constant. – An enum type is a type whose fields consist of a fixed set of constants. – An abstract class defines the common properties and behaviors of other classes. It is used as a base class to derive specific classes of the same type: • Abstract classes allow implementation of a behavior in different ways. The implementation is done in subclasses. • An abstract class cannot be instantiated. • Subclasses must override the abstract methods of the super class. – Interfaces are used to define a behavior protocol that can be implemented by any class anywhere in the class hierarchy. Ver. 1.0 Session 7 Slide 23 of 24
  • 24. Java Programming Language Summary (Contd.) An exception is an abnormal event that occurs during the program execution and disrupts the normal flow of instructions. You can implement exception handling in your program by using the following keywords: • try • catch • throws/throw • finally Assertions can be used to document and verify the assumptions and internal logic of a single method: Internal invariants Control flow invariants Postconditions and class invariants Ver. 1.0 Session 7 Slide 24 of 24