Related to super and sub class - default-constructor

I'm a beginner in java
class A{
{
Sopln("IB in A");
}
static {
Sopln(" SB in A");
}
A() {
Sopln(" delt in A);
}
}
class B extends A {
{
Sopln(" IB in A");
}
static {
Sopln(" sb in B");
}
B() {
Sopln(" deflt in B");
}
}
class Hello {
Psvm(String arg[]){
B b1= new B();
}
}
Actual output:-
Sb in A
Sb in B
Ib in A
Deflt in A
Ib in B
Deflt in B
But according to me.....if class A loaded first then all members of class A process first and then members of B
If class B loaded first then all members of class B are processed first then all members of B
Please explain me the reason behind this

Related

Cannot invoke "Widget.incrementWaitTime(double)" because "<local4>" is null

Assignment due tomorrow at 27/05/2022. Ran into this problem that I have no idea what it means. Something in the array is null maybe?
public class ISStorage
{
private ArrayList<Widget> queue;
public ISStorage (String id, int size)
{
...
queue = newArrayList<Widget>();
...
}
...
public void updateWaitTimes(double increment)
{
for (Widget w : queue)
{
w.incrementWaitTime(increment);
}
}
...
}
public class Widget
{
...
public void incrementWaitTime(double increment)
{
waitTime = waitTime + increment;
}
...
}
This is the related code, I believe. If you need more, just ask.

What is an example of an accessor method?

I got this question from a worksheet my AP Computer Science teacher had on his worksheet:
class Exam{
private int myA, myB;
private final int MAX = 100;
public Exam( ) { myA = myB = 100; }
public Exam ( int a, int b ) { myA = a; myB = b; }
public void setA(int a) { myA = a; }
public void setB(int b) { myB = b; }
public int getA() { return myA; }
public int getB() { return myB; }
public String toString( ) { return getA() + " " + getB(); }
}
How many accessor methods are in Exam?
Three are accessor methods. An accessor "accesses" the variable. Only three of them directly return the private variables. All of the others are mutators because they "mutate" the variables. The last accessor listed below is not a great accessor because it doesn't follow encapsulation best practices.
These are the accessors.
public int getA() { return myA; }
public int getB() { return myB; }
public String toString( ) { return getA() + " " + getB(); }

Access to a property with Interface cast

ActionBase, ActionA, ActionB and ActionC are Entities (from a database). ActionA, ActionB and ActionC are derived type of ActionBase.
ActionB and ActionC implements ISpecialAction with a SpecialProperty.
ex :
public interface ISpecialAction
{
Guid SpecialProperty { get; }
}
public partial class ActionBase
{
public objectX OnePropertyBase { get; set; }
}
public partial class ActionA : ActionBase
{
public objectY OnePropertyA { get; set; }
}
public partial class ActionB:ActionBase,ISpecialAction
{
public objectZ OnePropertyB { get; set; }
public Guid SpecialProperty
{
get
{
return OnePropertyB.ID;
}
}
}
public partial class ActionC : ActionBase ,ISpecialAction
{
public objectW OnePropertyC { get; set; }
public Guid SpecialProperty
{
get
{
return OnePropertyC.ID;
}
}
}
My problem is that SpecialProperty is build from other Properties of the objects (ActionB or ActionC) and when the cast (to ISpecialAction) is done, OtherProperty and OtherProperty2 are null.
I tried :
GetActionBase().ToList().Where(x=>x is ISpecialAction && ((dynamic) x).SpecialProperty== p_SpecialProperty);
GetActionBase().ToList().Where(x=>x is ISpecialAction && ((ISpecialAction) x).SpecialProperty== p_SpecialProperty);
GetActionBase().ToList().OfType<ISpecialAction>().Where(x => x.SpecialProperty== p_SpecialProperty).Cast<ActionBase>();
return GetActionOnGoing().ToList().OfType<ICityAction>().Cast<ActionBase>().Where(x => ((dynamic)x).CityId == p_CityId);
remark : OfType<> doesn't works with an Interface in Linq to entities but is ok in Linq to object
How do I access my property interface without knowing the type of the object?
I might missed something but this is Ok with the code you provided :
public class objectX
{
}
public class objectY
{
}
public class objectZ
{
public Guid ID { get { return Guid.NewGuid();} }
}
public class objectW
{
public Guid ID { get { return new Guid(); } }
}
class Program
{
private static Guid p_SpecialProperty;
static void Main(string[] args)
{
var result = GetActionBase().ToList().Where(x => x is ISpecialAction && ((dynamic)x).SpecialProperty == p_SpecialProperty).FirstOrDefault();
var result1 = GetActionBase().ToList().Where(x => x is ISpecialAction && ((ISpecialAction)x).SpecialProperty == p_SpecialProperty).FirstOrDefault();
var result2 = GetActionBase().ToList().OfType<ISpecialAction>().Where(x => x.SpecialProperty == p_SpecialProperty).Cast<ActionBase>().FirstOrDefault();
}
private static IEnumerable<ActionBase> GetActionBase()
{
return new List<ActionBase> {new ActionA{OnePropertyA= new objectY()}, new ActionB{OnePropertyB=new objectZ()},new ActionC{OnePropertyC=new objectW()} };
}
}
Not sure if I exactly understand your question, but could you try using an intermediate interface, such as:
public interface ISpecialActionB : ISpecialAction
{
objectZ OnePropertyB { get; set; }
}
public class ActionB : ActionBase, ISpecialActionB
{
//same stuff
}
and casting to that instead.
var b = new ActionB{OnePropertyB = new Whatever()};
var bAsSpecial = b as ISpecialActionB;
var whatever = b.OnePropertyB; // should not be null
It' ok.
Your example run very well without problem so I searched in a other way : AutoMapper.
l_List.Actions = Mapper.Map<List<ActionBase>, Action[]>(l_ActionManagement.GetActionBySpecialId(l_Special.ID).ToList());
The problem was not interfaces or Linq queries but it was that automapper need an empty constructor and in this constructor, I need to initialize OnePropertyB and OnePropertyC to compute SpecialProperty.
Thanks

Using ValueInjecter, is there a way to only inject a given property one time

Given 3 classes,
A, and B which each have an ID property, and then various other properties
and C, which has an ID, and the combined properties of A and B,
I want to
C.InjectFrom(A);
C.InjectFrom(B);
such that the ID from A is preserved and not overwritten by B.
Obviously in this simple case, I could just reverse the order of the two calls, but in my real world example, it is slightly more complicated where I cannot just solve the problem with ordering.
Esentially I want the second injection to ignore anything that the first injection has already handled, and this may be continued down a chain of several injections. Some of these injections may be from the same objects too
C.InjectFrom(A);
C.InjectFrom<SomeInjector>(A);
C.InjectFrom<SomeInjector2>(A);
C.InjectFrom<SomeInjector3>(A);
etc.
here you go:
using System;
using System.Collections.Generic;
using Omu.ValueInjecter;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var a = new { Id = 1, P1 = "p1" };
var b = new { Id = 2, P2 = "p2" };
var c = new C();
var propList = new List<string>();
c.InjectFrom(new HandlePropOnce(propList), a);
c.InjectFrom(new HandlePropOnce(propList), b);
Console.WriteLine("Id = {0} P1 = {1} P2 = {2}", c.Id, c.P1, c.P2);
}
}
public class C
{
public int Id { get; set; }
public string P1 { get; set; }
public string P2 { get; set; }
}
public class HandlePropOnce : ConventionInjection
{
private readonly IList<string> handledProps;
public HandlePropOnce(IList<string> handledProps)
{
this.handledProps = handledProps;
}
protected override bool Match(ConventionInfo c)
{
if (handledProps.Contains(c.SourceProp.Name)) return false;
var isMatch = c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type;
if (isMatch) handledProps.Add(c.SourceProp.Name);
return isMatch;
}
}
}

Key comparisons for Linq GroupBy using Default EqualityComparer

I'm trying to do a Linq GroupBy on some objects using an explicit key type. I'm not passing an IEqualityComparer to the GroupBy, so according to the docs:
The default equality comparer Default is used to compare keys.
It explains the EqualityComparer<T>.Default property like this:
The Default property checks whether
type T implements the
System.IEquatable<T> generic interface
and if so returns an
EqualityComparer<T> that uses that
implementation.
In the code below, I'm grouping an array of Fred objects. They have a key type called FredKey, which implements IEquatable<FredKey>.
That should be enough to make the grouping work, but the grouping is not working. In the last line below I should have 2 groups, but I don't, I just have 3 groups containing the 3 input items.
Why is the grouping not working?
class Fred
{
public string A;
public string B;
public FredKey Key
{
get { return new FredKey() { A = this.A }; }
}
}
class FredKey : IEquatable<FredKey>
{
public string A;
public bool Equals(FredKey other)
{
return A == other.A;
}
}
class Program
{
static void Main(string[] args)
{
var f = new Fred[]
{
new Fred {A = "hello", B = "frog"},
new Fred {A = "jim", B = "jog"},
new Fred {A = "hello", B = "bog"},
};
var groups = f.GroupBy(x => x.Key);
Debug.Assert(groups.Count() == 2); // <--- fails
}
}
From MSDN
If you implement IEquatable, you should also override the base class implementations of Object::Equals(Object) and GetHashCode() so that their behavior is consistent with that of the IEquatable::Equals method. If you do override Object::Equals(Object), your overridden implementation is also called in calls to the static Equals(System.Object, System.Object) method on your class. This ensures that all invocations of the Equals() method return consistent results.
add this to FredKey and it should work
public override int GetHashCode()
{
return A.GetHashCode();
}
Here is a complete example with a Fiddle. Note: the example differs slightly from the question's example.
The following implementation of IEquatable can act as the TKey in GroupBy. Note that it includes both GetHashCode and Equals.
public class CustomKey : IEquatable<CustomKey>
{
public string A { get; set; }
public string B { get; set; }
public bool Equals(CustomKey other)
{
return other.A == A && other.B == B;
}
public override int GetHashCode()
{
return string.Format("{0}{1}", A, B).GetHashCode();
}
}
public class Custom
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
public static void Main()
{
var c = new Custom[]
{
new Custom {A = "hello", B = "frog" },
new Custom {A = "jim", B = "jog" },
new Custom {A = "hello", B = "frog" },
};
var groups = c.GroupBy(x => new CustomKey { A = x.A, B = x.B } );
Console.WriteLine(groups.Count() == 2);
}

Resources