Suman
Suman

Reputation: 4251

How to create an object of subclass in java?

I am trying to create an object of subclass which is not static in the main, but not able to do. Does anyone tried the same?

package com.example;

import com.example.SubClassExample.MainClass.SubClass;

public class SubClassExample {

    static class MainClass {
        public int mca;
        protected int mcb;
        private int mcc;

        SubClass sc = new SubClass();

        public class SubClass {
            public int sca;
            protected int scb;
            private int scc;

            void method() {
                mca = 10;
                mcb = 20;
                mcc = 30;

                sca = 10;
                scb = 20;
                scc = 30;

                System.out.println("Subclass: " + mca + " " + mcb + " " + mcc + " ");
                System.out.println("Subclass: " + sca + " " + scb + " " + scc + " ");
            }

        }

        void method() {
            mca = 100;
            mcb = 200;
            mcc = 300;

            sc.sca = 100;
            sc.scb = 200;
            sc.scc = 300;

            System.out.println("MainClass: " + mca + " " + mcb + " " + mcc + " ");
            System.out.println("MainClass: " + sc.sca + " " + sc.scb + " " + sc.scc + " ");
        }
    }

    public static void main(String[] args) {

        MainClass obj = new MainClass();
        obj.method();
        obj.sc.method();

        SubClass obj1 = new obj.SubClass(); //Getting ERROR here, tried so many like
                    //MainClass.clas.SubClass, obj.class.SubClass, Subclass() but still not able 
                    //to do. As it is a public subclass, cant we able to create an object of 
                    //that class
        obj1.method();
    }

}

Upvotes: 4

Views: 17369

Answers (4)

why you made main class static and in my code i must make Widow class static to make code run "

public class WoodenProducts {
/*
String treeType;
 int treeAge;

 */
double meterPrice;

 public WoodenProducts(){
     meterPrice = 100;

}


 class Widow extends WoodenProducts{

}


public static void main(String[] args) {

    Widow W = new WoodenProducts.Widow();

    System.out.println(" meter price is " + W.meterPrice);


}

} "

Upvotes: 0

Sage
Sage

Reputation: 15418

SubClass obj1 = new obj.SubClass(); //Getting ERROR here, tried so many like

You can't put a new before the obj:instance of MyClass. It actually doesn't make sense. This is because the object of the inner class is quietly connected to the object of the outer class that it was made from. A declaring obj.new InnerClass() has the same equivalent meaning of obj.innerInstance: referencing the new inner instance by the instance of outer class.

That is why, this line should be: SubClass obj1 = obj.new SubClass();

Upvotes: 1

Masudul
Masudul

Reputation: 21961

As SubClass is not static so you need to create new instance.

MainClass obj = new MainClass();
SubClass obj1 = obj.new SubClass();

Upvotes: 1

Narendra Pathai
Narendra Pathai

Reputation: 41945

Subclass instance = new MainClass().new SubClass();

or in your case

Subclass instance = obj.new SubClass();

You need the instance of the parent class to create instance of inner non-static class.

Upvotes: 5

Related Questions