CC2203-les03javaIII-for-viewing

CC2203-les03javaIII-for-viewing - 3-1/32Lesson 3 Java III...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3-1/32Lesson 3: Java III – More on Classes and ObjectsAfter 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 3-2/32Using the thisReferenceInstance 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: nullrating: nullmov2mov1title : nullrating: “PG”this[demonstration – javaIII.zip]3-3/32Initializing 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;3-4/32What 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.3-5/32Defining and Overloading Constructorspublic 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 Movieclassnow provides two constructors.[demonstration – javaIII.zip]3-6/32Sharing Code Between Constructorspublic 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?3-7/32What Are Class Variables?Class variables:•Belong to a class and are common to all instances of that class•Are declared as static in class definitionspublic class Movie {private static double minPrice; // class varprivate String title, rating; // inst varsMovieclass variableMovieobjectstitleratingtitleratingtitleratingminPrice[demonstration – javaIII.zip]3-8/32...
View Full Document

This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

Page1 / 32

CC2203-les03javaIII-for-viewing - 3-1/32Lesson 3 Java III...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online