2. Topics To Cover
• Case study of Square class: overloaded constructors
• Default and no-argument constructors
• Use of “this” keyword to refer current object’s members
• Composition
• Enumerations
• Garbage collection and method finalize()
• Static class members
• “final” keyword
• Creating and accessing packages
Instructor: Tanzila Kehkashan
2
3. Composition Example
• If Car object destroy the Engine object also destroys. Engine Can't survive without car class
object.
class Car
{
Engine engine;
Stereo stereo;
public Car()
{
this.engine=new Engine();
}
}
class Engine
{ }
Instructor: Tanzila Kehkashan
3
4. Package Creation Steps
• Declare a public class. If class is not public, it can be used only by other classes in
the same package.
• Choose a unique package name and add a package declaration statement in top of
class.
• package a.b.c;
• Order of java source code:
• Package declaration (if any)
• Import declaration
• Class declaration
• Compile the class so that it is placed in the appropriate package directory.
• javac –d . Classname.java
• Import the reusable class into a program and use the class.
Instructor: Tanzila Kehkashan
4
5. Assignment # 2
• Write a program in Java which displays and does basic
calculator functions.
Instructor: Tanzila Kehkashan
5