Genral
Object
Represent
- Information: Objets have attributes
- Behavior: have responsibilities
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.
- An instantitation of a class
Class
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 |
- Public: Methods of the same class, as well as methods of other classes.
- Private: Methods of the same class **only** .
- Protected: Methods in the same class as well as methods of subclasses and methods in class in the same package.
- No modifier (package access): Methods in the same package **only** .
Applications
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.
Inheritance
Classes are organized in an “is-a” hierarchy
- Keyword
- Inheriting class: base, super, parent class
- Inherited class: derivation, sub, child class
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);
}
}
‘is-a’ relationship
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.
![](/assets/img/java_general.png)
‘has-a’ relationship
It is called composition. The use of instance variables that are references to other objects. ‘Has-a’ relationship is a unidirectional also.
Java’s Inheritance Tree
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.
- Polymorphism: An object variable can reference objects of different classes at different time.
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
Package
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.
Exception
- Exception situation: Associated with an unusual, unpredictable event, detectable by software or hardware.
Java exception mechanism has three major parts.
- Defining the exception: usually as a subclass of Java’s Exception class.
- Generating (raising) the exception: by recognizing the exceptional situation and then using Java’s throw statement to “announce” that the exception has occurred.
- Handiling the exception: using Java’s try-catch statement to discover that an exception has been thrown and then take the appropriate action.
General guidelines for using Exception
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.
Example of Exception
RunTimeException
- Are thrown when a standard run-time program error occurs.
- Example of run-time errors are division-by-zero and array-index-out-of-bounds.
- Can happen in virtually any method or segment of code -> are not required to explicitly handle these exceptions
- Unchecked exceptions
Dealing with error situations within ADT methods
- Detect and handle the error within the method itself -> Best approach, if the error can be handled internally.
- Throw an exception related to the error and force the calling method to deal with the exception. -If not clear how to handle, throw it out to a level where it can be handled.
- Ignore the error situation. If the preconditions of a method are not met, the method is not responsible for the consequences
Enjoy Reading This Article?
Here are some more articles you might like to read next: