1 / 27

Programming with Java

Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java. Java API. The Java Library. 3793 pre-written Java classes Organized into packages Package is part of the full name java.util.ArrayList Must use the full name

lucio
Download Presentation

Programming with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor

  2. Programming with Java Java API

  3. The Java Library • 3793 pre-written Java classes • Organized into packages • Package is part of the full name • java.util.ArrayList • Must use the full name • Type it out each time you use it • Use an import statement • import java.util;

  4. Finding API Classes • Option one: get a book • O’Reilly “Java in a Nutshell” • Oracle “Java The Complete Reference” • Option two: online documentation • http://docs.oracle.com/javase/7/docs/api/

  5. Key Java API Packages java.lang – classes fundamental to the language java.util – utility classes javax.swing – Swing GUI classes java.awt – UI and graphics java.io – system input and output java.math – arbitrary precision arithmetic

  6. Wrapper Classes • Found in java.lang • “Wrap" primitive values in an object • primitives can be included in activities reserved for objects • such as being added to collections • or returned from a method with an object return value. • Provide an assortment of utility functions • converting primitives to and from String objects, • converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

  7. Wrapper Classes

  8. Wrapper Class Heirarchy

  9. Common Wrapper Methods parseInt(s) - returns a signed decimal integer value equivalent to string s toString(i) - returns a new String object representing the integer i byteValue() - returns the value of this Integer as a byte doubleValue() - returns the value of this Integer as a double floatValue() - returns the value of this Integer as a float intValue() - returns the value of this Integer as an int shortValue() - returns the value of this Integer as a short longValue() - returns the value of this Integer as a long intcompareTo(inti) - Compares the numerical value of the invoking object with that of i. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. static int compare(int num1, int num2) - Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2. booleanequals(Object intObj) - Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it returns false.

  10. Programming with Java Inheritance

  11. Object Orientation • Encapsulation • Data (instance variables, attributes) is kept private • Access is through public methods • Accessors or ‘getters’ • Mutatorsor ‘setters’ • Helper methods are kept private • Inheritance • Polymorphism • Abstraction

  12. Inheritance Derive a new class from an existing one Existing class is called the parent class,superclass, or base class Derived class is called the child class or subclass. Child inherits characteristics of the parent Child class inherits the methods and data defined for the parent class Can add new variables or methods, or can modify the inherited ones

  13. Visibility Revisited All variables and methods of a parent class, even private members, are inherited by its children Private members cannot be referenced by name in the child class Private members inherited by child classes exist and can be referenced indirectly Because the parent can refer to the private member, the child can reference it indirectly using its parent's methods The super reference can be used to refer to the parent class, even if no object of the parent class exists

  14. Implementation Inheritance should create an is-arelationship Child is a more specific version of the parent Subclass extends the superclass Subclass can add methods and instance variables Subclass can override methods from the superclass Subclass can only extend (inherit from) a single superclass Subclass inherits from all superclasses in the hierarchy tree

  15. Using extends public class Dog extends Animal { . . . }

  16. Override • A subclass can override (redefine) the methods of the superclass • Objects of the subclass type will use the new method • Objects of the superclass type will use the original • Applies to methods, not instance variables • Changes the definition of a method • Doesn’t change the parameter list • Doesn’t change the type • Can’t be less accessible • Can incorporate the superclass via super

  17. Using super public class Pekinese extends Dog{ void makeNoise () { super.makeNoise(); addPanting(); }

  18. Programming with Java Polymorphism

  19. Polymorphism When a program invokes a method through a superclass variable the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable The same method name and signature can cause different actions to occur depending on the type of object on which the method is invoked Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics. Objects to behave in manners appropriate to those objects as long as the objects belong to the same inheritance hierarchy.

  20. Principles A (public) method defined in a class is inherited by all descendants of that class When a message is sent to an object to use method m(), any messages that m() sends will also be sent to the same object If the object receiving a message does not have a definition of the method requested, an inherited definition is invoked If the object receiving a message has a definition of the requested method, that definition is invoked

  21. Inheritance Tree

  22. Polymorphism & Inheritance public class Shape { . . .} public class Rectangle extends Shape { . . .} public class Square extends Rectangle { . . .} public class Circle extends Shape { . . .} public class ShapeShifter { public static void main (String [ ] args) { Shape [ ] shapeList = new Shape[5]; shapeList[0] = new Circle(3.0); shapeList[1] = new Rectangle(3.0, 4.0); shapeList[2] = new Rectangle(2.5, 7.5); shapeList[3] = new Circle(2.5); shapeList[4] = new Square(5.0); for (int i = 0; i < shapeList.length; i++) { System.out.print(shapeList[i].toString( ) + “ ”); System.out.print(shapeList[i].area( ) + “ ”); System.out.println(shapeList[i].perimeter( )); } } }

  23. Polymorphic Arguments & Returns class Vet { public void giveShot (Animal a){ // . . . // a.makeNoise(); } } class PetOwner { public void start { Vet v = new Vet(); Dog d = new Dog(); Cat c = new Cat(); v.giveShot(d); v.giveShot (c); } }

  24. Overloading • Overload ≠ override • Two methods with the same name, different argument lists • Not polymorphic • Argument lists can be different • Return types can be different • But only if the argument list is changed • Access levels can change in any direction

  25. Overloading Example class Calculator {   // instance variables go here       public void add(int i, int j) {   System.out.println("Integer addition");      }      public void add(int i, double j) {   System.out.println("Integer and double addition");   }      public void add(double i, double j) {   System.out.println("Double addition");      }   }  

  26. Basic Constructors Create an instance of a class No constructor implies default constructor Access modifiers: public, protected, private, or none No return type including void Exact same name as the class Can be overloaded

  27. Default Constructor public class Example {} is functionally equivalent to public class Example { Example() { super; } } • The compiler automatically supplies • a no-argument constructor if no constructor is explicitly declared and • a no-argument supercall when a constructor has no explicit call to super.

More Related