SlideShare a Scribd company logo
Java Programming Language
Objectives


                In this session, you will learn to:
                   Understand the use of casting objects
                   Describe overloading methods and methods with variable
                   arguments
                   Describe overloading constructors and invoking parent class
                   constructors
                   Understand Wrapper classes
                   Understand autoboxing of primitive types
                   Create static variables, methods, and initializers




     Ver. 1.0                        Session 5                           Slide 1 of 22
Java Programming Language
Casting Objects


                Casting objects is used where you have received a
                reference to a parent class, and you want to access the full
                functionality of the object of the subclass:
                 – Use instanceof to test the type of an object.
                 – Restore full functionality of an object by casting.
                 – Check for proper casting using the following guidelines:
                       Casts upward in the hierarchy are done implicitly.
                       Downward casts must be to a subclass and checked by the
                       compiler.
                       The object type is checked at runtime when runtime errors can
                       occur.




     Ver. 1.0                         Session 5                                Slide 2 of 22
Java Programming Language
Overloading Methods


               The methods which perform closely related tasks can be
               given the same name by overloading them.
               Overloading can be used as follows:
                public void println(int i)
                public void println(float f)
                public void println(String s)
               Argument list must differ across various methods.
               Return types can be different.




    Ver. 1.0                     Session 5                       Slide 3 of 22
Java Programming Language
Methods Using Variable Arguments


                •   The varargs or variable arguments is a feature provided
                    by J2SE 5.0.
                •   It helps to pass variable number of arguments, of the same
                    type, as parameters, to a method.
                •   It can be used when you have a number of overloaded
                    methods, which share the same functionality.




     Ver. 1.0                          Session 5                        Slide 4 of 22
Java Programming Language
Methods Using Variable Arguments (Contd.)


                • The following example demonstrates the usage of
                  varargs:
                   public class Statistics {
                       public float average(int... nums) {
                          int sum = 0;
                          for ( int x : nums ) {             nums is an array
                                                             of type int[]
                               sum += x;
                          }
                          return ((float) sum) / nums.length;
                       }
                   }
                  We can invoke the average method by passing any number
                  of arguments as integers.


     Ver. 1.0                         Session 5                       Slide 5 of 22
Java Programming Language
Overloading Constructor


                As with methods, constructors can be overloaded:
                   An example is:
                    public Employee(String name, double
                    salary,Date doB)
                    public Employee(String name, double salary)
                    public Employee(String name, Date DoB)
                   Argument lists must differ.
                   You can use the this reference at the first line of a constructor
                   to call another constructor.




     Ver. 1.0                         Session 5                              Slide 6 of 22
Java Programming Language
Overloading Constructor (Contd.)


                Example of overloading constructors:
                 public class Employee {
                  private static final double BASE_SALARY =
                  15000.00;
                  private String name;
                  private double salary;
                  private Date birthDate;
                  public Employee(String name, double
                  salary, Date DoB) {
                    this.name = name;
                                                     Initializes all
                    this.salary = salary;            instance
                    this.birthDate = DoB;            variables

                  }

     Ver. 1.0                    Session 5                     Slide 7 of 22
Java Programming Language
Overloading Constructor (Contd.)


                   public Employee(String name, double
                salary) {
                                                 used as a forwarding call
                     this(name, salary, null);   to the first constructor
                   }
                   public Employee(String name, Date DoB) {
                     this(name, BASE_SALARY, DoB);
                   }
                   // more Employee code...
                                                calls the first constructor
                 }
                                                             passing in the class
                                                             constant BASE_SALARY
                – The this keyword in a constructor must be the first line of
                  code in the constructor.



     Ver. 1.0                       Session 5                             Slide 8 of 22
Java Programming Language
Constructors Are Not Inherited


                A subclass inherits all methods and variables from the
                superclass (parent class).
                A subclass does not inherit the constructor from the
                superclass.
                Two ways to include a constructor are:
                   Use the default constructor
                   Write one or more explicit constructors




     Ver. 1.0                        Session 5                       Slide 9 of 22
Java Programming Language
Invoking Parent Class Constructors


                • To invoke a parent class constructor, you must place a call
                  to super() in the first line of the constructor.
                • You can call a specific parent constructor by the arguments
                  that you use in the call to super().
                • If the parent class defines constructors, but does not
                  provide a no-argument constructor, then a compiler error
                  message is issued.




     Ver. 1.0                         Session 5                       Slide 10 of 22
Java Programming Language
Invoking Parent Class Constructors (Contd.)


                An example of invoking parent class constructor:
                 public class Manager extends Employee {
                 private String department;
                 public Manager(String name, double salary,

                String dept)
                {
                   super(name, salary);
                   department = dept;
                  }
                  public Manager(String name, String dept)
                  {
                    super(name);
                    department = dept; }
     Ver. 1.0                   Session 5                  Slide 11 of 22
Java Programming Language
Invoking Parent Class Constructors (Contd.)


                    public Manager(String dept)
                    {
                    // This code fails: no super()
                       department = dept;
                    }
                    //more Manager code...
                }




     Ver. 1.0                        Session 5       Slide 12 of 22
Java Programming Language
The Object Class


                • The Object class is the root of all classes in Java.
                • A class declaration with no extends clause implies extends
                  Object. For example:
                   public class Employee
                   {
                   ...
                   }
                   is equivalent to:
                   public class Employee extends Object
                   {
                   ...
                   }
                  Two important methods of object class are:
                      equals()
                      toString()



     Ver. 1.0                         Session 5                       Slide 13 of 22
Java Programming Language
The equals Method


               • The == operator determines if two references are identical
                 to each other (that is, refer to the same object).
               • The equals() method determines if objects are equal but
                 not necessarily identical.
               • The Object implementation of the equals() method uses
                 the == operator.
               • User classes can override the equals method to implement
                 a domain-specific test for equality.

                 Note: You should override the hashCode method if you
                 override the equals method.




    Ver. 1.0                         Session 5                       Slide 14 of 22
Java Programming Language
The toString Method


                • The toString() method has the following characteristics:
                   – This method converts an object to a String.
                   – Use this method during string concatenation.
                   – Override this method to provide information about a
                     user-defined object in readable format.
                   – Use the wrapper class’s toString() static method to
                     convert primitive types to a String.




     Ver. 1.0                         Session 5                            Slide 15 of 22
Java Programming Language
Wrapper Classes


                • The Java programming language provides wrapper
                  classes to manipulate primitive data elements as objects.
                • Each Java primitive data type has a corresponding wrapper
                  class in the java.lang package.
                • Example of Primitive Boxing using wrapper classes:
                   int pInt = 420;
                   Integer wInt = new Integer(pInt);
                   // this is called boxing
                   int p2 = wInt.intValue();
                   // this is called unboxing




     Ver. 1.0                        Session 5                      Slide 16 of 22
Java Programming Language
Autoboxing of Primitive Types


                The autoboxing feature enables you to assign and retrieve
                primitive types without the need of the wrapper classes.
                Example of Primitive Autoboxing:
                 int pInt = 420;
                 Integer wInt = pInt; // this is called autoboxing
                 int p2 = wInt;              // this is called autounboxing
                The J2SE 5.0 compiler will create the wrapper object
                automatically when assigning a primitive to a variable of the
                wrapper class type.
                The compiler will also extract the primitive value when
                assigning from a wrapper object to a primitive variable.




     Ver. 1.0                       Session 5                        Slide 17 of 22
Java Programming Language
The static Keyword


               • The static keyword is used as a modifier on variables,
                 methods, and nested classes.
               • The static keyword declares the attribute or method is
                 associated with the class as a whole rather than any
                 particular instance of that class.
               • Thus,static members are often called class members, such
                 as class attributes or class methods.




    Ver. 1.0                       Session 5                       Slide 18 of 22
Java Programming Language
The static Keyword (Contd.)


               Static Attribute:
                   A public static class attribute can be accessed from outside the
                   class without an instance of the class.
               Static Method:
                   A static method can be invoked without creating the instance of
                   the class.
                   Static methods can not access instance variables.
               Static Initializers:
                – A class can contain code in a static block that does not exist
                  within a method body.
                – Static block code executes once only, when the class is
                  loaded.
                – Usually, a static block is used to initialize static (class)
                  attributes.




    Ver. 1.0                          Session 5                            Slide 19 of 22
Java Programming Language
Demonstration


               Let see how to declare static members in a class, including
               both member variables and methods.




    Ver. 1.0                         Session 5                        Slide 20 of 22
Java Programming Language
Summary


               In this session, you learned that:
                – Casting objects is used where you have received a reference
                  to a parent class, and you want to access the full functionality
                  of the object of the subclass.
                – The methods which perform closely related tasks can be given
                  the same name by overloading them.
                – The varargs feature helps us to write a generic code to pass
                  variable number of arguments, of the same type to a method.
                – The super keyword is used to call the constructor of the
                  parent class.
                – The object class is the root of all classes. The two important
                  methods of the object class are equals() method and
                  tostring() method.




    Ver. 1.0                        Session 5                            Slide 21 of 22
Java Programming Language
Summary (Contd.)

               – Wrapper classes are used to manipulate primitive data
                 elements as objects. Each Java primitive data type has a
                 corresponding wrapper class in the java.lang package.
               – Autoboxing feature of J2SE 5.0 enables you to assign and
                 retrieve primitive types without the need of the wrapper
                 classes.
               – The static keyword declares members (attributes, methods,
                 and nested classes) that are associated with the class rather
                 than the instances of the class.




    Ver. 1.0                       Session 5                          Slide 22 of 22

More Related Content

What's hot

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
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
 
Java Basics
Java BasicsJava Basics
Core java
Core javaCore java
Core java
Shivaraj R
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
Math-Circle
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Sherihan Anver
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
eMexo Technologies
 
Java Notes
Java Notes Java Notes
Java Notes
Sreedhar Chowdam
 
Core java
Core java Core java
Core java
Ravi varma
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
bindur87
 
Java Basics
Java BasicsJava Basics
Java Basics
Brandon Black
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
GlowTouch
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
BG Java EE Course
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
Hoang Nguyen
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
Vskills
 
Java Programming
Java ProgrammingJava Programming
Java Programming
Anjan Mahanta
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
kankemwa Ishaku
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
Prof. Erwin Globio
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
Singsys Pte Ltd
 

What's hot (20)

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
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
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Core java
Core javaCore java
Core java
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Java Notes
Java Notes Java Notes
Java Notes
 
Core java
Core java Core java
Core java
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Object oriented programming-with_java
Object oriented programming-with_javaObject oriented programming-with_java
Object oriented programming-with_java
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 

Similar to Java session05

INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Soumen Santra
 
Future Programming Language
Future Programming LanguageFuture Programming Language
Future Programming LanguageYLTO
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
VinishA23
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and notSalvatore Fazio
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31myrajendra
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
Hamid Ghorbani
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
VijalJain3
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRubyelliando dias
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Hamid Ghorbani
 
How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in javaTyagi2636
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
Lhouceine OUHAMZA
 
lecture 6
 lecture 6 lecture 6
lecture 6
umardanjumamaiwada
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
umardanjumamaiwada
 

Similar to Java session05 (20)

 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 
Future Programming Language
Future Programming LanguageFuture Programming Language
Future Programming Language
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUEITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
ITT 202 PRINCIPLES OF OBJECT ORIENTED TECHNIQUE
 
Javascript fundamentals and not
Javascript fundamentals and notJavascript fundamentals and not
Javascript fundamentals and not
 
Java 06
Java 06Java 06
Java 06
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31Multiple interfaces 9 cm604.31
Multiple interfaces 9 cm604.31
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
 
Unit 4
Unit 4Unit 4
Unit 4
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
lecture 6
 lecture 6 lecture 6
lecture 6
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 

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 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
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
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
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.
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
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
 
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
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
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.
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
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
 

Recently uploaded (20)

Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
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
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
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
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
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
 
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
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
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 !
 

Java session05

  • 1. Java Programming Language Objectives In this session, you will learn to: Understand the use of casting objects Describe overloading methods and methods with variable arguments Describe overloading constructors and invoking parent class constructors Understand Wrapper classes Understand autoboxing of primitive types Create static variables, methods, and initializers Ver. 1.0 Session 5 Slide 1 of 22
  • 2. Java Programming Language Casting Objects Casting objects is used where you have received a reference to a parent class, and you want to access the full functionality of the object of the subclass: – Use instanceof to test the type of an object. – Restore full functionality of an object by casting. – Check for proper casting using the following guidelines: Casts upward in the hierarchy are done implicitly. Downward casts must be to a subclass and checked by the compiler. The object type is checked at runtime when runtime errors can occur. Ver. 1.0 Session 5 Slide 2 of 22
  • 3. Java Programming Language Overloading Methods The methods which perform closely related tasks can be given the same name by overloading them. Overloading can be used as follows: public void println(int i) public void println(float f) public void println(String s) Argument list must differ across various methods. Return types can be different. Ver. 1.0 Session 5 Slide 3 of 22
  • 4. Java Programming Language Methods Using Variable Arguments • The varargs or variable arguments is a feature provided by J2SE 5.0. • It helps to pass variable number of arguments, of the same type, as parameters, to a method. • It can be used when you have a number of overloaded methods, which share the same functionality. Ver. 1.0 Session 5 Slide 4 of 22
  • 5. Java Programming Language Methods Using Variable Arguments (Contd.) • The following example demonstrates the usage of varargs: public class Statistics { public float average(int... nums) { int sum = 0; for ( int x : nums ) { nums is an array of type int[] sum += x; } return ((float) sum) / nums.length; } } We can invoke the average method by passing any number of arguments as integers. Ver. 1.0 Session 5 Slide 5 of 22
  • 6. Java Programming Language Overloading Constructor As with methods, constructors can be overloaded: An example is: public Employee(String name, double salary,Date doB) public Employee(String name, double salary) public Employee(String name, Date DoB) Argument lists must differ. You can use the this reference at the first line of a constructor to call another constructor. Ver. 1.0 Session 5 Slide 6 of 22
  • 7. Java Programming Language Overloading Constructor (Contd.) Example of overloading constructors: public class Employee { private static final double BASE_SALARY = 15000.00; private String name; private double salary; private Date birthDate; public Employee(String name, double salary, Date DoB) { this.name = name; Initializes all this.salary = salary; instance this.birthDate = DoB; variables } Ver. 1.0 Session 5 Slide 7 of 22
  • 8. Java Programming Language Overloading Constructor (Contd.) public Employee(String name, double salary) { used as a forwarding call this(name, salary, null); to the first constructor } public Employee(String name, Date DoB) { this(name, BASE_SALARY, DoB); } // more Employee code... calls the first constructor } passing in the class constant BASE_SALARY – The this keyword in a constructor must be the first line of code in the constructor. Ver. 1.0 Session 5 Slide 8 of 22
  • 9. Java Programming Language Constructors Are Not Inherited A subclass inherits all methods and variables from the superclass (parent class). A subclass does not inherit the constructor from the superclass. Two ways to include a constructor are: Use the default constructor Write one or more explicit constructors Ver. 1.0 Session 5 Slide 9 of 22
  • 10. Java Programming Language Invoking Parent Class Constructors • To invoke a parent class constructor, you must place a call to super() in the first line of the constructor. • You can call a specific parent constructor by the arguments that you use in the call to super(). • If the parent class defines constructors, but does not provide a no-argument constructor, then a compiler error message is issued. Ver. 1.0 Session 5 Slide 10 of 22
  • 11. Java Programming Language Invoking Parent Class Constructors (Contd.) An example of invoking parent class constructor: public class Manager extends Employee { private String department; public Manager(String name, double salary, String dept) { super(name, salary); department = dept; } public Manager(String name, String dept) { super(name); department = dept; } Ver. 1.0 Session 5 Slide 11 of 22
  • 12. Java Programming Language Invoking Parent Class Constructors (Contd.) public Manager(String dept) { // This code fails: no super() department = dept; } //more Manager code... } Ver. 1.0 Session 5 Slide 12 of 22
  • 13. Java Programming Language The Object Class • The Object class is the root of all classes in Java. • A class declaration with no extends clause implies extends Object. For example: public class Employee { ... } is equivalent to: public class Employee extends Object { ... } Two important methods of object class are: equals() toString() Ver. 1.0 Session 5 Slide 13 of 22
  • 14. Java Programming Language The equals Method • The == operator determines if two references are identical to each other (that is, refer to the same object). • The equals() method determines if objects are equal but not necessarily identical. • The Object implementation of the equals() method uses the == operator. • User classes can override the equals method to implement a domain-specific test for equality. Note: You should override the hashCode method if you override the equals method. Ver. 1.0 Session 5 Slide 14 of 22
  • 15. Java Programming Language The toString Method • The toString() method has the following characteristics: – This method converts an object to a String. – Use this method during string concatenation. – Override this method to provide information about a user-defined object in readable format. – Use the wrapper class’s toString() static method to convert primitive types to a String. Ver. 1.0 Session 5 Slide 15 of 22
  • 16. Java Programming Language Wrapper Classes • The Java programming language provides wrapper classes to manipulate primitive data elements as objects. • Each Java primitive data type has a corresponding wrapper class in the java.lang package. • Example of Primitive Boxing using wrapper classes: int pInt = 420; Integer wInt = new Integer(pInt); // this is called boxing int p2 = wInt.intValue(); // this is called unboxing Ver. 1.0 Session 5 Slide 16 of 22
  • 17. Java Programming Language Autoboxing of Primitive Types The autoboxing feature enables you to assign and retrieve primitive types without the need of the wrapper classes. Example of Primitive Autoboxing: int pInt = 420; Integer wInt = pInt; // this is called autoboxing int p2 = wInt; // this is called autounboxing The J2SE 5.0 compiler will create the wrapper object automatically when assigning a primitive to a variable of the wrapper class type. The compiler will also extract the primitive value when assigning from a wrapper object to a primitive variable. Ver. 1.0 Session 5 Slide 17 of 22
  • 18. Java Programming Language The static Keyword • The static keyword is used as a modifier on variables, methods, and nested classes. • The static keyword declares the attribute or method is associated with the class as a whole rather than any particular instance of that class. • Thus,static members are often called class members, such as class attributes or class methods. Ver. 1.0 Session 5 Slide 18 of 22
  • 19. Java Programming Language The static Keyword (Contd.) Static Attribute: A public static class attribute can be accessed from outside the class without an instance of the class. Static Method: A static method can be invoked without creating the instance of the class. Static methods can not access instance variables. Static Initializers: – A class can contain code in a static block that does not exist within a method body. – Static block code executes once only, when the class is loaded. – Usually, a static block is used to initialize static (class) attributes. Ver. 1.0 Session 5 Slide 19 of 22
  • 20. Java Programming Language Demonstration Let see how to declare static members in a class, including both member variables and methods. Ver. 1.0 Session 5 Slide 20 of 22
  • 21. Java Programming Language Summary In this session, you learned that: – Casting objects is used where you have received a reference to a parent class, and you want to access the full functionality of the object of the subclass. – The methods which perform closely related tasks can be given the same name by overloading them. – The varargs feature helps us to write a generic code to pass variable number of arguments, of the same type to a method. – The super keyword is used to call the constructor of the parent class. – The object class is the root of all classes. The two important methods of the object class are equals() method and tostring() method. Ver. 1.0 Session 5 Slide 21 of 22
  • 22. Java Programming Language Summary (Contd.) – Wrapper classes are used to manipulate primitive data elements as objects. Each Java primitive data type has a corresponding wrapper class in the java.lang package. – Autoboxing feature of J2SE 5.0 enables you to assign and retrieve primitive types without the need of the wrapper classes. – The static keyword declares members (attributes, methods, and nested classes) that are associated with the class rather than the instances of the class. Ver. 1.0 Session 5 Slide 22 of 22