Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8456013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:26:15+00:00 2026-06-10T12:26:15+00:00

I am creating following class hierarchy: abstract class Shape{ protected abstract float getArea(); protected

  • 0

I am creating following class hierarchy:

abstract class Shape{
    protected abstract float getArea();
    protected abstract float getVolume();
}

abstract class TwoDimentionalShape extends Shape{
    public abstract float getArea();
    protected float getVolume(){
        return 0;
    }
}

class Square extends TwoDimentionalShape {
    float width, height;
    Square(float w, float h){
        width = w;
        height = h;
    }
    public float getArea(){
        return width*height;
    }
}

public class ShapeTest {
    public static void main(String args[]){
        Shape s = new Square(3, 4);
        System.out.println(s.getVolume());
    }
}

What I wish to do is to hide the function getVolume() for TwoDimentionalShape class, as it will be used for ThreeDimentionalShape class.

The problem is that I have declared the function as protected, but when I call it from main(), the program is working. Why is this happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:26:16+00:00Added an answer on June 10, 2026 at 12:26 pm

    Protected is visible in sub classes in or outside of the package and classes in the same package. It looks like your classes are all in the same package. That is why you can see it.

    In general the hierachy is:

    Public – available in all packages and sub classes

    Protected – avaialbe in sub classes and the same package

    (Default ie nothing) available in the same package

    Private – available in the same class

    It is possible to access (using reflection) some fields/methods that you cannot normally “see” although this is not guaranteed and depends on teh security manager that is used.

    I think the first answer of this questions explains it very well
    In Java, difference between default, public, protected, and private

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code I am creating a List<> of the following class: public class
We have the following class hierarchy: public interface IManager { object GetObject(int); } public
I have a folder hierarchy represented by the following class: public class Folder {
Given the following assemblage of classes (contrived): public class School { [PrimaryKey] public string
I am creating a few pictureboxes dynamically, then assigning the following: // class variable
I have a class like following: public class Trainee { private static int numberOfTrainee
I used an interface and class similar to following: public interface IIdentity { int
So assume I am mocking the following class: public class ClassAParams { public int
Let's consider I want to create the following hierarchy: Animal -> Class (mammal, amphibian,
Consider following class class test { public: test(int x){ cout<< test \n; } };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.