why I don't access imported package's object but access the instant class object? [duplicate]












0
















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.










share|improve this question















marked as duplicate by Radiodef java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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
















0
















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.










share|improve this question















marked as duplicate by Radiodef java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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














0












0








0









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.










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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



















  • 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












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage