answersLogoWhite

0

What is public class in java?

Updated: 8/17/2019
User Avatar

Wiki User

11y ago

Best Answer

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();

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is public class in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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(){ } }


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 { ......... }


How a class in java declared default?

default it is public type


Why the name of a java file is same as the name of the public class?

It is not compulsory that the java file name and name of the public class should be same. if u will give java file name and public class name different then u have to compile and run the program with another names. for example: u have named class sample i.e. (public class sample) and main function is also defined in this class. and u have saved the file as abc.java then u will first comple the file as: javac abc.java now run the file (type java class name(in which main function is defined) i.e. java sample try it.


Is it possible to have more than one public class in the same file?

No. There can be multiple java classes in the same .java file, but the name of the file must match the name of the public class in the file.


Java program file must match the name of class with extension true or false?

The name of the .java file should exactly match with the name of the public class in the file. Ex: public class Test { ..... } this file should be saved as Test.java


What are the two ways of spawning a thread in Java?

You can create a Thread in Java by using two ways. 1. Extending the Thread class public class Test extends Thread { ..... } 2. Implementing the Runnable Interface public class Test implements Runnable { ... }


Can a .java file contain more than one java classes?

Yes, it can. However, there can only be one public class per .java file, as public classes must have the same name as the source file.


Why would you make a class final in java?

You would make a class Final in Java if you do not want anybody to inherit the features of your class. If you declare a class as Final, then no other class can extend this class. Example: public final class X { .... } public class Y extends X { .... } Here Y cannot extend X because X is final and this code would not work.


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.


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 the recent public implementation of Java?

Apache Harmony is the recent open source implementation of the Java runtime with class libraries and associated tools.