JAVA method overriding and Inner class concepts - java

class A { //1st code starts here
private void display() {
System.out.println("A class");
}
}
class B extends A {
protected void display() {
System.out.println("B class");
}
}
class Test {
public static void main(String args[]) {
A obj = new B();
obj.display();
}
}
Output : Test.java:22: error: display() has private access in A
obj.display();
class Outer{ //2nd Code starts here
class Inner1{
private void m2() {
System.out.println("Inner1 class");
}
}
class Inner2 extends Inner1{
protected void m2() {
System.out.println("Inner2 class");
}
}
public static void main(String args[]) {
Outer o=new Outer();
Outer.Inner1 i=o.new Inner2();
i.m2();
}
}
Output : Inner1 class
Why compile time error in 1st code while output Inner1 class in 2nd code???

The code of the Outer class can access any member or method declared within the Outer class, regardless of the access level. However, the m2 method being called is the method of the base class Inner1, since you can't override a private method.
On the other hand, the code of the Test class cannot access a private method of a different class, which is why that code doesn't pass compilation.

Because private members are accessible only in class.
when class B extends A private members are inaccessible For B.
In case of Inner classes, An inner class is a member of class and have access to all members of enclosing class.

Related

Call the inner method in outer class through inner class reference

I want to call the Inner class method in outer class, but it isn't working. I also made a reference of Inner class object and try to call the method, but also invalid again
class Outer{
int a;
public void show() {
System.out.println("Show Method");
}
Inner obj=new Inner();
obj.display();
class Inner{
public void display() {
System.out.println("Display Method");
}
}
}
It is very unclear what you are trying to do, however, the syntactically smallest possible change you can make to get your code to compile (any maybe do what you want, although it is not clear to me what it is that you are trying to achieve), would be to move the method call into an instance initializer:
class Outer {
int a;
public void show() {
System.out.println("Show Method");
}
Inner obj = new Inner();
{ obj.display(); }
// ↑ ↑
class Inner {
public void display() {
System.out.println("Display Method");
}
}
}
Now, given a suitable program entry point:
class Test {
public static void main(String... args) {
new Outer();
}
}
This will print:
Display Method

Why does Java create anonymous class internally as static?

abstract class Person {
abstract void eat();
}
class TestAnonymousInner {
public static void main(String args[]){
Person p=new Person() {
void eat(){System.out.println("nice fruits");}
};
p.eat();
}
}
Internal class generated by the compiler
static class TestAnonymousInner$1 extends Person
{
TestAnonymousInner$1(){}
void eat()
{
System.out.println("nice fruits");
}
}
For which reason does the compiler create the anonymous class as static? What would happen if it's non-static?
The code as shown creates an anonymous class in a static context. An inner class (or non-static nested class) needs to have a reference to the enclosing object(*). In this case there is no enclosing object because it is created in a static method, so using a static nested class is the only valid option.
This can easily be demonstrated by transforming your example to
public class TestInner {
public static void main(String args[]){
Person p = new Testperson();
p.eat();
}
public class Testperson extends Person {
void eat() {
System.out.println("nice fruits");
}
}
}
Compiling this will yield the error
non-static variable this cannot be referenced from a static context
While it will compile just fine if you change it to:
public class TestInner {
public static void main(String args[]){
Person p = new Testperson();
p.eat();
}
public static class Testperson extends Person {
void eat() {
System.out.println("nice fruits");
}
}
}
*: The compiler will modify the constructor(s) of an inner class to accept the enclosing object as a parameter, and constructor calls will be rewritten to pass this as the value of that parameter. This is not the case for static nested classes.

Error in a static method in a inner class [duplicate]

This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).

Error in a member class java [duplicate]

This question already has answers here:
Why can't we have static method in a (non-static) inner class (pre-Java 16)?
(15 answers)
Closed 8 years ago.
Im trying to understand how inner class works and while experimenting with some simple code i got a error : The method hello cannot be declared static; static methods can only be declared in a static or top level type
on this code
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
static public void hello() {
System.out.println("show class");
}
}
C2.hello();
}
}
and i cant understand why!
Refer to the documentation here.
Inner Classes: As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
Class2 is an inner class which means that it needs to be tied to an Class1 object. Then objects of Class2 can access the fields of the bound object at all times:
public class Class1 {
private String name = "class1";
public static void main(String[] args) {
Class1 a = new Class1();
Class2 c = a.new Class2();
c.show();
}
class Class2 {
public void show() {
System.out.println("helloworld: "+name); //accessing the name field of a without needing the variable
}
}
}
or you need to make Class2 static so it doesn't need the Class1 instance.
public class Class1 {
public static void main(String[] args) {
Class2 c = new Class2();
c.show();
}
static class Class2 {
public void show() {
System.out.println("helloworld");
}
}
}
Class C2 in your example above is a local Inner class, which means an inner class defined within a method of an outer class, and such classes cannot have static methods inside them because they are associated with objects, (static methods are not dependent upon objects).
Moreover, a local inner class must be instantiated within the method it has been created and not outside the method. This is a rule.
try modifying your code in following way:
public class Class1 {
public static void main(String[] args) {
Class1 c = new Class1();
c.show();
}
public static void show() {
class C2 {
public void hello() {
System.out.println("show class");
}
}
C2 obj= new C2();
obj.hello();
}
}
This should work.
You cant do this since you need to create an instance of Class 'Class1' before you can access Class 'C2'. However the method 'hello' should be possible to access without creating an instance (being a static method).

inner class and how to access them

I have the following code with a nested class called out1
class sample{
public int a=5;
class out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
//access main1() method on class out1
}
}
public class innerclass{
public static void main(String args[]){
sample ob=new sample();
System.out.println(ob.a);// access field a on class sample
//access call() on class sample
}
}
does anyone know on how to access inner class out1 and is it possible to access this inner class without using call() method on class sample?
You can create inner class out1 object as
ob.new out1();
This is my way how to access inner classes. I made a get method in class sample which returns an object of class out1:
public class innerclass {
public static void main(String args[]) {
sample ob = new sample();
ob.getOut1().call(); // calling the call() method in innerclass out1
}
}
class sample {
public int a = 5;
out1 getOut1() {
return new out1();
}
class out1 {
public void main1() {
System.out.println("this is out1");
}
public void call() {
main1();
}
}
}
And try to make classes with uppercase letter and also use camelcase like: Sample, InnerClass, Out1.
You can access the innerclass by new of outer.new of inner.
To call inner class method from outer class you need to create an object of the inner class.Otherwise you have to make the inner class as well as the method static.
class Sample{
public int a=5;
class Out1{
void main1(){
System.out.println("this is out1");
}
}
void call(){
new Out1().main1();
}
}
public class Innerclass{
public static void main(String args[]){
Sample ob=new Sample();
System.out.println(ob.a);// access field a on class sample
Sample.Out1 out1=new Sample().new Out1();
Out1.main1();
ob.call();
//access call() on class sample
}
}
And class names should start with capital letter by convention.
Inner classes can be static.
If you do not define your inner class as static, then you have to create an instance of it (an object) in order to use it.
Here is an example use case of static and member (non static) classes:
public class Tester {
public static void main() {
Outer outerTest = new Outer();
outerTest.test();
outerTest.publicInnerInstance.sayHello();
Outer.InnerStaticClass.sayHello();
}
}
class Outer{
class InnerMemberClass{
public void sayHello(){
System.out.println("Hello");
System.out.println("I'm an instance of 'InnerMemberClass'.");
}
}
static class InnerStaticClass{
public static void sayHello(){
System.out.println("Hello.");
System.out.println("I'm a static class 'InnerStaticClass'.");
}
}
public InnerMemberClass publicInnerInstance;
//'Outer' constructor
public void Outer(){
publicInnerInstance = new InnerMemberClass();
}
public void test(){
InnerStaticClass.sayHello();
InnerMemberClass instance = new InnerMemberClass();
instance.sayHello();
}
}

Categories