Public Members of a class in Java

When a class members are public they can be accessible within the declared class from the same package, outside the declared class within the same package, outside the declared class from a different package or through inheritance (means subclasses)

Let's check out all the possiblity to execute public members of a class.

# Within the declared class from the same package:


package com.company;

public class Rectangle{
  public float printArea(float width, float height){
    return width*height;
  }
  
  //main method
  public static void main (String[] args)
  {
   //create an object of Rectangle
   Rectangle rect = new Rectangle();
   System.out.println(rect.printArea(12.0f, 8.0f));
   
  }
}
/**
 * Output
 * 96.0
 */

# Same package but outside the declared class:

Create a class called Rectangle.java in com.company package.

Rectangle.java


package  com.company;

public class Rectangle{
  public float printArea(float width, float height){
    return width*height;
  }
}

Create another class called TestExample.java in com.company package.

TestExample.java (contains main method)


package com.company;

public class TestExample{
  
  public static void main (String[] args)
  {
   //create an object of Rectangle
   Rectangle rect = new Rectangle();
   System.out.println(rect.printArea(12.0f, 8.0f));
   
  }
}
/**
 * Output
 * 96.0
 */

# Outside the declared class and from a different package.

Create a class Rectangle.java in com.company package.

Rectangle.java


package com.company;
 
 public class Rectangle{
   public float printArea(float width, float height){
     return width*height;
   }
}   

Create another class called TestExample.java in com.company.test package. (Probably you have to create a new package test in com.company)

TestExample.java (contains main method)


 package com.company;
 
 public class TestExample{
   
   public static void main (String[] args)
   {
    //create an object of Rectangle
    Rectangle rect = new Rectangle();
    System.out.println(rect.printArea(12.0f, 8.0f));
   
   }
 }
 /**
  * Output
  * 96.0
  */
 

# Execute public members of a class through inheritance. (Subclass in other words)

Create a simple Shape class in com.company package and it has a float method called printArea(). This method returns float data when it is called.

Now, create a subclass of Shape class called Rectangle and it contains the main method of this application.

Inheritance: It is a mechanism to acquire the data members (properties, methods) of a parent class.

Rectangle.java (file name)


package com.company;

class Shape{
  public float printArea(float width, float height){
    return width*height;
  }
}

public class Rectangle extends Shape{
    public static void main (String[] args) {
  //create an object of Rectangle
  Rectangle rect = new Rectangle();
  System.out.println(rect.printArea(12.0f, 8.0f));
    }
}
/**
 * Output
 * 96.0
 */

We have successfully execute all the above scenarios. Hope this post helps you to understand about the possibility of public members of a class.

//Link: know about private members of class //Link: know about protected members of a class //Link: final members of a class //Link: Static members of a class //Non static members of a class