Java code for composition [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to implement composition in Java?
If we create a class A and use an instance of this class in class B, then how can we ensure that A cannot create an independent object by itself?

I do not think it is possible. The only similar solution is to create A as private inner class of B. Then nobody else than B can instantiate A.

You could have your composed classes as an inner class of your container class.
class ContainerClass{
class ContainedClass1{
}
class ContainedClass2{
}
}
This way they are tied to an instance of ContainerClass and cannot be created without an instance of the same. You can make them private, then they can only be created within the class

If I understand you question correctly, what you want is an inner class. Essentially to create an inner class you write class A inside class B.

You can create class A as static inner class of B and keep the constructor of A as private. Then only B can use this constructor.
Some links for reference:
scope of private constructor in Nested Class

Maybe you can use classLoaders to determine if A class loaded from B or not

Related

Issues related to declaration of private constructor in a class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm new to Java.
I was reading about Encapsulation concept in Object Oriented Programming.
While reading, I saw a line telling that :
If constructor of a class is declared as private, it will cause some
problems.
But it didn't tell what kind of problems could occur.
Can anyone tell what kind of problems are there with private constructor ?
When you declare a constructor of a class as 'private', you will not be able to create new instances "objects" of that class.
This is a problem if you do want to create new instances of the class, but this implementation is useful when making a Singleton Design Pattern.
If you are interested in knowing more about design patterns, I can share some resources with you.
Here is a book by Carlos E. Otero that covers the topic of design patterns:
https://books.google.com.tr/books?id=IL6FBLJn69UC&printsec=frontcover&redir_esc=y#v=onepage&q&f=false
Simple words, object or instance can't be created for that class.
As soon as, you write this statement below. For example
SomeClass c = new SomeClass(); // this will give an exception.
Basically, private constructors are used for making singleton classes.
On declaring a constructor as private you can't instantiate the class using default constructor. So, you need to create a constructor with public to access it outside.

Can anyone help me regarding mulitple inheritance in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
class A{}
class B extends A{}
here is it the case that B is extending A alongwith object class.
We all know, in java, all classes by default inherits Object class.
But it is also there that multiple inheritance is not allowed in java.
Then when we inherit a class, what happens with this rule?
We all know, in java, all classes by default inherits Object class.
Yes. But I suspect that you don't understand what that really means.
What it means is that if a class is not declare (via an explicit extend) to inherit from some class, then it implicitly inherits from Object.
class A {} // implicitly inherits Object
class B extends A {} // explicitly inherits A
To say this in other words:
A has Object as its only direct superclass
B has A as its only direct superclass.
B has Object as an indirect superclass.
This is single inheritance. Multiple inheritance would be if B had A and Object as direct superclasses.

How can a class extend and implement other classes and interfaces at the same time? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't get how can I write a sub class which will extend a superclass and a couple of interfaces at the same time.
A class can only extend one superclass, but can implement multiple interfaces, which is done by separating them by a comma, like so:
public class MyClass extends AnotherClass implements AnInterface, AnotherInterface
Extending a class is simple: unless the class you are extending is abstract, your class does not even need to do anything apart from providing a suitable constructor.
Implementing an interface or extending an abstract class, on the other hand, is simply a promise to implement a specific set of methods, as declared in the corresponding abstract class and/or the interface. Again, all your class needs to do is providing some implementation for these methods.
Of course, the implementations need to make sense in the overall scheme of your design, but technically they just need to be there in order for the code to compile.

why static methods and variables are accessible without any instance of class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Why is it that all static methods and variables are accessible without any instance of class and non-static members need instances to get access.
Actually, there is a class object representing each class in the JVM. So, the line Why is it that all static methods and variables are accessible without any instance of class is incorrect.
The JVM creates class objects (different from class instances) representing classes.
Example : String.class, Class.class etc
When We create a class that means we are creating 'Bunch of Objects(instances) of Same type',for those objects,methods are remains same but data and memory location changes and it is unique for each object.But When We use 'Static' variable or Method ,It creates Only ONE COMMON COPY in whole program.So it is same for all instances/objects and changes done in static method/variable is visible to all objects. hence,we can use it directly or without instance of class.in same way,non static members are different with respect to every object,so we need instance of class for it.

Singleton class, private variable or method parameter. Performance [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I got 3 classes, A,B and C.
Only 1 instance of class A exits at any given time but hundreds of objects of class B and C will exist.
Class A calls a method of class B.
Class B calls a method of class C.
And class C eventually calls a method of the class A object.
What would be the best and cleanest way to provide the reference to the class A object in C?
Saving it in a private variable and initializing it via the constructor?
Creating a static getInstance() method in Class A which returns the object itself?
Or passing "this" through parameters from A to B to C?
Any C Object will call the method of A multiple time in his lifetime.
The most convenient way is certainly #2. The "cleanest" is probably #1, or #3, depending on the circumstances.
The Singleton (#2) pattern has some problems. That does not mean you should avoid it at all costs. But you should be aware of the implications.
For some information on the topic see What is so bad about singletons?
I would suggest create a static getInstance() method in Class A which returns the object itself.
Option 2. Have your Singleton class contain the instance privately with a private constructor and return the instance using get instance. This the commonly used pattern.
Option 1 makes the constructor open to other methods which is not needed since it should only be called once (if I'm reading the option correctly). Option 3 will clutter your code.

Categories

Resources