Object can represent "Real-world" entities such as bank accounts. It is self-contained and therefore easy to implement, modify, and test for correctness.
Object-oriented classes, when designed properly, are very easy to reuse.
A class defines the structure of its objects. A class definition includes variables (data) and methods (actions) that determine the behavior of an object. The class must have more than one constructor. Constructor must same name with class's name. Class has No return type, if it needs put 'void'.
Within the Classes | Within Subclasses in the same package | Within subclasses in other packages | Everywhere | |
---|---|---|---|---|
Public | X | X | X | X |
Protected | X | X | X | |
Package | X | X | ||
Private | X |
Object-oriented application set of objects working together to solve a problem (by sending each other messages). Key step is identifying classes that can be used to help solve problem.
Classes are organized in an "is-a" hierarchy
Except 'private' all member of upper class are inherited. Static variables can also be inherited.
Memory allocation when creating an object in lower class.
public class IncDate extends Date { //Date: upper class
public IncDate (int newMonth, int newDay, int newYear) {
super(newMonth, newDay, newYear);
}
}
}
Whenever one class inherits another class, it is called an 'is-a' relationship.
For example, a lion is a anial; a giraffe is a animal.
'is-a' relationship (inheritance) can be achieved by using extends keyword. Additionally used for code reusability in Java and to avoid code redundancy.
'is-a' relationship is unidirectional. We cannot say a animal is a lion. We only say a lion is a animal.
It is bonded, so chaning one entity will affect another entity.
It is called composition. The use of instance variables that are references to other objects.
'Has-a' relationship is a unidirectional also.
Object class is the root of the tree. Java supports single inheritance only
Use the '@Override' notation to indicate the redefinition of an inherited method.
Object obj;
if (cutoff <= 50)
obj = new String("Hello");
else
obj = new Date (1, 1, 2015);
System.out.println(obj.toString());
-> obj is a polymorphic object
Let us group related classes together into a single named unit and organize our files. Package can be compiled separately and imported into our programs. it makes it easier for programs to use common class files. Helps us aboid naming conflicts (two classes can have the same name if they are in different packages). Imported classes are not menbers of the package.
The name of the file containing the compilation unit must match the name of the public class within the unit. Each Java compilation unit is stored in its own file.
A package with muliple public classes must be implemented with multiple compilation units, each in a separate file.
Java exception mechanism has three major parts.
Exception may be handled any place in the software hierarchy - from the place in the program module where it is first detected through the top level of the program. Unhandled built-in exceptions carry the penalty of program termination. Where in an application an exception is handled is a design decision. However, exceptions should always be handled at a level that knows what the exception means.
Exception need not be fatal. For non-fatal, the thread of execution can continue from various points in the program, but execution should continue from the lowest level that can recover from the exception.
RunTimeException