class Vehicle {
public void show() {
System.out.println("Parent");
}
}
class Car extends Vehicle{
public int show() {
System.out.println("Child");
return 3;
}
}
When I am trying to override Car class' method in child class, it is showing error but when I specify a parameter in the method, the error goes away.
Related
i'm confused creating Laravel Model classes for my database schema. How to create relationships?
The tables:
table_phone
id
imei
model_id
table_model
id
name
photo_id
manufacturer_id
table_manufacturer
id
name
photo_id
The Data Classes:
class Phone extends Model {
function model() {
}
}
class PhoneModel extends Model {
function getPhones() {
}
function manufacturer() {
}
}
class PhoneManufacturer extends Model {
function getPhones() {
}
function getModels() {
}
Phone class has one PhoneModel Class (one-to-one).
PhoneModel class has one PhoneManufacturer Class (one-to-one).
PhoneManufacturer class should have a method getModels(), which should return all PhoneModel class associated with the manufacturer_id.
PhoneManufacturer class should have a method getPhones(), which should return all Phone class associated with the manufacturer_id in the PhoneModel Class.
PhoneModel Class should have a method getPhones() which should return all PhonePhone class associated with the model_id.
Phone class should have model method and manufacturer method.
PhoneModel class should have manufacturer method.
You have to create relationship in this way:
class Phone extends Model {
public function phonemodel()
{
return $this->hasOne(PhoneModel::class)
}
}
class PhoneModel extends Model {
public function phone()
{
return $this->belongsTo(Phone::class)
}
public function manufacturer()
{
return $this->belongsTo(Manufacturer::class)
}
}
class Manufacturer extends Model {
public function model()
{
return $this->hasMany(PhoneModel::class)
}
}
I have a method ResetMethod(ClassA a) in a class and I have accessed this method by property of ResetMethod's class like this:
public class MyClass1
{
public MyClass1()
{
}
public void ResetMethod(ClassA a)
{
}
}
public class MyClass2
{
MyClass1 class1;
public MyClass2()
{
ClassA a= new ClassA();
MyClass1.ResetMethod(a);
}
public MyClass1 MyClass1
{
get
{
if (myClass1 == null)
myClass1 = new MyClass1 ();
return myClass1 ;
}
set
{
myClass1 = value;
}
}
}
While running FxCop rules, for method ResetMethod, it shows this error:
The 'this' parameter (or 'Me' in Visual Basic) of 'MyClass1.ResetMethod(MyClassA)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body or at least one property accessor, if appropriate.
How do I resolve this error?
You invocation of MyClass1.ResetMethod(a); is already calling a static method. So the code you posted does not compile to my understanding.
So all that's left to the do is to make the method itself static:
public static void ResetMethod(ClassA a)
{
// ...
}
If I have the Grandparent, how do I get a single collection of its' parents' children?
Grandparent
`Class Grandparent extends Model
{
public function parents()
{
return $this->hasMany('\App\Parent');
}
}`
Parent
`Class Parent extends Model
{
public function grandparent()
{
return $this->belongsTo('\App\Grandparent');
}
public function children()
{
return $this->hasMany('\App\Child');
}
}`
Child
`Class Child extends Model
{
public function parent()
{
return $this->belongsTo('\App\Parent');
}
}`
A Collection of Collections could also work.
I get Exception
org.springframework.beans.NotReadablePropertyException: Invalid property 'entries[0].reason' of bean class [my.company.data.SDROrder]: Bean property 'entries[0].reason' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
from the following code snippet:
Errors errors = new BeanPropertyBindingResult(new SDROrder(), "sdr");
orderValidator.validate(order, errors);
for validator:
public class OrderValidator implements Validator
{
#Override
public boolean supports(Class<?> clazz)
{
return Order.class.isAssignableFrom(clazz);
}
#Override
public void validate(final Object target, final Errors errors)
{
errors.rejectValue("entries[0].reason", "Wrong Reason");
}
}
where we have such data hierarchy
public class Order
{
private List<AbstractOrderEntry> entries;
public List<AbstractOrderEntry> getEntries()
{
return entries;
}
public void setEntries(List<AbstractOrderEntry> entries)
{
this.entries = entries;
}
}
public class SDROrder extends Order
{
}
public class AbstractOrderEntry
{
}
public class SDROrderEntry extends AbstractOrderEntry
{
private String reason;
public String getReason()
{
return reason;
}
public void setReason(String reason)
{
this.reason = reason;
}
}
Please see working example here: here
Update 1: Just to clarify. The problem is I try to rejectValue on object that has Collection of objects where each element has specific attribute at Runtime but has not it at Compile time. Spring uses Bean's properties to resolve these fields and can't find inherited attribute. The question is: can I explain Spring to resolve inherited fields somehow?
I found the solution here.
The trick is at
org.springframework.validation.Errors.pushNestedPath(String)
and
org.springframework.validation.Errors.popNestedPath()
methods.
The correct validation should be done as follow:
errors.pushNestedPath("entries[0]");
errors.rejectValue("reason", "Wrong Reason");
errors.popNestedPath();
I have two controller classes extending basecontroller. I have a common functionality to be implemented and implementation is in basecontroller.
public class BaseController{
protected void populateWidget(List li, int zipcode){
//implementation for child A
populate only 10 employee records
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
populateWidget(li, 90034)
}
I have written setChildB method and working on it. like below
public class BaseController{
protected boolean childB;
protected void setIsChildB(boolean childB){ this.childB = childB;}
protect boolean isChildB(){ return childB; }
protected void populateWidget(List li, int zipcode){
//implementation for child A
if(!isChildB())
populate only 10 employee records
else
//implementation specific to child B
populate 100 student records
}
}
public class ChildA extends BaseController{
List<Employees> li = ...
populateWidget(li, 90034)
}
public class ChildB extends BaseController{
List<Students> li = ...
setChildB(true); //setting as child B call
populateWidget(li, 90034)
}
Please advice the best way to do it.
I recommend implementing a Template Method Pattern. You could have your base controller like this:
public class BaseController {
protected abstract void getPeople(List li, int zipcode);
protected void populateWidget(List li, int zipcode) {
//Do generic logic here
//Refer to child
getPeople(List li, int zipcode)
//Do some more generic stuff
}
}
And then each of your children like this:
public class ChildA extends BaseController {
#Override
protected void getPeople(List li, int zipcode) {
//Specific Logic here
}
}
The children each override the getPeople() method to do their own thing.
That way if you need a ChildC, you can simply extend the parent without changing it.
Update: Based on the comments, here is an alternative way of doing this.
I would not do it as a setter, rather I would make a constructor argument on the base class that takes the type of child as an argument. This makes sure that the compiler enforces that you will always have a value set
You could use a boolean but I recommend strongly using an Enum, String constant or int constant.
The above two points are mostly because I always anticipate that I might have to do a ChildC when some requirements change comes along and at that point you won't have to do changes to the base class. It also falls into the set of patterns that other programmers I work with would expect and therefore makes the code easier readable and maintainable by others.
Sample base class:
public class BaseController {
private int childType;
protected static final int CHILD_A = 1;
protected static final int CHILD_B = 2;
public BaseController(int aChildType) {
childType = aChildType;
}
protected void populateWidget(List li, int zipcode) {
switch (childType) {
case CHILD_A:
//Handle Child A
case CHILD_B:
//Handle Child B
}
}
}
The child implementation would look like this:
public class ChildA extends BaseController {
public ChildA () {
super(CHILD_A);
}
//The rest of your code goes here...
}
Note, that in the above example I am using integer constants for the sake of brevity. In the 21s century Java creating an Enum would be the preferred way to go.