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
I was going through the Object class JAVA-docs and as we know Object is base class of all classes. But i was wondering, When Object class is added as super class?
Is it compile time or runtime(believe ideally it should be at compile time)?
Also as i have read JVM automatically checks if a class is inheriting from parent class then it will not add Object as super class to avoid Inheritance-diamond problem but what will happen in case of abstract class/ inner classes?
Where will be Super class-Object will be added ?
While creating any new built in objects example HashMap we can see the internal working from the source, Similarly is it possible to see this functionality anywhere in Java source code or it is done by compiler internally ?
If you don't specify a super class, the default is java.lang.Object. And the super class is determined at compile time. In the case of an abstract (an inner or a static) class, without an explicit super type; the super type is java.lang.Object.
Related
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.
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.
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 3 years ago.
Improve this question
Oracle Java Tutorials, Multiple Inheritance of State, Implementation, and Type:
One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. For example, suppose that you are able to define a new class that extends multiple classes. When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. What if methods or constructors from different superclasses instantiate the same field?
Couldn't this problem be easily solved by assigning priorities to all the classes a subclass extends?
class A extends B,C,D
{
}
Let us assume that priority was assigned in the ascending order in which they are mentioned in declaration of subclass .
Then priority of B < priority of C < priority of D . So if there is any state or behaviour common to any of these classes B ,C or D then priority decides what should be inherited and what should be hided.
Please advise. Thanks in advance.
Yes, roughly speaking, this how Scala supports extending a class with multiple Traits
"priorities" are determined based on a linearization equation, similar to what you mentioned so there is no ambiguity.
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.
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