answersLogoWhite

0

Are you allowed to declared main method in every class?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

Yes. Every Java class can have a main method

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Are you allowed to declared main method in every class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can methods in c sharp declared outside the class?

C# is a completely object-oriented language, everything is an object. Every datatype, is a superset of the object class. I'm sorry to say, but every method must be declared inside of a class. :(


Do you use virtual functions in Java?

Every method in java that isn't a class (static) method is automatically "virtual." If you want to disable virtual overrides, make the method "final" in the base class.


Does Every class has a toString method and an equals method inherited from the Object class?

Yes - in Java, every class has this method, which is inherited from the Object class. Often, the inherited method does nothing particularly useful, but you can override it with your own implementation.


What will happen if a Java Class has no Main Method?

Nothing will happen. There is no restriction that every Java class must have a main method. The only program is that, this class cannot be executed as a standalone java program.


What is an object in java and is it useful in calling a method?

Objects are very useful in calling a method because they inherit the whole class of the object in the RAM to be executed. And, so a person can each & every method of the class.


A variable that is declared inside a method is called variable?

It is called a local variable since it only exists inside the method.


Why do you need to override the toString method?

By default every class you write inherently extends the base Object class. This has a basic toString() method which merely returns the name of the class followed by a hex representation of the hash value. By overriding this method, you can return a more meaningful value specific to your class, such as member attributes and their values.


Why use interfaces in Java?

an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior. When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not) Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isnt implemented inside the class. Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile: public class Ball implements Convertible { // Keyword 'implements' public void openHood() { } public void setOpenHoodFactor(int bf) { } } Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all… Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following: • Provide concrete (nonabstract) implementations for all methods from the declared interface. • Follow all the rules for legal overrides. • Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method. • Maintain the signature of the interface method, and maintain the same return type (or a subtype). • It does not have to declare the exceptions declared in the interface method declaration


How do you use an interface?

When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not) Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class. Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile: public class Ball implements Convertible { // Keyword 'implements' public void openHood() { } public void setOpenHoodFactor(int bf) { } } Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all… Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following: • Provide concrete (nonabstract) implementations for all methods from the declared interface. • Follow all the rules for legal overrides. • Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method. • Maintain the signature of the interface method, and maintain the same return type (or a subtype). • It does not have to declare the exceptions declared in the interface method declaration


Interface method have by default method public?

Yes, Interface mehtods are public by default so that they could be implemented by every class implementing the interfaces.


What is an interface and how will you go about implementing an interface?

an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior. When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not) Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class. Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile: public class Ball implements Convertible { // Keyword 'implements' public void openHood() { } public void setOpenHoodFactor(int bf) { } } Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all… Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following: • Provide concrete (nonabstract) implementations for all methods from the declared interface. • Follow all the rules for legal overrides. • Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method. • Maintain the signature of the interface method, and maintain the same return type (or a subtype). • It does not have to declare the exceptions declared in the interface method declaration.


When is the CBSE exam result 2012?

CBSE Results are declared every year in the second half of May for class XII and last days of May for class X (except once when the class x results were declared in first half of june). These results are mostly declared in phases according to the various regions of CBSE. chennai, panchkula to name a few. In 2011 class x results were declared in 5 phases from 31 may to 10 June 2011 In 2011 class XII results were declared in two phases on 23 may and 27 may 2011 This year too they are expected during the same period.