Property with same name in Child class java - java

public class HelloWorld
{
protected int num = 12;
public void callme()
{
System.out.print(this.num);
}
public static void main(String[] args)
{
HelloWorld myObject1 = new HelloWorld();
myObject1.callme();
OtherClass myObject2 = new OtherClass();
myObject2.callme();
}
}
public class OtherClass extends HelloWorld
{
protected int num = 14;
}
Why the output is "1212" instead of "1214"? In php its "1214" but not viceversa in java. What's the logic behind that?

callme() method is defined only in the base class, and therefore return this.num; returns the instance variable of the base class.
There is no overriding of instance variables in Java.
If you'd override that method in the sub-class, by adding
public void callme()
{
System.out.print(this.num);
}
to OtherClass, myObject2.callme(); will return 14, since it will execute callme() method of the sub-class, and therefore access the sub-class instance variable.

Related

How do you pass an instance of a class as an argument for a method of that class?

Let's say I have this code. When using an instance of ClassA, how do I pass the instance of the class to the method methodA?
public class ClassA {
public static int methodA(ClassA class) {
return 1;
}
}
//This is wrong but this is what I'm trying to do
public class Main {
public static void main(String [] args) {
ClassA classa = new ClassA();
classa.methodA(classa);
}
}
The way to achieve this is correct. Just use another name, because class is a reserved keyword for class definition
public static int methodA(ClassA instance) {
return 1;
}
Your approach works but you must not use the keyword class as a variable name. Try
public class ClassA {
public static int methodA(ClassA clazz) {
return 1;
}
}

Inheritance with a variable in java

Can anyone clarify me. Here instance method is overridden but variable is not.
output is: B 10
class A{
int i=10;
public void name(){
System.out.println("A");
}
}
class B extends A{
int i=20;
public void name(){
System.out.println("B");
}
}
public class HelloWorld {
public static void main(String[] args){
A a = new B();
a.name();
System.out.println(a.i);
}
}
You are absolutely correct. Methods are overridden in Java if the parameter list and function names are identical, and the return types are covariant.
i in the base class is simply shadowed: a.i refers to the i member in the base class, since the type of the reference a is an A, even though it refers to a B instance.
You cannot override attribute, you can only override method:
public class A{
private int i=10;
public void name(){
System.out.println("A");
}
public int getI(){
return i;
}
}
public class B extends A{
private int i=20;
public void name(){
System.out.println("B");
}
#Override
public int getI(){
return i;
}
}
public class HelloWorld {
public static void main(String[] args){
A a = new B();
a.name();
System.out.println(a.getI());
}
}
In your example, you define variable a as type A so the i value in B is ignored.
In Java instance variables cannot be overridden, only methods can be overridden. When we declare a field with same name as declared in super class then this new field hides the existing field. See this Java doc Hiding Fields.

Nested Interafce in java

This is my java code:
class A {
interface That {
void show();
}
}
class B implements A.That {
public void show() {
System.out.println("Hi");
}
}
public class MainClass {
public static void main(String args[]) {
A obj = new A();
obj.That object = new B();
object.show();
}
}
Since A is a class (not abstract) we can create its instance and than we can use members of that instance. Now interface is member so obj.That should work but javac says that obj.That is not package. Why?
Interfaces are always static when nested in a class. You should therefore access your interface declaration as A.That, not obj.That.

How is it possible to access the instance variable of aa abstract class to a class?

How can I use this abstract class instance variable in my DemoAbs class?
This is the D class:
abstract class D {
int i=10;
String str="java";
D(){
System.out.println("called abstract class constructor");
}
abstract void m1();
void m2() {
int i=20;
System.out.println("called m2() in abstract class !");
}
}
This is the DemoAbs class:
public class DemoAbs extends D{
// access instance variable here from abstract class
DemoAbs() {
System.out.println("called DemoAbs class constr");
}
#Override
void m1() {
System.out.println("inside m1() method");
}
public static void main(String[] args) {
DemoAbs d=new DemoAbs();
d.m1();
d.m2();
}
}
accessing instance fields of a superclass from a subclass is simple: just use the name of these instance fields as-is. The only caveat: these fields must have the proper visibility. In your case you define the i field to have default visibility which may not always work[1]. To make sure the field is visible to subclasses it should be defined with the protected visibility.
Here's your program (slightly adapted) that shows this:
abstract class D {
protected int i=10;
protected String str="java";
D(){
System.out.println("called abstract class constructor");
}
protected abstract void m1();
void m2() {
System.out.println("called m2() in abstract class! i=" + i);
}
}
public class DemoAbs extends D{
// access instance variable here from abstract class
DemoAbs() {
System.out.println("called DemoAbs class constr");
}
#Override
protected void m1() {
i = 30;
System.out.println("inside m1() method");
}
public static void main(String[] args) {
DemoAbs d=new DemoAbs();
d.m1(); // Output: "inside m1() method"
d.m2(); // Output: "called m2() in abstract class! i=30"
}
}
Note that the call to d.m2() will produce output which says "i=30". That's because the call to d.m1() carried out the assignment i = 30 (see body of of DemoAbs.m1()) thus changing i from its initial value (10 see its declaration in D).
[1] Specifically, if the subclass is declared in a different package than the superclass, a field with default visibility will not be visible to it.
You can use super keyword to access superclass non private variables from methods of subclass
Your code(modified):
abstract class D {
int i = 10;
String str = "java";
private String newStr = "java not accessible";
D() {
System.out.println("called abstract class construtar");
}
abstract void m1();
void m2() {
int i = 20;
System.out.println("called m2() Abstrate class !");
}
}
public class DemoAbs extends D {
// access instance variable here from abstract class
DemoAbs() {
System.out.println("called DemoAbs class contr");
}
#Override
void m1() {
System.out.println(super.i); // added
System.out.println(super.str); // added
//System.out.println(super.newStr); -- Not accessible
System.out.println("inside m1() method");
}
public static void main(String[] args) {
DemoAbs d = new DemoAbs();
d.m1();
d.m2();
}
}
Simply In your main method just do this,
public static void main(String[] args) {
DemoAbs d=new DemoAbs();
d.m1();
d.m2();
int num = d.i;
String strVar = d.str;
}
Now variable num and strVar are holding the values of the member variable.
You can define getter method for instance variables in abstract class D and access it using DemoAbs class instance.

Can a method in an inner class access a parent class method?

I'm not sure if my question title describes my situation aptly, so my apologies if it doesn't! Anyway, let's say I have the following code snippet (visibility is as stated):
public class ChildClass extends ParentClass {
// more code
private void myMethod() {
MyClass mine = new MyClass() {
public void anotherMethod() {
// insert code to access a method in ParentClass
}
};
}
}
Is it possible for code within anotherMethod() to access a protected method found in ParentClass? If so, how can this be done?
I've tried something like...
(ParentClass.this).parentMethod();
...but obviously it doesn't work due to scope issues.
This compiles fine:
class MyClass {
}
class ParentClass {
protected void parentMethod() {
}
}
class ChildClass extends ParentClass {
private void myMethod() {
MyClass mine = new MyClass() {
public void anotherMethod() {
parentMethod(); // this works
}
};
}
}
A non-static inner class can access all methods of the enclosing class as if it were it's own methods:
public class Test {
public int getOne() {
return 1;
}
public class Inner {
public int getEnclosingOne() {
return getOne(); // this works...
}
}
}
A static inner class can not, as a static inner class is not bound to an instance of the parent class. That can only call static methods on the enclosing class.
As for methods when taking into account inheritance, an method in a non-static inner class can use all the methods of the enclosing (outer) class.
The interesting part is Test2.super.getOne() which indeed obtains getOne() from Test2.super, which is a Test. This is just like Test2 would access the method, namely using super though prefixed with Test2 to indicate you're accessing the namespace of the outer class.
public class Test2 extends Test {
public int getInnerOuterParentOne() {
Inner2 inner2 = new Inner2();
return inner2.getOuterParentOne();
}
public int getInnerOuterOne() {
Inner2 inner2 = new Inner2();
return inner2.getOuterOne();
}
public int getOne() {
return 2;
}
public class Inner2 {
public int getOuterOne() {
return getOne();
}
public int getOuterParentOne() {
return Test2.super.getOne();
}
}
public static void main(String[] args) {
Test2 test2 = new Test2();
System.out.println(test2.getInnerOuterOne()); // 2
System.out.println(test2.getInnerOuterParentOne()); // 1
}
}
There is no way to access "parent class method" in Java, irrelatively to visibility (except for super.parentMethod() in subclass's parentMethod()).
That is, if ChildClass overrides parentMethod(), there is no way to call ParentClass.parentMethod() (bypassing ChildClass.parentMethod()) from other methods of ChildClass.
However, if ChildClass doesn't override parentMethod(), that method is inherited by ChildClass, so that you can access it as a ChildClass's method, i.e. simply as parentMethod().

Categories

Resources