This question already has answers here:
Java How to call method of grand parents? [duplicate]
(3 answers)
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 3 months ago.
Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in class A invoke the test()method defined in class C?
Answer is "It is not possible to invoke test()".
class C {
public static void test(){
System.out.println("IMESH ISURANGA");
}
}
class B extends C {
}
class A extends B {
//how prove it
}
class Demo {
public static void main(String[] args) {
//----
}
}
How to prove it?
The answer is "It is not possible to invoke test()". Because if it's possible, it would break encapsulation.
We can invoke the test() method in class B using "super." keyword. This happens under the assumption that we are aware that we are violating the encapsulation on our own class. But we are not allowed to access the class above the parent class using "super." keywords because we don't know what rules the superclass is enforcing. So, we can't bypass an implementation.
Related
This question already has answers here:
Java: Interface with new keyword how is that possible?
(9 answers)
Closed 8 months ago.
I understood that java interface cannot be instantiated. However, the below code looks like it instantiated an interface ? I'm trying to understand the code.
interface I{
int a();
int b();
}
public class C {
public static void main(String [] arg){
I i=(new I(){
public int a(){return 1;}
public int b(){return 2;}
});
assert i.a()==1 && i.b()==2;
}
}
An interface is like an all-abstract class, but that unambiguously supports multiple inheritance. You can instantiate any child of it that implements all the abstract methods.
The code you are asking about is creating an anonymous class that provides an implementation of all the abstract methods. Since the class is anonymous, the type of its instance can is most specifically given as I. Given that it's anonymous, that's also the only aspect of it you care about.
This question already has answers here:
What is polymorphism, what is it for, and how is it used?
(29 answers)
Polymorphism vs Overriding vs Overloading
(21 answers)
Try to describe polymorphism as easy as you can [closed]
(24 answers)
Polymorphism - Define In Just Two Sentences [closed]
(31 answers)
Closed 5 years ago.
I have these two classes and I want to know why the output is A, B, and B.xxx. Can someone explain why it is not A.xxx when it is casted?
Here are my two classes and the main method:
public abstract class A {
public A() {
System.out.println("A");
}
public void xxx() {
System.out.println("A.xxx");
}
abstract void yyy();
}
public class B extends A {
public B() {
System.out.println("B");
}
public void xxx() {
System.out.println("B.xxx");
}
public void yyy() {
System.out.println("B.yyy");
}
}
public class ClassRunner {
public static void main(String[] args) {
B b2 = new B();
((A)b2) .xxx();
}
}
So when you have inheritance...a variable initialization of the form
A my_A = new B()
where B extends A...is called disguising. You are disguising an instance of B as an object of A. And the rule of thumb is that the class on the left side of the equals sign (A) defines which methods you are allowed to call on your object. Since class A has the ".xxx()" method, we are allowed to use it. On the other hand, the class on the right hand side of the equals sign defines the actual version of the .xxx() method that we use. Since class B is on the right side, we use B's version of .xxx(). If B does not have a .xxx(), then it defaults to using A's version of it.
This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 7 years ago.
I am using a Inheritance concept here. I have extended class A(Superclass) into class B(Subclass). And I have created an object of a subclass and by using that object I have called add() method. But it is printing a value 5(Super Class).
Why it didn't take subclass's value which is 10 ?
class A{
int a=5;
public void add(){
System.out.println(a);
}
}
class B extends A{
int a=10;
}
public class InheritExample {
public static void main(String args[]){
B b1=new B();
b1.add();
}
}
Help Appreciated.
Thanks.
There's no overriding of instance variables. Overriding only works on instance methods. Therefore, the method add of class A has access only to the a variable declared in A, even if you call that method for an instance of class B.
This question already has answers here:
Why is super.super.method(); not allowed in Java?
(22 answers)
Closed 8 years ago.
I am using inherited codes which cannot be modified. It is being overrided many times. I want to invoke a specific overrided method of a super class (not a direct super class).
public class SuperSuperClass
{
...
public doSomething()
{
//Does something that I want
}
}
public class SuperClass extends SuperSuperClass
{
...
public doSomething()
{
//Does something I do not want
super.doSomething();
}
}
public class SubClass extends SuperClass
{
...
public doSomething()
{
SuperSuperClass.doSomething(); // is this possible?
}
}
The SuperClass.doSomething() does something I do not want before itself calling SuperSuperClass.doSomething(). Is there a way I can invoke SuperSuperClass.doSomething() from SubClass?
The answer to your question is "no". In Java there is no way to express that you want to invoke an instance method that was defined in some specific class that is part of your inheritance hierarchy. The language provides no way to express that.
This question already has answers here:
Why is Multiple Inheritance not allowed in Java or C#?
(17 answers)
Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
(21 answers)
Closed 9 years ago.
In C++, you can extends many classes, so what's the advantages of this design in Java that a class can only extends one class ?
Since interface is a pure kind of class(abstract class actually), why not limit the number of interfaces implementation just like class extension ?
Being able to extend only one base class is one way of solving the diamond problem. This is a problem which occurs when a class extends two base classes which both implement the same method - how do you know which one to call?
A.java:
public class A {
public int getValue() { return 0; }
}
B.java:
public class B {
public int getValue() { return 1; }
}
C.java:
public class C extends A, B {
public int doStuff() {
return super.getValue(); // Which superclass method is called?
}
}
Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.