ISYS 303 - FINAL REVIEW Flashcards
Scope & Life Time of Variables in Block
-Java allows variables to be declared w/in any block
-Block: begins w/ curly brace and ends with curly brace, defines a scope, each time you start a new block you are creating a new scope
-Scope: determines what objects are visible to other parts of the program & determines the lifetime of those objects
-Method Scope: if the method has parameters it is included in the method’s scope, variables declared (type var-name) inside a scope are not visible to code that is defined outside that scope
In nested scopes, variables declared in the outside are visible to the nested scopes (inner scopes) but not vice versa
Scope (Public, Private, Protected)
Public - Visible within class, subclasses, same/different packages, and entire project Private - Visible only within same class Protected - Visible within class, subclasses, and same package Default - visible within class, subclasses, and same package (Except different package by subclass)
What is a class?
Class is a template that defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specifications to construct objects. Objects are instances of a class. Thus, a class is a set of plans that specify how to build an object. A class is a logical abstraction; it is not until an object of that class has been created that a physical representation of that class exists in memory.
How to define a class?
A class defintion creates a new data type. In this case, the new data type is called Vehicle. You will use this name to declare objects of type Vehicle. Remember that a class declaration is only a type of description; it does not create an actual object. To actually create a Vehicle object, you will: Vehicle minivan = new Vehicle();
What are methods?
Methods are subroutines that manipulate that data defined by the class and, in many cases, provide access to that data. A method contains one or more statements. Usually performs only one task. Each method has a name, and it is this name that is used to called the method.
What is a constructor?
Initializes an object when it is created, same name as class, no explicit return type
.this
when a methods is called, it is automatically passed an important argument that is referenced to the invoking object (that is, the object on which the method is called)
bubble sort
for (a = 1; a = a; b--) { if (nums[b - 1] > nums[b]) { t = nums[b-1]; nums[b-1] = nums[b]; nums[b] = t; } } }
Static
When a member is declared static, it can be accessed before any object of its class are created and without reference to any object. You can declare both methods and variables to be static. Outside the class, to use a static member, you need only specify the name of its class followed by the dot operator. Variables declared as static are, essentially, global variables. When an object is declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
super
super(var-name, var-name)
***var-name = variable names from the parent class
Method Overriding
When a method in a subclass has the same return type and signature as a method in its parent class, then the method in the subclass is said to OVERRIDE the method in the parent class. When an overriden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the parent class will be hidden. If you want to access the parent class version, you can by using super: super.NameOfMethod
Using final
Prevents method overriding and inheritance. -Declaring a class as final implicitly declares all of its methods as final too
Packages
Packages are groups of related classes. Packages help organize your code and provide another layer of encapsulation. Package serves two purpose.
- Provides a mechanism by which related pieces of a program can be organized as a unit, Classes defined within a package must be accessed through their package name. Thus, a package provides a way to name a collection of classes
- Participates in Java’s access control mechanism. Classes defined within a package can be made private to that package and not accessible by code outside the package. Thus, a package provides a means by which classes can be encapsulated
Importing Packages
import pkg.classname; //just to import the particular class
***pkg = package name
import pkg.* //import all of the classes in the package
Exception Handling
An exception is an error that occurs at run-time. Exception handling streamlines error handling by allowing your program to define a block of code, called exception handler, that is executed automatically when an error occurs.