Reputation: 1476
I am new to java and doing a program which involves use of javax.mail.Authenticator
but I am having trouble understanding a particular statement which is:
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
I want to know why Authenticator
object created using new
operator also has a body which is overriding a method?
I mean, I have never used or seen this kind of statement, so any kind of hint or reference will help.
Thanks in advance.
Upvotes: 1
Views: 106
Reputation: 75
Because class Authenticator is abstract,and you can't instantiate an abstract class and interface. An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. And here is why your code look's like this -> When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
Upvotes: 3