We have already discussed OOP concepts in Java in detail. In this lecture, we’ll cover remaining OOP concepts like default constructors, garbage collection, this reference.

Default Constructor (Method)

The default constructor (no-arg constructor) is the constructor without any parameters.  If the class does not specify any constructors, then an implicit default constructor is created.

  • Classes can have more than one constructor
  • All constructors have the same name (the class name)
  • Each constructor differs from the others in either the number or types of its arguments
  • New is used when using a constructor to create a new object
Overloading Constructor Methods
public StudentRecord(){
	//some initialization code here}
public StudentRecord(String temp){
	this.name = temp; }
public StudentRecord(String name, String address){
	this.name = name;
	this.address = address;
public StudentRecord(double mGrade, double eGrade,double sGrade){
	mathGrade = mGrade;
	englishGrade = eGrade;
	scienceGrade = sGrade;

To use these constructors, we have the following code,

public static void main( String[] args ){
//create three objects for Student record
StudentRecord aRecord=new StudentRecord(“Ahmed");
StudentRecord bRecord=new StudentRecord(“BIT“, “Pakistan");
StudentRecord cRecord=new StudentRecord(80,90,100);
//some code here

“this()” constructor call

Constructor calls can be chained, meaning, you can call another constructor from inside another constructor.


public StudentRecord(){
	this("some string");
 public StudentRecord(String temp){
 	this.name = temp;
 public static void main( String[] args )
	StudentRecord aRecord = new StudentRecord();

Garbage Collection

If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable.

Access Modifiers

There are four different types of member access modifiers in Java

public (Least restrictive)

public access specifies that class members (variables or methods) are accessible to anyone, both inside and outside the class and outside of the package. Any object that interacts with the class can have access to the public members of the class.


protected access specifies that the class members are accessible only to methods in that class and the subclasses of the class. The subclass can be in different packages.

private (Most restrictive)

private accessibility specifies that the class members are only accessible by the class they are defined in.


default access specifies that only classes in the same package can have access to the class’ variables and methods. no actual keyword for the default modifier; it is applied in the absence of an access modifier.


To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages. Java classes always exist in a class package including those we define in our programs. There is a default package which doesn’t have a name. Java core API is made up of several packages. Class names in a package are qualified by the package name e.g Math class has the fully qualified name as java.lang.Math.

Tagged with: ClassesJAVAObject Oriented

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.