casting

Inheritance and casting in Java

北慕城南 提交于 2021-02-07 03:26:40
问题 I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes: public class Automobile { public int var; public Automobile () { var = 1; } public String toString () { return "AUTOMOBILE: " + var; } } public class Porsche extends Automobile { public int var; public Porsche () { var = 2; } public String toString () { return "PORSCHE: " + var; } } public class Test { public static void main (String []

Is conversion and promotion the same thing?

蹲街弑〆低调 提交于 2021-02-07 02:01:16
问题 I am not sure if promotion just means converting a data type to a larger data type (for example short to int ). Or does promotion means converting a data type to another "compatible" data type, for example converting a short to an int , which will keep the same bit pattern (the extra space will be filled with zeros). And is conversion means converting something like an int to a float , which will create a completely different bit pattern? 回答1: There are two things that are called promotions:

Avoid casting in inherited java-classes

一世执手 提交于 2021-02-07 00:01:03
问题 I have a class: class MyClass { public MyClass getParent() { ... } public MyClass[] getChildren() { ... } .... } and a subclass MySubClass extends MyClass { public String getId() { } ... } Everytime I used getChildren() or getParent() on an instance of MySubClass , I have to cast the result of theese methods, e.g.: MySubClass sub = new MySubClass(); ((MySubClass)sub.getParent()).getId(); Is there any way (by language or design) to avoid that casting? Thanks for any ideas! Update What I would

Avoid casting in inherited java-classes

十年热恋 提交于 2021-02-07 00:00:57
问题 I have a class: class MyClass { public MyClass getParent() { ... } public MyClass[] getChildren() { ... } .... } and a subclass MySubClass extends MyClass { public String getId() { } ... } Everytime I used getChildren() or getParent() on an instance of MySubClass , I have to cast the result of theese methods, e.g.: MySubClass sub = new MySubClass(); ((MySubClass)sub.getParent()).getId(); Is there any way (by language or design) to avoid that casting? Thanks for any ideas! Update What I would

Avoid casting in inherited java-classes

随声附和 提交于 2021-02-07 00:00:36
问题 I have a class: class MyClass { public MyClass getParent() { ... } public MyClass[] getChildren() { ... } .... } and a subclass MySubClass extends MyClass { public String getId() { } ... } Everytime I used getChildren() or getParent() on an instance of MySubClass , I have to cast the result of theese methods, e.g.: MySubClass sub = new MySubClass(); ((MySubClass)sub.getParent()).getId(); Is there any way (by language or design) to avoid that casting? Thanks for any ideas! Update What I would

Using superclass type for subclass instance

╄→尐↘猪︶ㄣ 提交于 2021-02-06 03:10:59
问题 I know this question has been asked a lot, but the usual answers are far from satisfying in my view. given the following class hierarchy: class SuperClass{} class SubClass extends SuperClass{} why does people use this pattern to instantiate SubClass: SuperClass instance = new SubClass(); instead of this one: SubClass instance = new SubClass(); Now, the usual answer I see is that this is in order to send instance as an argument to a method that requires an instance of SuperClass like here:

Using superclass type for subclass instance

一曲冷凌霜 提交于 2021-02-06 03:10:15
问题 I know this question has been asked a lot, but the usual answers are far from satisfying in my view. given the following class hierarchy: class SuperClass{} class SubClass extends SuperClass{} why does people use this pattern to instantiate SubClass: SuperClass instance = new SubClass(); instead of this one: SubClass instance = new SubClass(); Now, the usual answer I see is that this is in order to send instance as an argument to a method that requires an instance of SuperClass like here:

Using superclass type for subclass instance

丶灬走出姿态 提交于 2021-02-06 03:08:26
问题 I know this question has been asked a lot, but the usual answers are far from satisfying in my view. given the following class hierarchy: class SuperClass{} class SubClass extends SuperClass{} why does people use this pattern to instantiate SubClass: SuperClass instance = new SubClass(); instead of this one: SubClass instance = new SubClass(); Now, the usual answer I see is that this is in order to send instance as an argument to a method that requires an instance of SuperClass like here:

Using superclass type for subclass instance

ε祈祈猫儿з 提交于 2021-02-06 03:05:51
问题 I know this question has been asked a lot, but the usual answers are far from satisfying in my view. given the following class hierarchy: class SuperClass{} class SubClass extends SuperClass{} why does people use this pattern to instantiate SubClass: SuperClass instance = new SubClass(); instead of this one: SubClass instance = new SubClass(); Now, the usual answer I see is that this is in order to send instance as an argument to a method that requires an instance of SuperClass like here:

Subtracting 1 day from a timestamp date

删除回忆录丶 提交于 2021-02-05 14:13:16
问题 I am using Datagrip for Postgresql. I have a table with a date field in timestamp format (ex: 2016-11-01 00:00:00) . I want to be able to: apply a mathematical operator to subtract 1 day filter it based on a time window of today-130 days display it without the hh/mm/ss part of the stamp (2016-10-31) Current starting query: select org_id, count(accounts) as count, ((date_at) - 1) as dateat from sourcetable where date_at <= now() - 130 group by org_id, dateat The ((date_at)-1) clause on line 1