why I don't access imported package's object but access the instant class object? [duplicate]
This question already has an answer here:
Understanding java's protected modifier
6 answers
Why subclass in another package cannot access a protected method?
6 answers
Protected member access from different packages in java - a curiosity
9 answers
package one;
public class B {
protected int n;
}
package two;
import one.B;
public class D extends B {
public void demo(){
n=42;
}
public void demo2(){
D obje=new D();
obje.n=45;
}
public void demo3(){
B obje=new B();
obje.n=49;//This part ERROR!!
}
}
I don't understand why this code has an error in demo3 ,but demo2 is running.
java oop inheritance
marked as duplicate by Radiodef
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 16:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Understanding java's protected modifier
6 answers
Why subclass in another package cannot access a protected method?
6 answers
Protected member access from different packages in java - a curiosity
9 answers
package one;
public class B {
protected int n;
}
package two;
import one.B;
public class D extends B {
public void demo(){
n=42;
}
public void demo2(){
D obje=new D();
obje.n=45;
}
public void demo3(){
B obje=new B();
obje.n=49;//This part ERROR!!
}
}
I don't understand why this code has an error in demo3 ,but demo2 is running.
java oop inheritance
marked as duplicate by Radiodef
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 16:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41
add a comment |
This question already has an answer here:
Understanding java's protected modifier
6 answers
Why subclass in another package cannot access a protected method?
6 answers
Protected member access from different packages in java - a curiosity
9 answers
package one;
public class B {
protected int n;
}
package two;
import one.B;
public class D extends B {
public void demo(){
n=42;
}
public void demo2(){
D obje=new D();
obje.n=45;
}
public void demo3(){
B obje=new B();
obje.n=49;//This part ERROR!!
}
}
I don't understand why this code has an error in demo3 ,but demo2 is running.
java oop inheritance
This question already has an answer here:
Understanding java's protected modifier
6 answers
Why subclass in another package cannot access a protected method?
6 answers
Protected member access from different packages in java - a curiosity
9 answers
package one;
public class B {
protected int n;
}
package two;
import one.B;
public class D extends B {
public void demo(){
n=42;
}
public void demo2(){
D obje=new D();
obje.n=45;
}
public void demo3(){
B obje=new B();
obje.n=49;//This part ERROR!!
}
}
I don't understand why this code has an error in demo3 ,but demo2 is running.
This question already has an answer here:
Understanding java's protected modifier
6 answers
Why subclass in another package cannot access a protected method?
6 answers
Protected member access from different packages in java - a curiosity
9 answers
java oop inheritance
java oop inheritance
edited Jan 19 at 16:18
Todd
18.5k74454
18.5k74454
asked Jan 19 at 16:17
LeoLeo
1
1
marked as duplicate by Radiodef
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 16:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Radiodef
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 19 at 16:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41
add a comment |
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
What will be error throw. Show the error.
– Ng Sharma
Jan 19 at 16:33
Otherwise also, protected variable you can access only same package or outside package only child class access.
– Ng Sharma
Jan 19 at 16:37
stackoverflow.com/questions/215497/…
– Ng Sharma
Jan 19 at 16:40
It is underlined by IntelliJ and "n has protected access in one.B".So can't we access B's n despite import 'one' package?
– Leo
Jan 19 at 16:41