answersLogoWhite

0

What is a public class?

Updated: 8/10/2023
User Avatar

Wiki User

6y ago

Best Answer

A public class is a base class declared with public inheritance:

class base {

// ...

};

class derived : public base {

// ...

};

In the above example, base is a public class of derived, thus derived is regarded as being a type of base. The derived class inherits all the public and protected methods of its base. Protected methods are accessible to the derived class, its derivatives and their friends.

If base were declared protected, its public methods become protected methods of derived. The base class is then an implementation detail of derived; only members of derived, its derivatives and their friends can treat derived as being a type of base.

If declared private, the public and protected methods of base become private methods of derived. The base class is then an implementation detail of derived; only members of derived and its friends can treat derived as a type of base.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

In Java, the term public means that the method is visible and can be called from the other objects of the other types. In other words, public in Java, means that the variables are visible to all classes.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Public access defines the class interface that is available outside of the class. Private access defines the class interface that is only accessible within the class and within friends of the class. Protected access is similar to private access, but extends the interface to derived classes as well.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

You cannot -- not without changing the class interface. Otherwise, class member access can only be modified through subclassing, but you can only decrease or maintain the same level of accessibility, you can never increase it. If this were permitted, encapsulation of the base class would be completely undermined. However, private class members are never accessible through subclassing anyway. Private members are only accessible to the class itself and to friends of the class. While you could declare a subclass a friend within its own base class, this would be an uncommon arrangement but would still not alter the fact that private base class members remain private -- they can never be made publicly accessible.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A Public Class is one that can be accessed by any other class in Java.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a public class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


Can an inner class be creatd?

Yes. public class My { public class Wallet { } } //To create one My.Wallet billFolder = new My.Wallet();


What happens if a constructor is not defined in class?

Nothing happens. The compiler successfully compiles the class. When a class does not have a specific constructor, the compiler places a default no argument construtor in the class and allows you to compile and execute the class. public class Test { } and public class Test { public Test(){ } } are one and the same.


What is the valid class declaration header for the derived class d with base classes b1 and b2 A class d public b1 public b2 B class d class b1 class b2 C class d public b1 b2 D class d b1 b2?

What is the valid class declaration header for the derived class d with base classes b1 and b2?A. class d : public b1, public b2 {/*...*/};B. class d : class b1, class b2 {/*...*/};C. class d : public b1, b2 {/*...*/};D. class d : b1, b2 {/*...*/};The answer is A, C and D.B is not valid because "class" is not a valid access specifier.All the others are valid because private access is the default when the access specifier is omitted. Note that if class D were declared using the struct prefix, inheritance would default to public access rather than private.


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }

Related questions

Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


Give an example of constructor and destructor in derived class?

class superclass { public: superclass() {... } // c'tor public: virtual ~superclass() {... } // d'tor }; // superclass class derived: public superclass { public: derived() : superclass() { ... } // derived c'tor public: virtual ~derived() {... } // derived d'tor }; // derived class


Can an inner class be creatd?

Yes. public class My { public class Wallet { } } //To create one My.Wallet billFolder = new My.Wallet();


What happens if a constructor is defined in class?

Nothing happens. The compiler successfully compiles the class. When a class does not have a specific constructor, the compiler places a default no argument construtor in the class and allows you to compile and execute the class. public class Test { } and public class Test { public Test(){ } } are one and the same.


What happens if a constructor is not defined in class?

Nothing happens. The compiler successfully compiles the class. When a class does not have a specific constructor, the compiler places a default no argument construtor in the class and allows you to compile and execute the class. public class Test { } and public class Test { public Test(){ } } are one and the same.


Write a program to demonstrate multilevel inheritence in java?

Ex: public class A { ... } public class B extends A { ... } public class C extends B { ... } Here class C extends B which in turn extends A so class C indirectly extends class A.


What is a example of override?

An override occurs when you create a method of the same name as the one in the parent/super class. Ex: public class A { ..... public String getName(){ } ..... } public class B extends A { ..... public String getName(){ } ..... } Here class B which is the child class of A also declares a method of the same name as in the parent class. This is overriding...


What is the valid class declaration header for the derived class d with base classes b1 and b2 A class d public b1 public b2 B class d class b1 class b2 C class d public b1 b2 D class d b1 b2?

What is the valid class declaration header for the derived class d with base classes b1 and b2?A. class d : public b1, public b2 {/*...*/};B. class d : class b1, class b2 {/*...*/};C. class d : public b1, b2 {/*...*/};D. class d : b1, b2 {/*...*/};The answer is A, C and D.B is not valid because "class" is not a valid access specifier.All the others are valid because private access is the default when the access specifier is omitted. Note that if class D were declared using the struct prefix, inheritance would default to public access rather than private.


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }


What is public class in java?

Generally to declare the class we use the public,abstract,final,strictfp and default modifiers. When ever we declare the class as public ,it mean public class A{} it is possible to access this class inside the same package and outside of the current package. ex: package name: pack This package contain the public class like package pack; public class A{} In this senario it is possible to access the in the same package "pack" like package pack; class Demo { public static void main(String args[]) { A a=new A(); } } And also it is posible to access the A class outside of the "pack" package like in anothe package like "pack1" package pack1; import pack.A; class Demo1 { public static void main(String args[]) { A a=new A(); } }


Examples of inheritance in c plus plus?

Single-inheritance is where one class inherits directly from another class: class A {}; class B : public A {}; Here, class B inherits all the public and protected members of class A. Multiple-inheritance is where one class inherits directly from two or more classes: class A {}; class B {}; class C : public A, public B {}; Here, class C inherits all the public and protected members of both A and B. Multi-level inheritance is where one class inherits from another class that itself derived. class A {}; class B : public A {}; class C : public B {}; Here, class B inherits all the public and protected members of A while class C inherits all the public and protected members of B, including those inherited from A. Virtual inheritance applies to multi-level inheritance whereby a virtual base class becomes a direct ancestor to the most-derived class. This variation of inheritance is typically used in multiple inheritance situations where two or more intermediate classes inherit from the same base class: class A {}; class B : public virtual A {}; class C : public virtual A {}; class D : public B, public C {}; Here, classes B and C both inherit from class A. Without virtual inheritance this would mean class D would inherit two instances of A (B::A and C::A), thus creating ambiguity when referring to D::A. By employing virtual inheritance, D inherits directly from A, and both B and C inherit from D::A. In other words, B and C share the same instance of A. Another use of virtual inheritance is when you need to make a class final. class A; class B { friend class A; B() {} // private constructor }; class A : public virtual B { }; Here, class A is the final class. Class B is a helper class that has a private constructor while class A is declared a friend of class B. Class A is therefore the only class that can inherit from class B as it is the only class that can construct objects from class B. However, by inheriting class B virtually, we ensure that no other class can be derived from class A because virtual inheritance ensures that the most-derived class must be able to construct a class B object first. Currently, only class A has that privilege and must always be the most-derived class.


What is the use of public access specifier?

use of public access specifier iswe can access the class members(methods,variables) out side the class using class reference