comprog

Subdecks (1)

Cards (93)

  • Inheritance
    • Enables the use of an existing class to define a new class
    • Derived class is defined by adding instance variables and methods to an existing class
    • Base class is the existing class that the derived class is built upon
  • Inheritance
    1. Defined by starting with another defined class and adding methods and instance variables
    2. Syntax: public class DerivedClass extends BaseClass { }
  • Inheritance
    • Sample usage: public class Student extends Person { }
  • If a derived class defines a method with the same name, parameters, and return type as a method in the base class, it overrides the definition in the base class
  • Derived class has its own constructors and does not inherit any constructors from the base class
  • Constructor definition for the derived class

    1. Typical first action is to call a constructor of the base class
    2. One way of defining a constructor is by calling another constructor in the same class using the this keyword
  • Use of the super keyword must be the first action in a constructor definition
  • Polymorphism is the ability of an object to take on many forms
  • Polymorphism
    • Allows changes in the method definition for derived classes to apply to methods in the base class
    • Common use occurs when a base class reference is used to refer to a derived class object
  • When an overridden method is invoked, its action is defined in the class used to create the object, not by the type of the variable naming the object
  • Dynamic binding allows many meanings to be associated with one method name
  • In dynamic binding, the definition of a method is not bound until run time when the method is called
  • Polymorphism
    • Example of polymorphism using different classes
  • Interface
    • Used to specify methods that a class must implement
    • Contains headings for a number of public methods
  • Abstract Methods
    • Abstract class cannot be instantiated but can be subclassed
    • Serves as a base for subclasses and may or may not include abstract methods
  • Abstract class
    Cannot be instantiated but can be subclassed, serves as a base for subclasses, may or may not include abstract methods
  • Abstract method
    Declared as abstract and does not have an implementation, written without braces followed by a semicolon
  • Abstract method syntax
    public abstract void methodName();
  • Abstract classes and interfaces
    Both cannot be instantiated and may contain a mix of methods declared with or without an implementation
  • Consider using interfaces if

    • Unrelated classes would implement your interface
    • Specify behavior of a particular data type without concern about who implements it
    • Take advantage of multiple inheritance of type
  • Consider using abstract classes if

    • Share code among several closely related classes
    • Expect that classes extending your abstract have many common methods or fields, or require access modifiers other than public
    • Declare non-static or non-final fields to define methods that can access and modify the state of the object
  • A constructor is a special method used to create and initialize an object, does not have a return type, can call methods within its class
  • Sample usages of constructors
    Various constructors with different parameters for initializing objects
  • Static variables and methods
    Belong to a class as a whole, shared by all objects of the class, static method can be invoked without using any object
  • Static method invocation
    Write the class name instead of the object name
  • When calling a static method, you write the class name instead of the object name
  • A static method cannot reference an instance variable of the class. It cannot invoke a non-static method of the class unless it has an object of the class and uses the object in the invocation
  • The predefined Math class provides a number of standard mathematical methods. The use of import statement is not required
  • Math class methods
    • pow
    • abs
    • max
    • min
    • random
    • round
    • ceil
    • floor
    • sqrt
  • Overloading
    Occurs when multiple methods have the same name within the same class by having different method definitions in the methods’ parameter lists
  • Java distinguishes methods according to the number of parameters and the types of the parameters. A method’s name and the number and types of its parameters are called the method’s signature. A class cannot define multiple methods with the same signature. Constructors can be overloaded too
  • The void keyword denotes that the method is not used to return a value. Parameter is the list of variables in a method declaration while argument is the actual value that is passed when the method is invoked. The data type of the return value must match the declared return type. Instance variables are variables declared outside the method, constructor, or any block. A method is a collection of statements that are grouped together to perform an operation. The this keyword represents the object’s name receiving the method call within a method definition. Local variables are variables declared within a method definition. These variables are only visible to the methods in which they are declared. They are not accessible from the rest of the class
  • Information hiding is the mechanism for restricting access to some of the object’s components. Advantages of Information Hiding: Makes components easier to understand/use, Simplifies modification and repair, Facilitates re-use. If an instance variable is public, there are no restrictions on where you can use its name. If an instance variable is private, its name cannot be used to access it outside of the class definition. If the method is public, you can invoke it anywhere without restriction. If a method definition is private, the method cannot be invoked within the definitions of methods in its class. The accessor is a public method that returns data from a private instance variable. The mutator is a
  • Private variable
    Its name cannot be used to access it outside of the class definition
  • Public method
    Can be invoked anywhere without restriction
  • Private method
    Cannot be invoked within the definitions of methods in its class
  • Accessor method
    A public method that returns data from a private instance variable
  • Mutator method
    A public method that changes the data stored in one or more private instance variables
  • Encapsulation
    The process of combining data and actions into a single item, grouping instance variables and methods into a class, and hiding implementation details
  • UML Class Diagram
    Describes the structure of a class by displaying the class name, variables, and methods