1

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.

Md Azharuddin
  • 1,346
  • 1
  • 9
  • 30
  • 3
    It's an anonymous class, inheriting from `Authenticator`. You can read about them in e.g. the [Oracle Tutorial on Anoynmous Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). – Turing85 Aug 22 '19 at 18:59
  • Also note that you can add new, non-overriding methods to anonymous objects (e.g. [this answer](https://stackoverflow.com/a/26227947/6288436)). There are few use cases for anonymous objects, but where they're useful, they're *really* useful. – cbryant02 Aug 22 '19 at 19:25
  • And BTW, [you don't usually need to use an Authenticator with JavaMail](https://javaee.github.io/javamail/FAQ#commonmistakes). – Bill Shannon Aug 22 '19 at 21:11

1 Answers1

3

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.

s.3.valkov
  • 75
  • 3
  • The answer misses the question. The syntax is general-purpose: It can be used to implement interfaces and all non-final classes. – Turing85 Aug 22 '19 at 19:05
  • So does that mean whenever I instantiate an abstract method, I need to provide a body and implement all abstract methods that it has? What if the abstract class doesn't have any abstract methods? – Md Azharuddin Aug 22 '19 at 19:08
  • @MdAzharuddin `new Authenticator() {}` is enough since there are no abstract methods in the `Authenticator` – Andrew Tobilko Aug 22 '19 at 19:11
  • @MdAzharuddin no, it does not. You would normally implement an abstract class through... well... a regular class like so: `public class MyAuthenticator extends Authenticator {...}`. Before using advanced frameworks, like JavaEE, I would recommend learning the language basics, e.g. from the [official Oracle tutorial](https://docs.oracle.com/javase/tutorial/). – Turing85 Aug 22 '19 at 19:17
  • @Andrew Tobiko Well I think we need to override ```getPasswordAuthentication()``` otherwise it will return null by default and our gmail via SSL connection (that I am trying to do) will fail. Am I wrong? – Md Azharuddin Aug 22 '19 at 19:21
  • @Turing85 I know that way of handling an abstract class. This was the first time I saw this kind of method of using an abstract method that is what confused me. Well thanks for your concern. – Md Azharuddin Aug 22 '19 at 19:26