Question

JAVA QUESTIONS: 1. What are the differences between constructors and other methods? 2. What is a...

JAVA QUESTIONS:

1. What are the differences between constructors and other methods?

2. What is a NullPointerException and how should it be handled?

3. What must a class do to implement an interface and what must a class do to extend another class?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ques 1.

  • A constructor is a special type of method which is called automatically when an object of a class is created. But a normal method has to be called explicitly using the object.
  • Also, the function can be public, private and protected, but the constructor is always public.
  • Also, the name of the constructor is the same as he name of the class. But method has a different name than the class name.

Ques 2. A NullPointerException is a type of exception which is thrown when an object is used, but it has null value. So, we are trying to access a null value. So, NullPointerException is thrown.

We can handle the exception using try-catch block to run a code in case exception occurs.

// to check for exception

try{

// code which can throw NullPointerException

}

catch( NullPointerException e ){

  // code which runs when an exception occurs

}

Ques 3. In order to omplement an interface, we will use the implements keyword after the class name.

e.g:

class class_name implements interface_name {

}

Also, we need to override each function present in the interface inside class. Otherwise it will show error.

For inheriting the class, we use the extends keyword and then the name of the class to be inherited. e.g:

class class_name extends class_to_be_inherited_name {

}

Add a comment
Know the answer?
Add Answer to:
JAVA QUESTIONS: 1. What are the differences between constructors and other methods? 2. What is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString...

    java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString methods, getters and setters. Then create three Ticket instances. • Airport: String name, String city, String country • Route: Airport departure , Airport destination • Date: int day, int month, int year (Day must be between 0-31, month must be between 0-12, year must be between 2020-2025; please consider this when you're implementing setters and constructors). • ClockTime: int hour, int minute (Hour must...

  • Java (Study Questions on Iterators and Introduction to Algorithm Complexity) 1) Look up the Iterator interface...

    Java (Study Questions on Iterators and Introduction to Algorithm Complexity) 1) Look up the Iterator interface in the Java API. If a class implements this interface, what methods must it supply the code for? Include return types and parameter lists. 2) Look up the Iterable interface in the Java API. If a class implements this interface, what methods must it supply the code for?

  • Java Questions A class must implement at least one interface. Question 1 options: True False Question...

    Java Questions A class must implement at least one interface. Question 1 options: True False Question 2 (1 point) Abstract methods can be called. Question 2 options: True False Question 3 (1 point) Constructors Question 3 options: Are inherited but cannot be accessed Are inherited and can be accessed Are accessible but are not inherited Are not inherited and cannot be accessed Question 4 (1 point) We can instantiate an object from an abstract class. Question 4 options: True False...

  • java 4. Java allows for the use of generic typing to do away with all type...

    java 4. Java allows for the use of generic typing to do away with all type checking of parameters to methods.​ TRUE OR FALSE ? 5 How many interfaces can a class implement? 6.When a programmer writes a class definition to implement an interface, he/she must write the method body for at least one of the method headings specified in the interface TRUE OR FALSE? 7. Declaring a formal parameter's type as T is the same thing as declaring the...

  • Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should...

    Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should define a way to establish numeric salience among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Salience interface. Create a driver class to exercise some Task objects.

  • (Java Problem)In this question we will step you through implementing classes and methods in Java for...

    (Java Problem)In this question we will step you through implementing classes and methods in Java for a new ride-sharing app called Rebu. Rebu connects passengers looking for a ride with drivers in their area. You can assume that the classes or methods in earlier questions “exist” in later questions, even if you haven’t answered the question. You must follow proper Object Oriented Design principles, and Java programming conventions. Write all class declarations. Do not write method signatures that are given...

  • 1.     When a sub class object is created, when is the call to the super class...

    1.     When a sub class object is created, when is the call to the super class constructor made? How does a programmer call the super class constructor from the sub class? What do all classes indirectly extend? What methods does every class inherit from the Object class? 2.     When writing methods in a sub class, how can those methods call the methods from the parent class? 3.     Which class is more specific, a super class or a sub class? 4.    ...

  • 1. What are the differences between doGet and doPost in java servlet 2. What does response...

    1. What are the differences between doGet and doPost in java servlet 2. What does response and request mean in a Java servlet ? 3. what does out.println do in a Java servlet ? 4. Where does Java compiler get the binaries for the following ? import javax.servlet.*; import javax.servlet.http.*;

  • Could someone provide me general java script statements that would answer these questions? These can be...

    Could someone provide me general java script statements that would answer these questions? These can be very general, I just need to know what statements to use to perform these functions so I can develop an outline. How to implement concatenated if/else statements. How to implement while loops, do while loops, and for loops. Be able to perform error checking using while loops. Define and implement classes: properties, accessors, mutators, constructors (defaults and with arguments), methods, toString method Create objects...

  • (JAVA)What are the methods that every class should implement

    (JAVA)What are the methods that every class should implement

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT