SlideShare a Scribd company logo
Collaborate


Knowledge Byte
    In this section, you will learn about:


         •   The super and this keywords
         •   The nested try and catch block




 ©NIIT                          Collaborate   Lesson 1C / Slide 1 of 23
Collaborate


The super and this Keywords
    •    The super keyword
           • Java provides the super keyword that enables a subclass to refer to its
             superclass. The super keyword is used to access:
                • superclass constructors
                • superclass methods and variables
           • The syntax to invoke the constructor of a superclass using the super()
             method is:
             super (<parameter1>, <parameter 2>,..,<parameterN>);
             In the above syntax, <parameter1>, <parameter 2>,..,<parameterN>
             refer to the list of parameters that you need to pass to the constructor of
             the superclass.




 ©NIIT                           Collaborate                   Lesson 1C / Slide 2 of 23
Collaborate


The super and this Keywords (Contd.)
    •    The super Keyword (Contd.)
           • If no parameters are passed to the super() method, it invokes the default
             constructor of the superclass.
           • If the superclass contains the overloaded constructor, you can pass
             parameters to the super() method to invoke a particular constructor.
           • When you use the super() method in the constructor of the subclass, it
             should be the first executable statement in the constructor.




 ©NIIT                          Collaborate                   Lesson 1C / Slide 3 of 23
Collaborate


The super and this Keywords (Contd.)
    •    The super Keyword (Contd.)
           • The syntax to access the member variable of a superclass is:
             super.<variable>;
           • The subclass can also access the member methods of the superclass using
             the super keyword.




 ©NIIT                         Collaborate                  Lesson 1C / Slide 4 of 23
Collaborate


The super and this Keywords (Contd.)
    •    The this Keyword
         • The this keyword is used to refer to the current object.
         • You can use the this keyword when a method defined in a Java class
              needs to refer to the object used to invoke that method.
         • The following code snippet shows how to use the this keyword:
              Book(int bcode, double bprice)
              {
              this.bookCode=bcode;
              this.bookPrice=bprice;
              }
              In the above code snippet, the this keyword refers to the object that
              invokes the Book() constructor.




 ©NIIT                        Collaborate                   Lesson 1C / Slide 5 of 23
Collaborate


The super and this Keywords (Contd.)
    •    The this Keyword (Contd.)
         • Another situation where you can use the this keyword is when the local
              and instance variables have the same name.
         • The following code snippet shows how to use the this keyword when
              instance and formal parameters have the same name:
              Book(int bcode, double bprice)
              {
              this.bcode=bcode;
              this.bprice=bprice;
              }
              In the above code snippet, this keyword refers to the instance variables,
              bcode and bprice. The values of the formal parameters, bcode and
              bprice of the Book() constructor are assigned to the instance variables.




 ©NIIT                        Collaborate                    Lesson 1C / Slide 6 of 23
Collaborate


The super and this Keywords (Contd.)
    •    The this Keyword (Contd.)
          • In addition, you can use the this keyword in a constructor of a class to
               invoke another constructor of the class.
          • Unlike the super keyword, the this keyword can invoke the
               constructor of the same class.
          • The following code snippet shows how to invoke a constructor using
               the this keyword:
               class Book {
               public Book(String bname)        {
                   this(bname, 1001); }
               public Book(String bname, int bcode) {
                   bookName=bname;
                   bookCode=bcode; }
                }


 ©NIIT                       Collaborate                   Lesson 1C / Slide 7 of 23
Collaborate


The Nested try-catch Block
    •    The try-catch block is used to handle exceptions in Java applications.
    •    You can enclose a try-catch block in an existing try-catch block.
    •    The enclosed try-catch block is called the inner try-catch block, and the
         enclosing block is called the outer try-catch block.
    •    If the inner try block does not contain the catch statement to handle an
         exception then the catch statement in the outer block is checked for the
         exception handler.
    •    The following syntax shows how to create a nested try-catch block:
         class SuperClass
         {
         public static void main(String a[])
         {
         <code>;
         try
         {

 ©NIIT                        Collaborate                   Lesson 1C / Slide 8 of 23
Collaborate


The Nested try-catch Block (Contd.)
    •    The following syntax shows how to create a nested try-catch block: (Contd.)
              <code>;
              try
              {
              <code>;
              }
              catch(<exception_name> <var>)
              {
              <code>;
              }
         }
              catch(<exception_name> <var>)
              {
              <code>;
              } } }
 ©NIIT                        Collaborate                  Lesson 1C / Slide 9 of 23
Collaborate


From the Expert’s Desk

    In this section, you will learn:


         •    Best practices on:
              • The Use of final Keyword
         •    Tips on:
              • The Use of Constructors
              • The finally Block
         •    FAQs




 ©NIIT                           Collaborate   Lesson 1C / Slide 10 of 23
Collaborate


Best Practices
The Use of final Keyword
    •    The final keyword can be used with:
         • A variable
         • A method
         • A class
    •    The use of the final keyword prevents you from changing the value of a
         variable.
    •    When you use the final keyword with a variable, the variable acts as a
         constant.
    •    It is recommended that you name the final variable in uppercase.




 ©NIIT                        Collaborate                 Lesson 1C / Slide 11 of 23
Collaborate


Best Practices
The Use of final Keyword (Contd.)
    •    The syntax to declare the final variable is:
         final int MY_VAR=10;
         In the above syntax, the MY_VAR variable is declared as final. As a result,
         the value of the variable cannot be modified.
    •    The final keyword is used with methods and classes to prevent method and
         class overriding while implementing inheritance in a Java application.
    •    If a method in a subclass has the same name as the final method in a
         superclass, a compile time error is generated.




 ©NIIT                        Collaborate                  Lesson 1C / Slide 12 of 23
Collaborate


Best Practices
The Use of final Keyword (Contd.)
    •    The following syntax shows how to declare the final method:
         class SuperClass
         {
         final void finalMethod()
         {}
         }
    •    Similarly, if a class is declared final, you cannot extend that class.
    •    If you extend the final class, a compile time error is generated.




 ©NIIT                          Collaborate                    Lesson 1C / Slide 13 of 23
Collaborate


Best Practices
The Use of final Keyword (Contd.)
    •    The following syntax shows how to declare the final class:
         final class SuperClass
         {
         //This is a final class.
         }
         In the preceding syntax, the SuperClass class is declared as final.




 ©NIIT                         Collaborate                  Lesson 1C / Slide 14 of 23
Collaborate


Tips
The Use of Constructors
    •    Consider a Java application that contains multiple classes. Each class is
         extended from another class in the application and contains a default
         constructor.
    •    When the application is executed, the constructors are called in the order in
         which the classes are defined.
    •    For example, there are three classes in a Java application: Class1, Class2,
         and Class3. Class2 extends Class1, and Class3 extends Class2. When you
         create an instance of Class 3, the constructors are called in the following
         order:
         • Class1 constructor
         • Class2 constructor
         • Class3 constructor


 ©NIIT                         Collaborate                  Lesson 1C / Slide 15 of 23
Collaborate


Tips
The finally Block
    •    The finally block associated with a try block is executed after the try or catch
         block is completed.
    •    It is optional to associate the finally block with a try block, but if the try block
         does not have a corresponding catch block, you should provide the finally
         block.




 ©NIIT                          Collaborate                     Lesson 1C / Slide 16 of 23
Collaborate


FAQs
    •    Can classes inherit from more than one class in Java?

         No, a class cannot be inherited from more than one class in Java. A class is
         inherited from a single class only. However, multi-level inheritance is
         possible in Java. For example, if class B extends class A and class C extends
         class B, class C automatically inherits the properties of class A.


    •    What is the difference between an interface and a class?

         Both, class and interface, contain constants and methods. A class not only
         defines methods but also provides their implementation. However, an
         interface only defines the methods, which are implemented by the class that
         implements the interface.




 ©NIIT                         Collaborate                  Lesson 1C / Slide 17 of 23
Collaborate


FAQs (Contd.)
    •    Do you have to catch all types of exceptions that might be thrown by Java?

         Yes, you can catch all types of exceptions that are thrown in a Java
         application using the Exception class.


    •    How many exceptions can you associate with a single try block?

         There is no limit regarding the number of exceptions that can be associated
         with a single try block. All the exceptions associated with a try block should
         be handled using multiple catch blocks.




 ©NIIT                         Collaborate                   Lesson 1C / Slide 18 of 23
Collaborate


FAQs (Contd.)
    •    What are the disadvantages of inner classes?

         The inner classes have various disadvantages. The use of inner class
         increases the total number of classes in your code. The Java developers also
         find it difficult to understand to implement the concept of inner class within
         the programs.


    •    What is the level of nesting for classes in Java?

         There is no limit of nesting classes in Java.




 ©NIIT                          Collaborate                  Lesson 1C / Slide 19 of 23
Collaborate


Challenge
    1.   You can access particular constructors in the superclass using
         ______operator.
    2.   Which of the following exceptions is raised when a number is divided by
         zero:
         a.    NullPointerException
         b.    ArithmeticException
         c.    ArrayIndexOutOfBoundsException
         d.    NumberFormatException
    3.   You can have more than one finally block for an exception-handler.
         (True/False)
    4.   Match the following:
          a. public 1. Ensures the value is not changed in the classes that
                        implement an interface.
         b. static 2. Ensures that are able to override the data members in
                        unrelated classes.
         c. final    3. Ensures that you cannot create an object of the interface.
 ©NIIT                         Collaborate                  Lesson 1C / Slide 20 of 23
Collaborate


Challenge (Contd.)
  1.     The following code is a jumbled code. Rearrange the following code to make it
         executable:
         public class Excp
         {
              public static void main(String args []) {
              int num1, num2, num3;
              num1 = 10;
              num2 = 0;
              catch(ArithmeticException e)
              {
                   System.out.println(" Error Message");
              } try
                   {
                              num3 = num1/num2;
                   }
 ©NIIT                          Collaborate                 Lesson 1C / Slide 21 of 23
Collaborate


Solutions to Challenge

    •       dot
    •       b. ArithmeticException
    •       False
    •       a. 2
            b. 1
            c. 3
    5.      public class Excp
                            {
                      public static void main(String args [])
                      {
                      int num1, num2, num3;
                      num1 = 10;
                      num2 = 0;

 ©NIIT                      Collaborate                Lesson 1C / Slide 22 of 23
Collaborate


Solutions to Challenge (Contd.)
         try
         {
                 num3 = num1/num2;
         }
         catch( ArithmeticException e)
         {
                  System.out.println(" Error Message");
         }




 ©NIIT                     Collaborate              Lesson 1C / Slide 23 of 23

More Related Content

What's hot

Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Lakshmi Sarvani Videla
 
Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)
smumbahelp
 
Questions of java
Questions of javaQuestions of java
Questions of java
Waseem Wasi
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
Chandan Benjaram
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
Ayush Gupta
 
Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
Bernhard Huemer
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
Mark Papis
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
sandy14234
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Lovely Professional University
 
OOPs difference faqs-3
OOPs difference faqs-3OOPs difference faqs-3
OOPs difference faqs-3
Umar Ali
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
Hitesh-Java
 
Inheritance
InheritanceInheritance
Inheritance
piyush Kumar Sharma
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
SivaRamaSundar Devasubramaniam
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
sureshraj43
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Khaled Adnan
 
Java session13
Java session13Java session13
Java session13
Niit Care
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
PawanMM
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
Shipra Swati
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
keval_thummar
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar
 

What's hot (20)

Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)Mcs 024 assignment solution (2020-21)
Mcs 024 assignment solution (2020-21)
 
Questions of java
Questions of javaQuestions of java
Questions of java
 
Ej Chpt#4 Final
Ej Chpt#4 FinalEj Chpt#4 Final
Ej Chpt#4 Final
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
 
Concurrency on the JVM
Concurrency on the JVMConcurrency on the JVM
Concurrency on the JVM
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
OOPs difference faqs-3
OOPs difference faqs-3OOPs difference faqs-3
OOPs difference faqs-3
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
 
Inheritance
InheritanceInheritance
Inheritance
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java session13
Java session13Java session13
Java session13
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similar to Dacj 2-1 c

Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
Niit Care
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
Niit Care
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
Niit Care
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 
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
WondimuBantihun1
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
Muhammad Younis
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
Mindsmapped Consulting
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
javatrainingonline
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
Raghavendra V Gayakwad
 
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
 
31 days Refactoring
31 days Refactoring31 days Refactoring
31 days Refactoring
Ahasanul Kalam Akib
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Exception handling in .net
Exception handling in .netException handling in .net
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
ManojSatishKumar
 
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
Jamsher bhanbhro
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
Lhouceine OUHAMZA
 

Similar to Dacj 2-1 c (20)

Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Dacj 1-2 c
Dacj 1-2 cDacj 1-2 c
Dacj 1-2 c
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
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
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
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...
 
31 days Refactoring
31 days Refactoring31 days Refactoring
31 days Refactoring
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
 
Exception handling in .net
Exception handling in .netException handling in .net
Exception handling in .net
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf04_-_Inheritance_Polymorphism_and_Interfaces.pdf
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
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
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 

More from Niit Care

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

More from 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 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
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 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 

Recently uploaded

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
TIPNGVN2
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 

Recently uploaded (20)

Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Data structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdfData structures and Algorithms in Python.pdf
Data structures and Algorithms in Python.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 

Dacj 2-1 c

  • 1. Collaborate Knowledge Byte In this section, you will learn about: • The super and this keywords • The nested try and catch block ©NIIT Collaborate Lesson 1C / Slide 1 of 23
  • 2. Collaborate The super and this Keywords • The super keyword • Java provides the super keyword that enables a subclass to refer to its superclass. The super keyword is used to access: • superclass constructors • superclass methods and variables • The syntax to invoke the constructor of a superclass using the super() method is: super (<parameter1>, <parameter 2>,..,<parameterN>); In the above syntax, <parameter1>, <parameter 2>,..,<parameterN> refer to the list of parameters that you need to pass to the constructor of the superclass. ©NIIT Collaborate Lesson 1C / Slide 2 of 23
  • 3. Collaborate The super and this Keywords (Contd.) • The super Keyword (Contd.) • If no parameters are passed to the super() method, it invokes the default constructor of the superclass. • If the superclass contains the overloaded constructor, you can pass parameters to the super() method to invoke a particular constructor. • When you use the super() method in the constructor of the subclass, it should be the first executable statement in the constructor. ©NIIT Collaborate Lesson 1C / Slide 3 of 23
  • 4. Collaborate The super and this Keywords (Contd.) • The super Keyword (Contd.) • The syntax to access the member variable of a superclass is: super.<variable>; • The subclass can also access the member methods of the superclass using the super keyword. ©NIIT Collaborate Lesson 1C / Slide 4 of 23
  • 5. Collaborate The super and this Keywords (Contd.) • The this Keyword • The this keyword is used to refer to the current object. • You can use the this keyword when a method defined in a Java class needs to refer to the object used to invoke that method. • The following code snippet shows how to use the this keyword: Book(int bcode, double bprice) { this.bookCode=bcode; this.bookPrice=bprice; } In the above code snippet, the this keyword refers to the object that invokes the Book() constructor. ©NIIT Collaborate Lesson 1C / Slide 5 of 23
  • 6. Collaborate The super and this Keywords (Contd.) • The this Keyword (Contd.) • Another situation where you can use the this keyword is when the local and instance variables have the same name. • The following code snippet shows how to use the this keyword when instance and formal parameters have the same name: Book(int bcode, double bprice) { this.bcode=bcode; this.bprice=bprice; } In the above code snippet, this keyword refers to the instance variables, bcode and bprice. The values of the formal parameters, bcode and bprice of the Book() constructor are assigned to the instance variables. ©NIIT Collaborate Lesson 1C / Slide 6 of 23
  • 7. Collaborate The super and this Keywords (Contd.) • The this Keyword (Contd.) • In addition, you can use the this keyword in a constructor of a class to invoke another constructor of the class. • Unlike the super keyword, the this keyword can invoke the constructor of the same class. • The following code snippet shows how to invoke a constructor using the this keyword: class Book { public Book(String bname) { this(bname, 1001); } public Book(String bname, int bcode) { bookName=bname; bookCode=bcode; } } ©NIIT Collaborate Lesson 1C / Slide 7 of 23
  • 8. Collaborate The Nested try-catch Block • The try-catch block is used to handle exceptions in Java applications. • You can enclose a try-catch block in an existing try-catch block. • The enclosed try-catch block is called the inner try-catch block, and the enclosing block is called the outer try-catch block. • If the inner try block does not contain the catch statement to handle an exception then the catch statement in the outer block is checked for the exception handler. • The following syntax shows how to create a nested try-catch block: class SuperClass { public static void main(String a[]) { <code>; try { ©NIIT Collaborate Lesson 1C / Slide 8 of 23
  • 9. Collaborate The Nested try-catch Block (Contd.) • The following syntax shows how to create a nested try-catch block: (Contd.) <code>; try { <code>; } catch(<exception_name> <var>) { <code>; } } catch(<exception_name> <var>) { <code>; } } } ©NIIT Collaborate Lesson 1C / Slide 9 of 23
  • 10. Collaborate From the Expert’s Desk In this section, you will learn: • Best practices on: • The Use of final Keyword • Tips on: • The Use of Constructors • The finally Block • FAQs ©NIIT Collaborate Lesson 1C / Slide 10 of 23
  • 11. Collaborate Best Practices The Use of final Keyword • The final keyword can be used with: • A variable • A method • A class • The use of the final keyword prevents you from changing the value of a variable. • When you use the final keyword with a variable, the variable acts as a constant. • It is recommended that you name the final variable in uppercase. ©NIIT Collaborate Lesson 1C / Slide 11 of 23
  • 12. Collaborate Best Practices The Use of final Keyword (Contd.) • The syntax to declare the final variable is: final int MY_VAR=10; In the above syntax, the MY_VAR variable is declared as final. As a result, the value of the variable cannot be modified. • The final keyword is used with methods and classes to prevent method and class overriding while implementing inheritance in a Java application. • If a method in a subclass has the same name as the final method in a superclass, a compile time error is generated. ©NIIT Collaborate Lesson 1C / Slide 12 of 23
  • 13. Collaborate Best Practices The Use of final Keyword (Contd.) • The following syntax shows how to declare the final method: class SuperClass { final void finalMethod() {} } • Similarly, if a class is declared final, you cannot extend that class. • If you extend the final class, a compile time error is generated. ©NIIT Collaborate Lesson 1C / Slide 13 of 23
  • 14. Collaborate Best Practices The Use of final Keyword (Contd.) • The following syntax shows how to declare the final class: final class SuperClass { //This is a final class. } In the preceding syntax, the SuperClass class is declared as final. ©NIIT Collaborate Lesson 1C / Slide 14 of 23
  • 15. Collaborate Tips The Use of Constructors • Consider a Java application that contains multiple classes. Each class is extended from another class in the application and contains a default constructor. • When the application is executed, the constructors are called in the order in which the classes are defined. • For example, there are three classes in a Java application: Class1, Class2, and Class3. Class2 extends Class1, and Class3 extends Class2. When you create an instance of Class 3, the constructors are called in the following order: • Class1 constructor • Class2 constructor • Class3 constructor ©NIIT Collaborate Lesson 1C / Slide 15 of 23
  • 16. Collaborate Tips The finally Block • The finally block associated with a try block is executed after the try or catch block is completed. • It is optional to associate the finally block with a try block, but if the try block does not have a corresponding catch block, you should provide the finally block. ©NIIT Collaborate Lesson 1C / Slide 16 of 23
  • 17. Collaborate FAQs • Can classes inherit from more than one class in Java? No, a class cannot be inherited from more than one class in Java. A class is inherited from a single class only. However, multi-level inheritance is possible in Java. For example, if class B extends class A and class C extends class B, class C automatically inherits the properties of class A. • What is the difference between an interface and a class? Both, class and interface, contain constants and methods. A class not only defines methods but also provides their implementation. However, an interface only defines the methods, which are implemented by the class that implements the interface. ©NIIT Collaborate Lesson 1C / Slide 17 of 23
  • 18. Collaborate FAQs (Contd.) • Do you have to catch all types of exceptions that might be thrown by Java? Yes, you can catch all types of exceptions that are thrown in a Java application using the Exception class. • How many exceptions can you associate with a single try block? There is no limit regarding the number of exceptions that can be associated with a single try block. All the exceptions associated with a try block should be handled using multiple catch blocks. ©NIIT Collaborate Lesson 1C / Slide 18 of 23
  • 19. Collaborate FAQs (Contd.) • What are the disadvantages of inner classes? The inner classes have various disadvantages. The use of inner class increases the total number of classes in your code. The Java developers also find it difficult to understand to implement the concept of inner class within the programs. • What is the level of nesting for classes in Java? There is no limit of nesting classes in Java. ©NIIT Collaborate Lesson 1C / Slide 19 of 23
  • 20. Collaborate Challenge 1. You can access particular constructors in the superclass using ______operator. 2. Which of the following exceptions is raised when a number is divided by zero: a. NullPointerException b. ArithmeticException c. ArrayIndexOutOfBoundsException d. NumberFormatException 3. You can have more than one finally block for an exception-handler. (True/False) 4. Match the following: a. public 1. Ensures the value is not changed in the classes that implement an interface. b. static 2. Ensures that are able to override the data members in unrelated classes. c. final 3. Ensures that you cannot create an object of the interface. ©NIIT Collaborate Lesson 1C / Slide 20 of 23
  • 21. Collaborate Challenge (Contd.) 1. The following code is a jumbled code. Rearrange the following code to make it executable: public class Excp { public static void main(String args []) { int num1, num2, num3; num1 = 10; num2 = 0; catch(ArithmeticException e) { System.out.println(" Error Message"); } try { num3 = num1/num2; } ©NIIT Collaborate Lesson 1C / Slide 21 of 23
  • 22. Collaborate Solutions to Challenge • dot • b. ArithmeticException • False • a. 2 b. 1 c. 3 5.      public class Excp { public static void main(String args []) { int num1, num2, num3; num1 = 10; num2 = 0; ©NIIT Collaborate Lesson 1C / Slide 22 of 23
  • 23. Collaborate Solutions to Challenge (Contd.) try { num3 = num1/num2; } catch( ArithmeticException e) { System.out.println(" Error Message"); } ©NIIT Collaborate Lesson 1C / Slide 23 of 23