CIS 160 - Classes and objects

Objectives

  • Create and use classes
  • Create and use objects
  • Discuss and examine access modifiers (public, private, protected, (package))
  • Discuss and examine static vs. instance
  • Use method overloading
  • Use method overriding

Creating a class

  • syntax for class definition:
    [modifiers] class Class_name [modifiers] { [class members] }
  • Access modifiers: public, private, protected, (package)
  • Multiple methods within a class may have the same name if they have parameter lists that differ substantively within the parameter list (called method overloading).
  • Variables declared within a method can only be seen within a method. These are called local variables.
  • Variables declared outside of a method, but within a class, can be seen by all methods in that class. These are called global variables.
  • Members within a class may be static or instance. If they are not declared as static, then they are instance. Instance members exist only with reference to an object created from the class. Static members exist with relation to the original class, so they have only one copy and exist before any objects have been created from that class.
  • Class/instance data members are often declared private and access is then allowed only through methods. This allows the class to validate and control all access to its internal data.
  • Data hiding is considered good, in part because it helps hide irrelevant details of how a class works internally, and thus reduces complexity.

Creating an object

  • Objects are created from classes using the new operator.
  • Constructors are called when an object is being created. The constructor called depends on the parameters used.
  • Constructors look like methods, but they do not have return data types.
  • The default constructor is the constructor that has no parameters/arguments. If a class has no defined constructors, then Java will create a default constructor automatically. If you define a constructor for a class, then Java will NOT create the default constructor for you.
  • Classes that extend another object are said to be a subclass of the object they are extending. The parent is then that object's superclass. The superclass is also referred to as the base class, while the subclass is called a derived class. This is done using the extends keyword in the class definition.
  • Subclasses inherit the data fields and methods of the parent (but not the constructors).

Using an object

  • Members of an object can be referenced using the "member-of" (dot) operator: objectReference.member
  • Data elements in an object or class are often kept private. Accessor methods are those methods that allow other objects to view hidden data within a class/object. Mutator methods are those methods that allow other objects to modify hidden data within a class/object.
  • Creating a method in a subclass with the same signature as the superclass is called overriding. The superclass method is then hidden from view (but may be accessed using the super reference.
  • Creating two methods with different signatures within a class is called overloading.
  • If a method has a local variable with the same name as a class variable, then the class variable is hidden, but may be accessed using the this reference.

Examples

class A { private static int count; // one copy shared by all A objects public int n1; protected int n2; private int n3; // only A has access to private members public A() { this(0, 0, 0); // calls other A constructor } public A(int a, int b, int c) { incrementCount(); n1 = a; n2 = b; setN3(c); } private void incrementCount() { count++; } public void setN3(int x) { if (x < 1) n3 = 1; else n3 = x; } public int getN3() { return n3; } public static int getCount() { return count; } } class B extends A { // B has inherited fields and methods from class A public double n4; public B() { super(7, 8, 9); // calls constructor for A n4 = 3.14; } public B(int i, int j, int k, double d) { super(i, j, k); // calls constructor for A n4 = d; } } A obj1 = new A(); B obj2 = new B(); B obj3 = new B(10, 20, 30, 5.2); // obj1.getCount() would return 3 // obj2.getCount() would return 3 // obj3.getCount() would return 3 // A.getCount() would return 3 // B.getCount() would return 3 // obj1.n1 would return 0 // obj2.n1 would return 7 // obj3.n1 would return 10 // obj1.n2 would return 0 // obj2.n2 would return 8 (if it works) // obj3.n2 would return 20 (if it works) // obj1.n3 would not work since n3 is private // obj2.n3 would not work since n3 is private // obj3.n3 would not work since n3 is private // obj1.getN3() would return 0 // obj2.getN3() would return 9 // obj3.getN3() would return 30 // obj1.n4 would not work // obj2.n4 would return 3.14 // obj3.n4 would return 5.2

Resource