Static methods belong to the class as a whole not the objects in particular

Static methods belong to the class as a whole not the

This preview shows page 2 - 5 out of 7 pages.

Static methods belong to the class as a whole, not the objects in particular Some behavior does not depend on object's state Some data are not state of an object, e.g.,static double Math.PI Method Main Application: Java program with such a method Execution begins with the method main Modifier Final Used with field or local variable Final variable cannot be changed Useful for constants, often use names in all caps Exceptions Used when something has gone wrong An assertion fails
Image of page 2
An argument is illegal, so nothing can be returned A method is invoked, but there's no object to receive the message To raise a concern: throw Exceptions are objects throw new IllegalArgumentException() To handle it: try...catch… try { code that could cause exception to be thrown } catch (ExceptionClass e) { code that handles it } Inheritance 2/4 public class InterestAccount extends Account { private double interestRate; public InterestAccount(String num, double rate) { super(num); interestRate= rate; } ... } Subclassing public class InterestAccount extends Account A class has at most one direct superclass A should extend B if and only if A “is a” B Subclasses add partitions to folder Inheritance A subclass inherits the fields and methods of its superclass Enables reuse A subclass can override methods of its superclass Enables customization Overriding @Override Annotation placed on method to indicate intent to override (not just accidental) super. Invoke method using bottom-up rule, but start looking in partition above Any class that does not explicitly extend another class automatically extends class Object private string number this.number = number Delegation Can delegate within class: this(args) Can delegate to superclass: super(args) If omitted, delegates to superclass: super() Initialization Order Every partition invokes super constructor before doing its own work Initialization is therefore top down Polymorphism 2/6 Polymorphism: language treats as though same, despite differences
Image of page 3
General phenomenon with three occurrences in Java: Ad-hoc polymorphism Parametric polymorphism Subtype polymorphism Ad-hoc polymorphism A method/operator appears to be applicable to several different types Those types need not share a common structure Method/operator may behave in unrelated ways for each type ex: overloading (same method/operator has “many forms”), autoboxing Java Wrapper Classes Wrapper class: wrap a value of a primitive type inside an object (built-in; not defined by a class) Java automatically coerces between value of primitive type and object of wrapper class Autoboxing: same value has two forms Parametric Polymorphism public class Box <T> {...} Constructor: public Box(T t) { put(t); } Cannot parameterize primitive types
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 7 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors