Tuesday, July 10, 2012

Some Object Oriented Definitions from Sam's Teach Yourself Java 6 in 21 Days


Some Object Oriented Definitions from Sam's Teach Yourself Java 6 in 21 Days


Object-oriented programming is an approach to building computer programs that mimics how objects are assembled in the physical world. And is used to create programs that are more reusable, reliable, and understandable.


In object-oriented programming, a computer program is conceptualized as a set of objects that work together to accomplish a task. Each object is a separate part of the program, interacting with the other parts in specific, highly controlled ways.


An object is a self-contained element of a computer program that represents a related group of features and is designed to accomplish specific tasks.


A class is a template used to create an object. Every object created from the same class has similar features.


Classes embody all features of a particular set of objects. When you write a program in an object-oriented language, you don’t define individual objects. Instead, you define classes used to create those objects.


The class defines what an object would be like if one were used in a program, but it doesn't create one of these objects.


The process of creating an object from a class is called instantiation, which is why objects also are called instances.


A Java class consists of two distinct types of information: attributes and behavior.


Attributes are the data that differentiates one object from another. They can be used to determine the appearance, state, and other qualities of objects that belong to that class.


In a class, attributes are defined by variables—places to store information in a computer program. Instance variables are attributes that have values that differ from one object to another.


An instance variable defines an attribute of one particular object. The object’s class defines what kind of attribute it is, and each instance stores its own value for that attribute. Instance variables also are called object variables.


A class variable defines an attribute of an entire class. The variable applies to the class itself and to all its instances, so only one value is stored no matter how many objects of that class have been created.


Behavior of a Class of Objects  Behavior refers to the things that a class of objects can do to themselves and other objects. Behavior can be used to change the attributes of an object, receive information from other objects, and send messages to other objects asking them to perform tasks.
Behavior for a class of objects is implemented using methods.


Methods are groups of related statements in a class that perform a specific task. They are used to accomplish specific tasks on their own objects and on other objects and are comparable to functions and subroutines in other programming languages.


Objects communicate with each other using methods. A class or an object can call methods in another class or object for many reasons, including the following:
 To report a change to another object
 To tell the other object to change something about itself
 To ask another object to do something


Instance methods, are usually just called methods, used when you are working with an object of the class. If a method makes a change to an individual object, it must be an instance method.
Class methods apply to a class itself.


The static keyword indicates that the method or variable is a class attribute shared by all objects.


Inheritance is a mechanism that enables one class to inherit all the behavior and attributes of another class.
Through inheritance, a class immediately picks up all the functionality of an existing class. Because of this, you only must define how the new class is different from an existing class.


A class that inherits from another class is called a subclass. The class that gives the inheritance is called a superclass.
A class can have only one superclass, but each class can have an unlimited number of subclasses. Subclasses inherit all the attributes and behavior of their superclasses.


At the top of the Java class hierarchy is the class Object—all classes inherit from this superclass. Object is the most general class in the hierarchy, and it defines behavior inherited by all the classes in the Java class library.


Subclassing is the mechanism for defining new classes as the differences between those classes and their superclass.
It is the process of creating a new class that inherits from an existing class.


When a subclass defines a method that matches a method defined in a superclass in name and other aspects. In this case, the method definition found first (starting at the bottom of the hierarchy and working upward) is the one that is used.
Because of this, you can create a method in a subclass that prevents a method in a superclass from being used. To do this, you give the method with the same name, return type, and arguments as the method in the superclass. This procedure is called overriding.


Java’s form of inheritance is called single inheritance because each Java class can have only one superclass (although any given superclass can have multiple subclasses).


In other object-oriented programming languages, such as C++, classes can have more than one superclass, and they inherit combined variables and methods from all those superclasses. 
This is called multiple inheritance, and it provides the means to create classes that encompass just about any imaginable behavior.


Java solves the problem of shared behavior across different branches of a class hierarchy by using interfaces.
An interface is a collection of methods that indicate a class has some behavior in addition to what it inherits from its superclasses. The methods included in an interface do not define this behavior; that task is left for the classes that implement the interface.


Packages in Java are a way of grouping related classes and interfaces. Packages enable groups of classes to be available only if they are needed and eliminate potential conflicts among class names in different groups of classes.
By default, your Java classes have access only to the classes in the java.lang package, which provide basic language features. To use classes from any other package, you must refer to them explicitly by package name or import them in your source file.
To refer to a class within a package, you must normally use the full package name.


A Java program is made up of classes and objects, which, in turn, are made up of methods and variables. Methods are made up of statements and expressions, which are made up of operators.


A statement is a simple command that causes something to happen.


An expression is a statement that produces a value. The value returned by an expression statement is called its return value.


Each statement in Java is terminated with a semicolon character (;).


Statements in Java are grouped using the opening brace ({) and closing brace (}). A group of statements organized between these characters is called a block or block statement.


A variable is a place where information can be stored while a program is running. The value can be changed at any point in the program.


To create a variable, you must give it a name and identify what type of information it will store. You also can give a variable an initial value at the same time you create it.


There are three kinds of variables in Java: instance variables, class variables, and local variables.
Instance variables, are used to define an object’s attributes.
Class variables define the attributes of an entire class of objects and apply to all instances of it.
Local variables are used inside method definitions or even smaller blocks of statements within a method. You can use them only while the method or block is being executed by the Java interpreter. They cease to exist afterward.


If the value should never change during a program’s runtime, you can use a type of variable called a constant.
A constant, which also is called a constant variable, is a variable with a value that never changes.


Constants are useful in defining shared values for the use of all methods of an object.
In Java, you can create constants for all kinds of variables: instance, class, and local.
To declare a constant, use the final keyword before the variable declaration and include an initial value for that variable.