class A
{
}
class B extend A
{
int i;
int j;
}
Can a class have an IS-A Relationship with itself?
In this question, B is an A, right?
But can class B have an IS-A relationship with class B?
It's an identity (and tautology) - an object of type B will always be able to describe itself as an object of type B.
The further extensions of the is-a relationship pertain to hierarchies of inheritance; that is to say, since B extends A, B is-an A. This allows you to write the following expression:
A anA = new B();
But B is a B too. It hasn't lost that part of its identity because it now inherits from another class.
Related
Say I have an Outer class A and it's nested Inner Class B. A is subclass of C. Then is B also a subclass of C? Since B can access all private members of A and thus that(public and protected) of it's superclass, so I think B becomes a subclass of C.
Is my line of thinking right?
Any help is highly appreciated.
Here's what you seem to be describing:
class C {}
class A extends C {
static class B {}
}
Being a nested class in A does not make B a subclass of C. Access to members is not the defining characteristic of being a subclass.
Suppose you try to assign an object of type B to a variable of type C.
C c1 = new A(); // OK -- A is a subclass of C
C c2 = new A.B(); // Not OK
If B were a subclass of C, the latter would be a legal assignment. But it is not.
I don't think so. If you want B to extend C, you may explicitly say:
class C {}
class A extends C {
static class B extends C {
//Todo...
}
}
The nested class B is just a (static) member of A just like any other member of it. You can access public and protected or even package access methods and variables of C within B because A has inherited those members from C. Since B is a nested class of A, You can access (static) members of A which can be inherited from other class or not.
I think the title is self explanatory.
So suppose I have this code:
interface A { }
abstract class B { }
class C { }
C c = new C();
System.out.println(c instanceof A); //fine
System.out.println(c instanceof B); // compile error
In a comment from the question I read this:
The compiler can never know whether a given type doesn't implement an interface because a potential subclass could implement it.
So if for interface this works, why it should not work for an abstract class ? It also should be extended by some other class, as it can't exist by it's own. Can someone clarify this?
Update
Compile message:
Error:(22, 28) java: incompatible types: C cannot be converted to B
It is simple: C extends Object. No subclass of C could possible extend B. You can't add another base class, because Java doesn't support multiple inheritance.
Whereas a subclass of C can very well implement that additional interface.
But there is simply no way how a C object could also be a B instance.
So:
D extends C implements B // obviously all fine
whereas
D extends B extends C
is impossible. Because B is already defined to not extend anything but Object. Of course, the "trick" here is that both classes B, C are both known, and as said: C isn't extending B.
Take for example
class D extends C implements A{}
C c = new D();
The compiler can immediately tell that c can never refer to an instance of B because if a class extends C it cannot extend B also. As the above example shows, the same cannot be said of interfaces.
That is because it's strictly impossible to create instances of an abstract class in java.
The operator instanceof is called from class Object and cannot be called if there is no instance of a class that revoked it (object).
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 6 years ago.
What does this statement mean:
B b = new C();
Does it mean that b is object of class B and C at the same time? Can anyone clarify this in detail.
I know
B b = new B();
when I create object from class B, but I don't know what this statement mean
B b = new C();
In this statement, C is clearly has an "isA" relationship with B - i.e., B is either C's ancestor or an interface that C implements.
In other words, you have one of
class B { ... }
class C extends B [ ... }
or
interface B { ... }
class C implements B { ... }
where there could also be combinations of these and B and C could be more than one inheritance step apart, for instance
class B { ... }
class X extends B { ... }
class C extends X { ... }
You're creating a C instance and assigning it to a variable of type B, meaning you'll only be able to use methods visible via B (without explicit casting, at least).
B b = new C();
It means that the reference of B type refers to the instance of C type. The C class is a subclass of the B. In this case, you can use methods that are defined in the B class by using overridden versions of these methods in the A class (if such methods exist). That mechanism called polymorphism.
Imagine, you have two subclasses of the B class, for example, A and C. You will write a general implementation in methods of the parent class. Then you will override a behavior of some methods in the child class to make them more specific.
B b1 = new A();
B b2 = new C();
// the same type of references
b1.performAction();
b2.performAction();
// the same methods, but the different code will be executed
// if the methods are overridden in the childs
I am new to JAVA and want to know that why
A a = new B();
is valid
and
B b = new A();
is invalid
Considering that:
class A;
class B extends A;
Because B, by extending A, is also an A. We say this in object-orientation terms by saying that a B is-a A. This means that you can use a B anywhere you use an A.
This relationship is not commutative -- B is-a A does not imply that A is-a B. Therefore you cannot use an A anywhere you would use a B.
Consider this case:
class Animal;
class Dog extends Animal;
This makes sense:
Animal animal = new Dog();
Anywhere it makes sense to use an Animal you can also use a Dog. This is intuitive.
Dog dog = new Animal();
This, on the other hand, does not make sense.
Because when B extends A you consider that every B is a more complex A, using the attributes and methods from A and adding some of his own, but an A cannot be a B, there could be methods in B not specified in A, and as it is not extended from B, cannot be instantiated.
Every B is an A , but no every A is a B ( unless you specify that )
class A {
int i = 10;
}
class B {
int i = 10;
int j = 20;
}
class C {
public static void main(String args[]){
B b;
B b=new A();
System.out.println(b.j);
}
}
More specific about the question:
//There're two classes A and B:
Class A {
public static List<B> b = new ArrayList<B>();
}
Class B {
}
In my schema, I want to an object b from Class B. Then under all circumstances,object b will involve at least two objects "a_x" and "a_y" from Class A. How can I create such a relationship?
First thing,This public static List<B> b = new List<B>(); wont compile.
You might need public static List<B> b = new ArrayList<B>();
You cannot instantiate an Interface.So provide an concreate implementation.Ex:ArrayList
And second thing,You should add them directly where ever your are creating this list.
This is the relationship:
class A
{
public List<B> b = new ArrayList<B>();
}
class B
{
A ax;
A ay;
B(A ax, A ay)
{
this.ax = ax;
this.ay = ay;
}
}
This is what you need?
Why do you need such relantioship?
If you have a relation where each B is related to at least two A instances, then B needs a collection-typed field. For example:
public class B {
private List<A> relatedAList = new ArrayList<>();
...
}
This needs to be an instance field, not a static field.
But if you have a 2+ relationship, it doesn't make sense to call the related objects x and y ... because what if there is a z, and a p and so on. Unless there is a fixed upper bound on the number of related A objects for each B, you have to use some kind of collection to represent each B's related As.