SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Java Programming Language
Objectives


                In this session, you will learn to:
                   Declare and create arrays of primitive, class, or array types
                   Explain how to initialize the elements of an array
                   Determine the number of elements in an array
                   Create a multidimensional array
                   Write code to copy array values from one array to another
                   Define inheritance, polymorphism, overloading, overriding, and
                   virtual method invocation
                   Define heterogeneous collections




     Ver. 1.0                        Session 4                           Slide 1 of 26
Java Programming Language
Arrays


                Declaring Arrays:
                   Group data objects of the same type.
                   An array is an object.
                   Declare arrays of primitive or class types:
                    • char s[ ];
                    • Point p[ ];
                    • char[ ] s;
                    • Point[ ] p;
                – The declaration of an array creates space for a reference.
                – Actual memory allocation is done dynamically either by a new
                  statement or by an array initializer.




     Ver. 1.0                        Session 4                         Slide 2 of 26
Java Programming Language
Creating Arrays


                In order to create an array object:
                 – Use the new keyword
                 – An example to create and initialize a primitive (char) array:
                    public char[] createArray()
                     {
                             char[] s;
                             s = new char[26];
                             for ( int i=0; i<26; i++ )
                             {
                             s[i] = (char) (’A’ + i);
                             }
                             return s;
                     }


     Ver. 1.0                         Session 4                            Slide 3 of 26
Java Programming Language
Creating Arrays (Contd.)


                Creating an Array of Character Primitives:




     Ver. 1.0                      Session 4                 Slide 4 of 26
Java Programming Language
Creating Reference Arrays


                The following code snippet creates an array of 10
                references of type Point:
                 public Point[] createArray()
                 {
                     Point[] p;
                     p = new Point[10];
                     for ( int i=0; i<10; i++ )
                     {
                       p[i] = new Point(i, i+1);
                     }
                     return p;
                 }


     Ver. 1.0                      Session 4                        Slide 5 of 26
Java Programming Language
Creating Reference Arrays (Contd.)


                Creating an Array of Character Primitives with Point
                Objects:




     Ver. 1.0                      Session 4                           Slide 6 of 26
Java Programming Language
Demonstration


               Lets see how to declare, create, and manipulate an array of
               reference type elements.




    Ver. 1.0                         Session 4                        Slide 7 of 26
Java Programming Language
Multidimensional Arrays


                A Multidimensional array is an array of arrays.
                For example:
                 int [] [] twoDim = new int [4] [];
                 twoDim[0] = new int [5];
                 twoDim[1] = new int[5];
                 – The first call to new creates an object, an array that contains
                   four elements. Each element is a null reference to an element
                   of type array of int.
                 – Each element must be initialized separately so that each
                   element points to its array.




     Ver. 1.0                         Session 4                            Slide 8 of 26
Java Programming Language
Array Bounds


               • All array subscripts begin at 0.
               • The number of elements in an array is stored as part of the
                 array object in the length attribute.
               • The following code uses the length attribute to iterate on
                 an array:
                  public void printElements(int[] list)
                  {
                       for (int i = 0; i < list.length; i++)
                       {
                           System.out.println(list[i]);
                       }
                   }

    Ver. 1.0                         Session 4                        Slide 9 of 26
Java Programming Language
The Enhanced for Loop


                • Java 2 Platform, Standard Edition (J2SE™) version 5.0 has
                  introduced an enhanced for loop for iterating over arrays:
                   public void printElements(int[] list)
                   {
                        for ( int element : list )
                      {
                          System.out.println(element);
                      }
                   }
                   – The for loop can be read as for each element in list do.




     Ver. 1.0                          Session 4                           Slide 10 of 26
Java Programming Language
Array Resizing


                You cannot resize an array.
                You can use the same reference variable to refer to an
                entirely new array, such as:
                 int[] myArray = new int[6];
                 myArray = new int[10];
                   In the preceding case, the first array is effectively lost unless
                   another reference to it is retained elsewhere.




     Ver. 1.0                         Session 4                               Slide 11 of 26
Java Programming Language
Copying Arrays


                • The Java programming language provides a special method
                  in the System class, arraycopy(), to copy arrays.
                  For example:
                   int myarray[] = {1,2,3,4,5,6}; // original array
                   int hold[] = {10,9,8,7,6,5,4,3,2,1}; // new
                   larger array
                   System.arraycopy(myarray,0,hold,0,
                   myarray.length); // copy all of the myarray array to
                   the hold array, starting with the 0th index
                   – The contents of the array hold will be : 1,2,3,4,5,6,4,3,2,1.
                   – The System.arraycopy() method copies references, not
                     objects, when dealing with array of objects.




     Ver. 1.0                          Session 4                             Slide 12 of 26
Java Programming Language
Inheritance


                Inheritance means that a class derives a set of attributes
                and related behavior from a parent class.
                Benefits of Inheritance:
                   Reduces redundancy in code
                   Code can be easily maintained
                   Extends the functionality of an existing class




     Ver. 1.0                        Session 4                        Slide 13 of 26
Java Programming Language
Inheritance (Contd.)


                Single Inheritance
                   The subclasses are derived from one super class.
                   An example of single inheritance is as follows:




     Ver. 1.0                        Session 4                        Slide 14 of 26
Java Programming Language
Inheritance (Contd.)


                Java does not support multiple inheritance.
                Interfaces provide the benefits of multiple inheritance
                without drawbacks.
                Syntax of a Java class in order to implement inheritance is
                as follows:
                 <modifier> class <name> [extends
                 superclass>]
                 {     <declaration>*         }




     Ver. 1.0                      Session 4                         Slide 15 of 26
Java Programming Language
Access Control


                Variables and methods can be at one of the following four
                access levels:
                   public
                   protected
                   default
                   private
                Classes can be at the public or default levels.
                The default accessibility (if not specified explicitly), is
                package-friendly or package-private.




     Ver. 1.0                         Session 4                               Slide 16 of 26
Java Programming Language
Overriding Methods


                A subclass can modify behavior inherited from a parent
                class.
                Overridden methods cannot be less accessible.
                A subclass can create a method with different functionality
                than the parent’s method but with the same:
                   Name
                   Return type
                   Argument list




     Ver. 1.0                      Session 4                         Slide 17 of 26
Java Programming Language
Overriding Methods (Contd.)


                • A subclass method may invoke a superclass method using
                  the super keyword:
                   – The keyword super is used in a class to refer to its
                     superclass.
                   – The keyword super is used to refer to the members of
                     superclass, both data attributes and methods.
                   – Behavior invoked does not have to be in the superclass; it can
                     be further up in the hierarchy.




     Ver. 1.0                          Session 4                           Slide 18 of 26
Java Programming Language
Overriding Methods (Contd.)


                • Invoking Overridden Methods using super keyword:
                    public class Employee
                    {
                       private String name;
                       private double salary;
                       private Date birthDate;
                       public String getDetails()
                       {
                         return "Name: " + name +
                         "nSalary: “ + salary;
                       }
                     }


     Ver. 1.0                       Session 4                        Slide 19 of 26
Java Programming Language
Overriding Methods (Contd.)


                public class Manager extends Employee
                {
                      private String department;
                      public String getDetails() {
                      // call parent method
                      return super.getDetails()
                      + “nDepartment: " + department;
                      }
                  }




     Ver. 1.0                 Session 4            Slide 20 of 26
Java Programming Language
Demonstration


               Lets see how to create subclasses and call the constructor of
               the base class.




    Ver. 1.0                         Session 4                        Slide 21 of 26
Java Programming Language
Polymorphism


               Polymorphism is the ability to have many different forms;
               for example, the Manager class has access to methods
               from Employee class.
                  An object has only one form.
                  A reference variable can refer to objects of different forms.
                  Java programming language permits you to refer to an object
                  with a variable of one of the parent class types.
                  For example:
                  Employee e = new Manager(); // legal




    Ver. 1.0                        Session 4                           Slide 22 of 26
Java Programming Language
Virtual Method Invocation


                Virtual method invocation is performed as follows:
                 Employee e = new Manager();
                 e.getDetails();
                   Compile-time type and runtime type invocations have the
                   following characteristics:
                      The method name must be a member of the declared variable
                      type; in this case Employee has a method called getDetails.
                      The method implementation used is based on the runtime object’s
                      type; in this case the Manager class has an implementation of the
                      getDetails method.




     Ver. 1.0                         Session 4                               Slide 23 of 26
Java Programming Language
Heterogeneous Collections


                Heterogeneous Collections:
                   Collections of objects with the same class type are called
                   homogeneous collections. For example:
                    MyDate[] dates = new MyDate[2];
                    dates[0] = new MyDate(22, 12, 1964);
                    dates[1] = new MyDate(22, 7, 1964);
                   Collections of objects with different class types are called
                   heterogeneous collections. For example:
                    Employee [] staff = new Employee[1024];
                    staff[0] = new Manager();
                    staff[1] = new Employee();
                    staff[2] = new Engineer();




     Ver. 1.0                        Session 4                              Slide 24 of 26
Java Programming Language
Summary


               In this session, you learned that:
                – Arrays are objects used to group data objects of the same
                  type. Arrays can be of primitive or class type.
                – Arrays can be created by using the keyword new.
                – A multidimensional array is an array of arrays.
                – All array indices begin at 0. The number of elements in an
                  array is stored as part of the array object in the length
                  attribute.
                – An array once created can not be resized. However the same
                  reference variable can be used to refer to an entirely new
                  array.
                – The Java programming language permits a class to extend one
                  other class i.e, single inheritance.




    Ver. 1.0                       Session 4                         Slide 25 of 26
Java Programming Language
Summary (Contd.)


               Variables and methods can be at one of the four access levels:
               public, protected, default, or private.
               Classes can be at the public or default level.
               The existing behavior of a base class can be modified by
               overriding the methods of the base class.
               A subclass method may invoke a superclass method using the
               super keyword.
               Polymorphism is the ability to have many different forms; for
               example, the Manager class (derived) has the access to
               methods from Employee class (base).
               Collections of objects with different class types are called
               heterogeneous collections.




    Ver. 1.0                    Session 4                           Slide 26 of 26

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Java basic
Java basicJava basic
Java basic
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
Java Reference
Java ReferenceJava Reference
Java Reference
 
Java Notes
Java Notes Java Notes
Java Notes
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Core java
Core javaCore java
Core java
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Java OOP Concepts 1st Slide
Java OOP Concepts 1st SlideJava OOP Concepts 1st Slide
Java OOP Concepts 1st Slide
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Core java
Core java Core java
Core java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 

Andere mochten auch

CV Vega Sasangka Edi, S.T. M.M
CV Vega Sasangka Edi, S.T. M.MCV Vega Sasangka Edi, S.T. M.M
CV Vega Sasangka Edi, S.T. M.MVega Sasangka
 
Advice to assist you pack like a pro in addition
Advice to assist you pack like a pro in additionAdvice to assist you pack like a pro in addition
Advice to assist you pack like a pro in additionSloane Beck
 
การลอกลาย
การลอกลายการลอกลาย
การลอกลายjimmot
 
What about your airport look?
What about your airport look?What about your airport look?
What about your airport look?Sloane Beck
 
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostrove
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostroveStrety záujmov pri ťažbe štrkopieskov na Žitnom ostrove
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostroveJozef Luprich
 
Brazilian Waxing in Shanghai - An Analysis of Strip's Strategy
Brazilian Waxing in Shanghai - An Analysis of Strip's StrategyBrazilian Waxing in Shanghai - An Analysis of Strip's Strategy
Brazilian Waxing in Shanghai - An Analysis of Strip's StrategyFuiyi Chong
 
Winter Packing Tips
Winter Packing TipsWinter Packing Tips
Winter Packing TipsSloane Beck
 
Lulu Zhang_Resume
Lulu Zhang_ResumeLulu Zhang_Resume
Lulu Zhang_ResumeLulu Zhang
 
Teamwork Healthcare work
Teamwork Healthcare work Teamwork Healthcare work
Teamwork Healthcare work kamalteamwork
 
Professional Portfolio Presentation
Professional Portfolio PresentationProfessional Portfolio Presentation
Professional Portfolio Presentationsheeter
 
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
 
Toolkit for supporting social innovation with the ESIF
Toolkit for supporting social innovation with the ESIFToolkit for supporting social innovation with the ESIF
Toolkit for supporting social innovation with the ESIFESF Vlaanderen
 

Andere mochten auch (20)

CV Vega Sasangka Edi, S.T. M.M
CV Vega Sasangka Edi, S.T. M.MCV Vega Sasangka Edi, S.T. M.M
CV Vega Sasangka Edi, S.T. M.M
 
Advice to assist you pack like a pro in addition
Advice to assist you pack like a pro in additionAdvice to assist you pack like a pro in addition
Advice to assist you pack like a pro in addition
 
Terri Kirkland Resume
Terri Kirkland ResumeTerri Kirkland Resume
Terri Kirkland Resume
 
การลอกลาย
การลอกลายการลอกลาย
การลอกลาย
 
What about your airport look?
What about your airport look?What about your airport look?
What about your airport look?
 
Marriage
MarriageMarriage
Marriage
 
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostrove
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostroveStrety záujmov pri ťažbe štrkopieskov na Žitnom ostrove
Strety záujmov pri ťažbe štrkopieskov na Žitnom ostrove
 
Brazilian Waxing in Shanghai - An Analysis of Strip's Strategy
Brazilian Waxing in Shanghai - An Analysis of Strip's StrategyBrazilian Waxing in Shanghai - An Analysis of Strip's Strategy
Brazilian Waxing in Shanghai - An Analysis of Strip's Strategy
 
Winter Packing Tips
Winter Packing TipsWinter Packing Tips
Winter Packing Tips
 
[iD]DNA® SKIN AGING - DNAge abstract sample report
[iD]DNA® SKIN AGING - DNAge abstract sample report[iD]DNA® SKIN AGING - DNAge abstract sample report
[iD]DNA® SKIN AGING - DNAge abstract sample report
 
Features of java
Features of javaFeatures of java
Features of java
 
Java features
Java featuresJava features
Java features
 
Lulu Zhang_Resume
Lulu Zhang_ResumeLulu Zhang_Resume
Lulu Zhang_Resume
 
Teamwork Healthcare work
Teamwork Healthcare work Teamwork Healthcare work
Teamwork Healthcare work
 
Systematic error bias
Systematic error  biasSystematic error  bias
Systematic error bias
 
Professional Portfolio Presentation
Professional Portfolio PresentationProfessional Portfolio Presentation
Professional Portfolio Presentation
 
Macro
MacroMacro
Macro
 
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
 
Toolkit for supporting social innovation with the ESIF
Toolkit for supporting social innovation with the ESIFToolkit for supporting social innovation with the ESIF
Toolkit for supporting social innovation with the ESIF
 
Lm wellness massage g10
Lm wellness massage g10Lm wellness massage g10
Lm wellness massage g10
 

Ähnlich wie Java session04

JAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptxJAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptxfwzmypc2004
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdfamitbhachne
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic classifis
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysWebStackAcademy
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introductionchnrketan
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
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
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsAnton Keks
 

Ähnlich wie Java session04 (20)

CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptxJAVA INETRNSHIP1 made with simple topics.ppt.pptx
JAVA INETRNSHIP1 made with simple topics.ppt.pptx
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
 
Core_java_ppt.ppt
Core_java_ppt.pptCore_java_ppt.ppt
Core_java_ppt.ppt
 
06slide
06slide06slide
06slide
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Core Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - ArraysCore Java Programming Language (JSE) : Chapter V - Arrays
Core Java Programming Language (JSE) : Chapter V - Arrays
 
Java programing language unit 1 introduction
Java programing language unit 1 introductionJava programing language unit 1 introduction
Java programing language unit 1 introduction
 
Arrays in java.pptx
Arrays in java.pptxArrays in java.pptx
Arrays in java.pptx
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
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
 
Java Course 4: Exceptions & Collections
Java Course 4: Exceptions & CollectionsJava Course 4: Exceptions & Collections
Java Course 4: Exceptions & Collections
 
JVM
JVMJVM
JVM
 
Java quickref
Java quickrefJava quickref
Java quickref
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
JavaYDL6
JavaYDL6JavaYDL6
JavaYDL6
 

Mehr von 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
 

Kürzlich hochgeladen

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 

Kürzlich hochgeladen (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Java session04

  • 1. Java Programming Language Objectives In this session, you will learn to: Declare and create arrays of primitive, class, or array types Explain how to initialize the elements of an array Determine the number of elements in an array Create a multidimensional array Write code to copy array values from one array to another Define inheritance, polymorphism, overloading, overriding, and virtual method invocation Define heterogeneous collections Ver. 1.0 Session 4 Slide 1 of 26
  • 2. Java Programming Language Arrays Declaring Arrays: Group data objects of the same type. An array is an object. Declare arrays of primitive or class types: • char s[ ]; • Point p[ ]; • char[ ] s; • Point[ ] p; – The declaration of an array creates space for a reference. – Actual memory allocation is done dynamically either by a new statement or by an array initializer. Ver. 1.0 Session 4 Slide 2 of 26
  • 3. Java Programming Language Creating Arrays In order to create an array object: – Use the new keyword – An example to create and initialize a primitive (char) array: public char[] createArray() { char[] s; s = new char[26]; for ( int i=0; i<26; i++ ) { s[i] = (char) (’A’ + i); } return s; } Ver. 1.0 Session 4 Slide 3 of 26
  • 4. Java Programming Language Creating Arrays (Contd.) Creating an Array of Character Primitives: Ver. 1.0 Session 4 Slide 4 of 26
  • 5. Java Programming Language Creating Reference Arrays The following code snippet creates an array of 10 references of type Point: public Point[] createArray() { Point[] p; p = new Point[10]; for ( int i=0; i<10; i++ ) { p[i] = new Point(i, i+1); } return p; } Ver. 1.0 Session 4 Slide 5 of 26
  • 6. Java Programming Language Creating Reference Arrays (Contd.) Creating an Array of Character Primitives with Point Objects: Ver. 1.0 Session 4 Slide 6 of 26
  • 7. Java Programming Language Demonstration Lets see how to declare, create, and manipulate an array of reference type elements. Ver. 1.0 Session 4 Slide 7 of 26
  • 8. Java Programming Language Multidimensional Arrays A Multidimensional array is an array of arrays. For example: int [] [] twoDim = new int [4] []; twoDim[0] = new int [5]; twoDim[1] = new int[5]; – The first call to new creates an object, an array that contains four elements. Each element is a null reference to an element of type array of int. – Each element must be initialized separately so that each element points to its array. Ver. 1.0 Session 4 Slide 8 of 26
  • 9. Java Programming Language Array Bounds • All array subscripts begin at 0. • The number of elements in an array is stored as part of the array object in the length attribute. • The following code uses the length attribute to iterate on an array: public void printElements(int[] list) { for (int i = 0; i < list.length; i++) { System.out.println(list[i]); } } Ver. 1.0 Session 4 Slide 9 of 26
  • 10. Java Programming Language The Enhanced for Loop • Java 2 Platform, Standard Edition (J2SE™) version 5.0 has introduced an enhanced for loop for iterating over arrays: public void printElements(int[] list) { for ( int element : list ) { System.out.println(element); } } – The for loop can be read as for each element in list do. Ver. 1.0 Session 4 Slide 10 of 26
  • 11. Java Programming Language Array Resizing You cannot resize an array. You can use the same reference variable to refer to an entirely new array, such as: int[] myArray = new int[6]; myArray = new int[10]; In the preceding case, the first array is effectively lost unless another reference to it is retained elsewhere. Ver. 1.0 Session 4 Slide 11 of 26
  • 12. Java Programming Language Copying Arrays • The Java programming language provides a special method in the System class, arraycopy(), to copy arrays. For example: int myarray[] = {1,2,3,4,5,6}; // original array int hold[] = {10,9,8,7,6,5,4,3,2,1}; // new larger array System.arraycopy(myarray,0,hold,0, myarray.length); // copy all of the myarray array to the hold array, starting with the 0th index – The contents of the array hold will be : 1,2,3,4,5,6,4,3,2,1. – The System.arraycopy() method copies references, not objects, when dealing with array of objects. Ver. 1.0 Session 4 Slide 12 of 26
  • 13. Java Programming Language Inheritance Inheritance means that a class derives a set of attributes and related behavior from a parent class. Benefits of Inheritance: Reduces redundancy in code Code can be easily maintained Extends the functionality of an existing class Ver. 1.0 Session 4 Slide 13 of 26
  • 14. Java Programming Language Inheritance (Contd.) Single Inheritance The subclasses are derived from one super class. An example of single inheritance is as follows: Ver. 1.0 Session 4 Slide 14 of 26
  • 15. Java Programming Language Inheritance (Contd.) Java does not support multiple inheritance. Interfaces provide the benefits of multiple inheritance without drawbacks. Syntax of a Java class in order to implement inheritance is as follows: <modifier> class <name> [extends superclass>] { <declaration>* } Ver. 1.0 Session 4 Slide 15 of 26
  • 16. Java Programming Language Access Control Variables and methods can be at one of the following four access levels: public protected default private Classes can be at the public or default levels. The default accessibility (if not specified explicitly), is package-friendly or package-private. Ver. 1.0 Session 4 Slide 16 of 26
  • 17. Java Programming Language Overriding Methods A subclass can modify behavior inherited from a parent class. Overridden methods cannot be less accessible. A subclass can create a method with different functionality than the parent’s method but with the same: Name Return type Argument list Ver. 1.0 Session 4 Slide 17 of 26
  • 18. Java Programming Language Overriding Methods (Contd.) • A subclass method may invoke a superclass method using the super keyword: – The keyword super is used in a class to refer to its superclass. – The keyword super is used to refer to the members of superclass, both data attributes and methods. – Behavior invoked does not have to be in the superclass; it can be further up in the hierarchy. Ver. 1.0 Session 4 Slide 18 of 26
  • 19. Java Programming Language Overriding Methods (Contd.) • Invoking Overridden Methods using super keyword: public class Employee { private String name; private double salary; private Date birthDate; public String getDetails() { return "Name: " + name + "nSalary: “ + salary; } } Ver. 1.0 Session 4 Slide 19 of 26
  • 20. Java Programming Language Overriding Methods (Contd.) public class Manager extends Employee { private String department; public String getDetails() { // call parent method return super.getDetails() + “nDepartment: " + department; } } Ver. 1.0 Session 4 Slide 20 of 26
  • 21. Java Programming Language Demonstration Lets see how to create subclasses and call the constructor of the base class. Ver. 1.0 Session 4 Slide 21 of 26
  • 22. Java Programming Language Polymorphism Polymorphism is the ability to have many different forms; for example, the Manager class has access to methods from Employee class. An object has only one form. A reference variable can refer to objects of different forms. Java programming language permits you to refer to an object with a variable of one of the parent class types. For example: Employee e = new Manager(); // legal Ver. 1.0 Session 4 Slide 22 of 26
  • 23. Java Programming Language Virtual Method Invocation Virtual method invocation is performed as follows: Employee e = new Manager(); e.getDetails(); Compile-time type and runtime type invocations have the following characteristics: The method name must be a member of the declared variable type; in this case Employee has a method called getDetails. The method implementation used is based on the runtime object’s type; in this case the Manager class has an implementation of the getDetails method. Ver. 1.0 Session 4 Slide 23 of 26
  • 24. Java Programming Language Heterogeneous Collections Heterogeneous Collections: Collections of objects with the same class type are called homogeneous collections. For example: MyDate[] dates = new MyDate[2]; dates[0] = new MyDate(22, 12, 1964); dates[1] = new MyDate(22, 7, 1964); Collections of objects with different class types are called heterogeneous collections. For example: Employee [] staff = new Employee[1024]; staff[0] = new Manager(); staff[1] = new Employee(); staff[2] = new Engineer(); Ver. 1.0 Session 4 Slide 24 of 26
  • 25. Java Programming Language Summary In this session, you learned that: – Arrays are objects used to group data objects of the same type. Arrays can be of primitive or class type. – Arrays can be created by using the keyword new. – A multidimensional array is an array of arrays. – All array indices begin at 0. The number of elements in an array is stored as part of the array object in the length attribute. – An array once created can not be resized. However the same reference variable can be used to refer to an entirely new array. – The Java programming language permits a class to extend one other class i.e, single inheritance. Ver. 1.0 Session 4 Slide 25 of 26
  • 26. Java Programming Language Summary (Contd.) Variables and methods can be at one of the four access levels: public, protected, default, or private. Classes can be at the public or default level. The existing behavior of a base class can be modified by overriding the methods of the base class. A subclass method may invoke a superclass method using the super keyword. Polymorphism is the ability to have many different forms; for example, the Manager class (derived) has the access to methods from Employee class (base). Collections of objects with different class types are called heterogeneous collections. Ver. 1.0 Session 4 Slide 26 of 26