CIS 160 - Object-oriented programming

Objectives

  • Compare and contrast composition and inheritance
  • Explain polymorphism
  • Compare and contrast access modifiers
  • Create and use abstract methods

Object-oriented programming

The topics here are presented as just an outline of discussion points. We will discuss them in much greater detail in class.

  • access modifiers: public, private, protected, (package)
  • constructors: both default and others
  • static versus instance variables/fields
  • static versus instance methods
  • composition versus inheritance
  • superclass constructor calls
  • superclass method calls
  • the "this" keyword
  • polymorphism
  • abstract methods
  • interfaces

Access modifiers

  1. public access: members of the same class, same package, subclasses and other classes all have access
  2. protected access: members of the same class, same package, and subclasses have access
  3. (package-private) access: members of the same class and classes in the same package have access
  4. private access: only members of the same class have access
  5. Note: There is no access modifier keyword for package-private. It is specified by having no access modifier present.
  6. Note: Only one top-level (un-nested) public class is allowed per source file, but there can be other classes in the source file.

Constructors

  1. Constructors look a lot like methods, but they have no return data type and they must have the same name as the class they are in.
  2. A constructor is called when an object is being created (instantiated).
  3. One of the primary functions of a constructor is to initialize an object's instance variables.
  4. "Default constructor" is the name given to a constructor that takes no arguments.
  5. Example of calling a default constructor: Employee emp = new Employee();
  6. Example of calling a non-default constructor: Employee emp = new Employee("John Smith");
  7. If you write no constructors for a class, Java will automatically create a default constructor for that class.
  8. If you write a constructor for a class, Java will NOT write a default constructor for that class.
  9. The first statement in a constructor will be a call to its superclass constructor. You do not use the parent class name in that call, but rather the keyword "super".
  10. If you do not call the superclass constructor as the first statement in a constructor, then Java will add a call to the superclass default constructor automatically. If you need to call a non-default superclass constructor, then you have to write it explicitly as the first statement in a constructor.
  11. Calls to a superclass constructor are only allowed as the first statement in a constructor.
  12. Example of calling the superclass default constructor: super();
  13. Example of calling a superclass non-default constructor: super(17, true);
  14. One constructor can call another in Java using the "this" keyword. This is commonly done when a constructor with less arguments calls a constructor with more arguments and fills in some default values for extra arguments.
  15. Example of using "this" in a constructor call: public class MyClass { int n; public MyClass() { this(5); } public MyClass(int a) { n = a; } }

Static versus instance variables/fields

  1. Variables/fields declared outside of any methods are either static or instance.
  2. If a variable/field is not declared as "static", then it is automatically "instance".
  3. There is no "instance" keyword.
  4. Static and instance have no meaning with regard to variables declared inside methods.
  5. There is only one copy of a static variable/field. It is associated with the class. All objects created from that class share the single copy of that variable. If it is modified, then all objects and the class reflect that modification (because there is only one).
  6. Static variables/fields come into existence when its class is loaded and therefore exist before any object is created from that class.
  7. Example of referring to a static variable/field using a class name: BorderLayout.NORTH
  8. Example of referring to a static variable/field using an object reference: BorderLayout layout = new BorderLayout(); int value = layout.NORTH;
  9. Whenever an object is created from a class, the new object gets its own storage space for each of the instance variables/fields declared in its class file. Instance variables/fields do not exist until an object is created from a class. Since each object of a class has its own copy of all of the instance variables/fields, changes made to one object's instance variables/fields have no effect on other object's instance variables/fields.
  10. One important purpose of constructors is to initialize the instance variables/fields of an object.
  11. Both instance and static variables/fields are automatically initialized. Numeric variables/fields are set to 0. Boolean variables/fields are set to false. Object references are set to null.

Static versus instance methods

  1. Methods are either static or instance.
  2. If a method is not declared as "static", then it is automatically "instance".
  3. There is no "instance" keyword.
  4. Static methods can be called before any objects of the class are created.
  5. Static methods can be called from other classes by using either the class name or an object reference, where the object is an instance of the class containing the method being called.
  6. Static methods can not call instance methods unless they use an object reference.
  7. Instance methods called from other classes must be called using an object reference, where the object is an instance of the class containing the method being called.
  8. Example of calling a static method using a class name: JOptionPane.showMessageDialog(null, "Hello");
  9. Example of calling a static method using an object reference: // Note: this is a very contrived example JOptionPane opane = new JOptionPane(); opane.showMessageDialog(null, "Hello");
  10. Example of calling an instance method: JFrame frame = new JFrame(); Container c = fra.getContentPane();
  11. Static methods do not have access to instance variables/fields unless they use an object reference.
  12. Instance methods have access to both static and instance variables/fields.
  13. An instance method may have an instance variable/field and a parameter or local variable with the same name. If this is the case, the parameter or local variable name will hide the instance variable/field. The instance variable/field may still be referenced in that case using the "this" keyword.
  14. Example of using "this" to access an instance variable/field: public class Item { int qty;
    public Item(int qty) { this.qty = qty; } }

Composition and inheritance

  1. Composition is when a class includes object reference variables/fields. This is called a "has-a" relationship.
  2. Inheritance is when a class extends another class. This is called an "is-a" relationship.
  3. If class A extends class B, then class B is called the superclass. It is also known as the base class. Class A is known as the subclass. Class A is also known as the derived class.
  4. If class A extends class B, then class A inherits all the methods and static and instance variables/fields of class B.
  5. A class may inherit a field or method, but still not have access to it if the field or method had private access.
  6. Example of class A extending class B:
    public class A extends B { }
  7. All classes that you write in Java must extend another class. If you do not specify a class to extend, then Java automatically assumes the class extends the Object class.
  8. The Object class is the base class for all other classes in Java.
  9. You can call superclass methods using the "super" keyword.
  10. Example of using a superclass method call: public String toString() { return super.toString() + " more description"; }

Overloading and overriding methods

  1. A class can have several methods with the same name as long as they can be differentiated by the data types of their parameters. This is called overloading.
  2. A class may replace an inherited method by writing a new version of the method with the same name and the same list of parameter data types.

Abstract methods and classes

  1. A class may contain an abstract method.
  2. Abstract methods must be declared abstract: abstract void draw();
  3. Abstract methods have no implementation.
  4. Classes containing an abstract method must be declared abstract: abstract class Shape { abstract void draw(); }
  5. Abstract classes may not be instantiated (you can't make an object out of them).
  6. Abstract classes are usually subclassed. The subclass can override and implement the abstract method it has inherited.
  7. If a class extends an abstract class and implements all of the abstract methods it has inherited, then it is no longer an abstract class and it can be instantiated (you can create objects out of it).
  8. If all of the methods in a class are abstract, then you are probably looking at an interface. Event handlers, such as ActionListener, are interfaces.

Polymorphism

  1. Polymorphism is when you are able to treat a number of different types of objects as one common superclass.
  2. Example of polymorphism: Assume you have an abstract Shape class and two sublasses of the Shape class named Rectangle and Circle. You can create an array to hold Shape object references even though you can't actually create a Shape object (since Shape is abstract). You can create Rectangle and Circle objects and store their references in the array of Shape object references. This is because both Circle and Rectangle are Shape objects underneath.

Resource