Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class changed to abstract #1

Closed
wants to merge 1 commit into from
Closed

class changed to abstract #1

wants to merge 1 commit into from

Conversation

Yousha
Copy link

@Yousha Yousha commented Sep 6, 2016

Use abstract instead of using constructor with throw.

Use `abstract` instead of using constructor with `throw`.
@nisrulz
Copy link
Owner

nisrulz commented Sep 6, 2016

@Yousha can you please explain your PR and why would I want to make my classes abstract.

@Yousha
Copy link
Author

Yousha commented Sep 6, 2016

To prevent instantiation of class...

And private constructor is NOT the right way, because:
1- One way to bypass the restriction is to use reflections.
2- AS a rule, the reason for making a constructor private is to CONTROL or limit instantiation, not to prevent instantiation.
3- If it's a nested class, you can access it from the enclosing class...

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.
http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

@nisrulz
Copy link
Owner

nisrulz commented Sep 6, 2016

@Yousha Well private constructor is in place to prevent instantiation in my library.

Here is some explanation

Defining a constructor with the private modifier says that only the native class (as in the class in which the private constructor is defined) is allowed to create an instance of the class, and no other caller is permitted to do so. There are two possible reasons why one would want to use a private constructor – the first is that you don’t want any objects of your class to be created at all, and the second is that you only want objects to be created internally – as in only created in your class.

Which in my case is exactly what I want to do. I only want the objects be instantiated internal to the library and only only be referenced statically. This Library contains Utility classes which are in general always created as statics in applications. I have just put them under one library for me to be able to access them easily in multiple projects.

With that said , I only want that people should access them statically.

@Yousha
Copy link
Author

Yousha commented Sep 6, 2016

With that said , I only want that people should access them statically.

People can still access them statically by using abstract class.

But as you want... :)

@Yousha Yousha closed this Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants