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 4 days ago.
Improve this question
I have a abstract class with two properties:
abstract class Animal(alive: Boolean, age: Int) {
abstract fun getName() : String
}
I would like to have a derivative class which set alive = true by default.
class Dog : Animal {
constructor(age: Int) : super(true, age) {
this.age = age
this.alive = true
}
}
Is following a good practice?
abstract class Animal(var age: Int) {
abstract var alive: Boolean
}
class Dog(age: Int, override var alive: Boolean = true) : Animal(age) {
}
It is up to you if you need alive in all the derived classes you can have it in the abstract class. If don't you then it's good to have data or behavior in the derived class.
But keep in mind alive will not be accessible with your abstract class reference because you introduced it in the derived class.
Usually, if you add some behavior or data to the derived class you make it private.
Other than that it is up to you. It is not a bad practice.
Related
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 1 year ago.
Improve this question
I'm wondering how I would get a class from a java.lang.Object object to a specific class, such as a Cat class I may write; not directly casting to Cat, but to automatically get the main class of the object and cast to the main class, since I may take in Object arguments to take in many kinds of classes. Would getting an object through Class<T>, getting T (the type of the main class) be possible?
Here's what I mean:
class Cat {
public String meowMessage = "meow";
public void meow() {
System.out.println(meowMessage);
}
}
public class Main {
public static void main(String[] args) {
Object obj = new Cat();
// Cat c = obj.getClass()...;
// ^ how to get the Class object and automatically cast to the main class (Cat)?
}
}
What you want would involve determining a generic parameter at runtime. This is not possible due to type erasure.
If I understand correctly, you need instanceof so you can write:
if (obj instanceof Cat) {
Cat cat = (Cat) obj;
cat.meow();
} else {
//do something else
}
It's not possible to cast to a type which is not known compile time and I don't think it would make any sense because casting is needed to access the members of the class instance statically from the code. But how to know what is accessible if the class is unknown?
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 2 years ago.
Improve this question
Imagine I have an interface like below:
public interface DataChecker<T, C> {
default ProcessResult beforeProcess(T record, CountertHelper countertHelper, C config){
return new ProcessResult(false, Collections.emptyList());
}
default List<String> checkData(T record, CountertHelper countertHelper, C config) {
return Collections.emptyList();
}
#RequiredArgsConstructor
#Getter
class ProcessResult {
private final boolean skip;
private final List<String> keys;
public ProcessResult(boolean skip) {
this.skip = skip;
this.keys = Collections.emptyList();
}
}
}
Some implementer of this interface may only implement beforProcess method and some others may only implement checkData method and some others may chose to implement both of them.
Is it a good practice to make both of them default? Or more generally is it a good practice to have an interface which has default implementation for all of its method.
As name implies, default methods in java 8 are simply default. If you do not override them, they are the methods which will be invoked by caller classes.
In you case if the Some class only want beforProcess and do not want checkData and vice versa I suggest you to split ur interface into 2
like
public interface DataChecker<T, C> {
List<String> checkData(T record, CountertHelper countertHelper, C config);
and
public interface DataProcessor<T, C> {
ProcessResult beforeProcess(T record, CountertHelper countertHelper, C config);
Now the approach is cleaner and the classes override one or both if implemented both hence a cleaner approach.
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 3 years ago.
Improve this question
Lets say I have three classes, A,B,C. B inherits A, is there a way to access the getVal method of Class A, in C?
class A {
getVal method
}
class B extends A {
}
Class C {
main() {
B x = new B
x.getVal?
}
Yes. Assuming the classes remain in the same package - x.getVal will work.
class A {
String getVal(){
return "from a";
}
}
class B extends A {
}
public class C {
public static void main(String [] args) {
B x = new B();
x.getVal();
}
}
It works - because of the default access modifier. Use the protected access modifier for inheritance.
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 4 years ago.
Improve this question
I am new to Java OOP and have been studying about this reading a GitBook called AP Computer Science in Java
I found this page very confusing:https://codehs.gitbooks.io/apjava/content/Classes-And-Object-Oriented-Programming/class-design-and-abstract-classes.html
the code is like this
public abstract class VehicleClass {
private String type;
private String vehicleName;
public VehicleClass(String vType, String vName) {
type = vType;
vehicleName = vName;
}
/* This will need to be abstract, since
* we will need to implement different formulas
* depending on if the vehicle is electric or
* gas powered.
*/
public abstract double getMileage();
}
/* As you can see, in both classes, we have `getMileage` implemented
* with different formulas.
*/
public class Truck extends VehicleClass {
private double gasTankCapacity;
private double milesPerTank;
public Truck(double capacity, double miles) {
gasTankCapacity = capacity;
milesPerTank = miles;
}
public double getMileage() {
return milesPerTank/gasTankCapacity;
}
}
public class ElectricCar extends VehicleClass {
private double maxCharge;
private double milesPerCharge;
private double maxEnergyUsage;
public ElectricCar(double charge, double maxEnergy, double milesCharge) {
maxCharge = charge;
maxEnergyUsage = maxEnergy;
milesPerCharge = milesCharge;
}
public double getMileage() {
return (maxCharge*milesPerCharge)/maxEnergyUsage;
}
}
MY questions are:
1. Why does he build the constructor of the two child classes without using super(). Doesn't he need to pass type and vehicleName to the super-class's constructor?
2. Why does he make type and vehicleName in the super-class to be private? I know that child class cannot inherit instances variables from the super class. Why doesn't he use protect instead?
You are totally right! That code will not compile.
It's hard to say without knowing the purpose of those variables and of the class.
You're right, and it doesn't compile for that reason.
You may only omit parameterless calls to super, if there is such a constructor in the super class, in which case it is automatically inferred.
Speculation about the reason: The author wanted to show something else (as stated in the comments), maybe first had a working solution but then decided, that the vtype code bloated the example too much up and removed it, without testing again.
To Q.2, why does he make type and vehicleName in the superclass to be private?
I would say, you inherit instance variables, private or not, you just don't have direct access to them.
Why doesn't he use protect instead?
We can't tell. Since it is only demo code, and the private members aren't used and not accessible via getter or setter, we just can't tell. Maybe it is an example which is later extended and then they get used?
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
Is there a way to insantiate a variable that encompasses all subclasses of a class? From what I've read so far we must state what type the variable is before setting it equal to something:
Example:
ExampleObject1 object = reference to the object
But what if we wanted to make it so that we could set the variable to any instance or subclass of that object?
Yes, you can already do that.
A variable of type T (as long as T is a class/interface/enum/annotation) can hold a reference to any instance of the class T, or any instance of a class that extends or implements T.
For example, this works:
class MyClass1 {
// ... stuff goes here ...
}
class MyClass2 extends MyClass1 {
// ... stuff goes here ...
}
class Main {
public static void main(String[] args) {
MyClass1 object = new MyClass2();
}
}