Make only child class able to call constructor in java - java

I want to extend a class A and call the constructor of the class A from the child class B with the super() method. Also should the class A only be instantiated via the child class B. I can do that very simply by making class A abstract. But I read that I should only declare classes abstract, when they have at least one abstract method. Is there another way of making class A only be instantiated by the child class B by calling the super() method?

You should use abstract if there are certain implementations your child class needs, especially if you don't want the parent class to be instantiated ever!
However, in your case, I don't think there are any methods that need to be implemented or inherited, so you can just use protected.
Good luck!

Related

Default Superclass in Java

I was reading an article on Inheritance . Few facts were listed on Inheritance but one point i couldn't understand what is Default superclass and its explanation. What is Default superclass?
Default Superclass : Except Object class, which has no superClass, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of the Object class.
The default superclass is Object (more precisely java.lang.Object). If a class does not define a direct superclass explicitly (via extends), then Object is implicitly a superclass of that class.
Have a look at the following example which graphically shows this:
public class A {}
public class B extends A {}
public class C {}
Note that his rule does not apply to Object itself since this would produce a cyclic inheritance. In other words, java.lang.Object is the root of the class hierarchy.
Except Object class, which has no Super Class, every class has one and only one direct superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of the Object class
Points to understand first:
There is a class Object in java which is already available with the JRE libraries.
When you define a class without an extends keyword, your class will by default extend the Object class in Java.
Even if you extend another class in your new class, the parent or its parent transitively inherits the Object class.
Simple way to understand - after you define a class with / without a parent class, create an object for it. If you are using an IDE, you can see that there are some method suggestions which is not implemented in your class (or parent ). Methods like clone() equals() hashCode() wait() etc. Where did these methods/ behaviors come from to your object ? - Yes it came from the ultimate parent Object
The default inheritance is implicit and handled by the Java itself. Hope this makes your understanding better.
object class is base class for every class you create.
when you create an object of your class then constructor of object class is called.
The Object class in Java is the default superclass. Object class is inherited into a newly created class by default if it does not explicitly inherited from any other class. So every class that you create in Java programming is inherently a child class of the Object.
All classes in Java extend Object class by default, and they are subclass of object class, the only exception of this convention is the Object class itself, object class does not extend any class by default, this is the whole idea.

In java why always child class call the super class constructor ? I want to the internal flow

I know how these stuff flow but I want to know when we make the object of child class it calls the parent class no arg constructor first. But i want to know the internal thing.
The parent class constructor needs to be called before the subclass constructor. This will ensure that if you call any methods on the parent class in your constructor, the parent class has already been set up correctly.

Mocking abstract super class method directly from child class

I have a sub class that extends abstract class called AbstractParentClass
like
class Child extends AbstractParentClass
This AbstractParentClass contains a method called getParentAbstractServiceMethod which returns some service class object. That services class has another method called getParentAbstractClassDomainFacade which returns some other class object which is not abstract and so on...This is like method chaining.
Snippet inside Child class is as follows
SessionClass userSession = (SessionClass)
getParentAbstractServiceMethod().getParentAbstractClassDomainFacade().getParentAbstractClassDomainObject(SessionClass.NAME);
How to mock getParentAbstractServiceMethod() method since this is abstract class method I cannot instantiate it and call..
The best solution was to follow the "Favour composition over Inheritance" principle and turn the AbstractServiceClass into a regular class that gets the current extenders as dependencies implementing an interface that provides the method to be called on them.
having written this the less advisable solution is to create a mock of the Abstract class using Mockito:
AbstractParentClass cut= Mockito.mock(AbstractParentClass.class,Mockito.CALLS_REAL_METHODS);
Mockito.when(cut.getParentAbstractServiceMethod()).thenAnswer(...);
I think you should step back and look at your design. What is the point of testing a child class (of an abstract class) ... that doesn't implement the abstract methods in the first place.
I think you should do the following:
A) test your base class as far as possible; for example by creating a test only child class that somehow meaningfully implements the abstract methods
B) make sure that your "real" child classes provide reasonable implementations of your abstract methods.
And hint: consider not putting abstract into your method names. They are only abstract on the parent level; not in the childs that implement them.

Defining an abstract class without any abstract methods

Can I define an abstract class without adding an abstract method?
Of course.
Declaring a class abstract only means that you don't allow it to be instantiated on its own.
Declaring a method abstract means that subclasses have to provide an implementation for that method.
The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around.
Yes you can. The abstract class used in java signifies that you can't create an object of the class. And an abstract method the subclasses have to provide an implementation for that method.
So you can easily define an abstract class without any abstract method.
As for Example :
public abstract class AbstractClass{
public String nonAbstractMethodOne(String param1,String param2){
String param = param1 + param2;
return param;
}
public static void nonAbstractMethodTwo(String param){
System.out.println("Value of param is "+param);
}
}
This is fine.
Yes you can do it. Why don't you just try doing that?
YES You can create abstract class with out any abstract method the best example of abstract class without abstract method is HttpServlet
Abstract Method is a method which have no body, If you declared at least one method into the class, the class must be declared as an abstract its mandatory BUT if you declared the abstract class its not mandatory to declared the abstract method inside the class.
You cannot create objects of abstract class, which means that it cannot be instantiated.
Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.
Yes, you can declare a class you cannot instantiate by itself with only methods that already have implementations. This would be useful if you wanted to add abstract methods in the future, or if you did not want the class to be directly instantiated even though it has no abstract properties.
yes, we can declare an abstract class without any abstract method. the purpose of declaring a class as abstract is not to instantiate the class.
so two cases
1) abstract class with abstract methods.
these type of classes, we must inherit a class from this abstract class and must override the abstract methods in our class,
ex: GenricServlet class
2) abstract class without abstract methods.
these type of classes, we must inherit a class from this abstract class,
ex: HttpServlet class
purpose of doing is although you if you don't implement your logic in child class you can get the parent logic
please check the HttpServlet source code
You can, the question in my mind is more should you. Right from the beginning, I'll say that there is no hard and fast answer. Do the right thing for your current situation.
To me inheritance implies an 'is-a' relationship. Imagine a dog class, which can be extended by more specialized sub types (Alsatian, Poodle, etc). In this case making the dog class abstract may be the right thing to do since sub-types are dogs. Now let's imagine that dogs need a collar. In this case inheritance doesn't make sense: it's nonsense to have a 'is-a' relationship between dogs and collars. This is definitely a 'has-a' relationship, collar is a collaborating object. Making collar abstract just so that dogs can have one doesn't make sense.
I often find that abstract classes with no abstract methods are really expressing a 'has-a' relationship. In these cases I usually find that the code can be better factored without using inheritance. I also find that abstract classes with no abstract method are often a code smell and at the very least should lead to questions being raised in a code review.
Again, this is entirely subjective. There may well be situations when an abstract class with no abstract methods makes sense, it's entirely up to interpretation and justification. Make the best decision for whatever you're working on.
yes you can do that.
declaring class abstract means that class will not be instantiated by any other class.
and there should be at least one abstract method inside that and meaning of that you can declare abstract method in that class if you are not declaring method than its ok.
example:
public abstract class abs {
protected int cx = 0, cy = 0;
public void p() {
System.out.print("hello");
}
}
this will work for sure.
Yes you can. Sometimes you may get asked this question that what is the purpose doing this?
The answer is: sometimes we have to restrict the class from instantiating by its own. In that case, we want user to extend our Abstract class and instantiate child class
Actually there is no mean if an abstract class doesnt have any abstract method . An abstract class is like a father. This father have some properties and behaviors,when you as a child want to be a child of the father, father says the child(you)that must be this way, its our MOTO, and if you don`t want to do, you are not my child.
Yes, you can define an abstract class without an abstract method. However, if there is no method inside you might better go with an interface

Implementing parent class methods with several children class in Java

I have a class (let's call it A) that is extended by several children class (B, C, D, etc.).
In each child class, there are specific methods that I'd like to be accessible from an instantiation of the parent class A.
I tried to declare A as an abstract class, and to declare each child class methods inside as abstract. Then I implemented these methods in their own class but it seems that each child class must implement every method of the parent class. However, I can't do this.
Would you have an idea for this issue?
In each children class, there are specific methods that I'd like to be accessible from an instantiation of the parent class A.
That's not the point of inheritance. Even if you could do this, it shows that your design is broken.

Categories