CC2203-les03javaIII-for-viewing - Lesson 3 Java III More on...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
3-1/32 Lesson 3: Java III – More on Classes and Objects After completing this lesson, you should be able to do the following: Provide one or more constructors for a class Use initializers to initialize both instance and class variables Use inheritance to define new classes Override methods in the superclass Define abstract classes Define abstract methods Define interfaces Implement interfaces
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3-2/32 Using the this Reference Instance methods receive an argument called this , which refers to the current object. public class Movie { public void setRating(String newRating) { this.rating = newRating; } void anyMethod() { Movie mov1 = new Movie(); Movie mov2 = new Movie(); mov1.setRating("PG"); … title: null rating: null mov2 mov1 title : null rating: “PG” this [demonstration – javaIII.zip]
Image of page 2
3-3/32 Initializing Instance Variables Instance variables can be explicitly initialized at declaration. Initialization happens at object creation. All instance variables are initialized implicitly to default values depending on data type. More complex initialization should be placed in a constructor. public class Movie { private String title; private String rating = "G"; private int numOfOscars = 0;
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3-4/32 What Are Constructors? For proper initialization, a class should provide a constructor. A constructor is called automatically when an object is created: Usually declared public Has the same name as the class No specified return type The compiler supplies a no-arg constructor if and only if a constructor is not explicitly provided. If any constructor is explicitly provided, then the compiler does not generate the no-arg constructor.
Image of page 4
3-5/32 Defining and Overloading Constructors public class Movie { private String title; private String rating = "PG"; public Movie() { title = "Last Action "; } public Movie(String newTitle) { title = newTitle; } } Movie mov1 = new Movie(); Movie mov2 = new Movie("Gone "); Movie mov3 = new Movie("The Good "); The Movie class now provides two constructors. [demonstration – javaIII.zip]
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3-6/32 Sharing Code Between Constructors public class Movie { private String title; private String rating; public Movie() { this("G"); } public Movie(String newRating) { rating = newRating; } } A constructor can call another constructor by using this() . Movie mov2 = new Movie(); What happens here?
Image of page 6
3-7/32 What Are Class Variables?
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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