-1

why i am getting these error in these code when trying to create object of square1

package practice;

import java.util.Scanner;

public class rectangle1 {
    float length;
    float breadth;
    float area;

    public void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the length");
        length=sc.nextFloat();
        System.out.println("enter the breadth");
        breadth=sc.nextFloat();
    }

    public void compute() {
        area=length*breadth;
    }

    public void display() {
        System.out.println(area);
    }

    class square1 extends rectangle1 {
        float length;
        float area;

        public void input() {
            Scanner sc = new Scanner(System.in);
            System.out.println("enter the length");
            length=sc.nextFloat();
        }

        public void compute() {
            area=length*length;
        }

    }

    class circle1 extends rectangle1 {
        float pie=3.14f;
        int r;
        float area;

        public void input() {
            Scanner sc = new Scanner(System.in);
            System.out.println("enter the radius");
            r=sc.nextInt();
        }

        public void compute() {
            area=pie*r*r;
        }

    }

    class pol {
        public void controlapp(rectangle1 ref) {
            ref.input();
            ref.compute();
            ref.display();
        }
    }

    public static void main(String[] args) {
        rectangle1 obj=new rectangle1();
        obj.input();
        obj.compute();
        obj.display();

        square1 obj1=new square1();
    }

}

I have tried all that I know but unable to find where I am going wrong

is it some thing I am missing with extends keyword or something?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    See if [this](https://stackoverflow.com/questions/9560600/what-causes-error-no-enclosing-instance-of-type-foo-is-accessible-and-how-do-i) helps – Grinding For Reputation Aug 26 '23 at 12:42
  • 1
    Best to put all classes into their own Java file. Side note: you will want to learn and follow Java naming conventions, including giving classes names that start with an upper-case letter. Following this will help make your code easier for others to understand. – Hovercraft Full Of Eels Aug 26 '23 at 12:55
  • 2
    Otherwise, in the short term, `class square1 extends rectangle1 {` can be changed to `static class square1 extends rectangle1 {`. But still, I recommend that you put all classes into their own java files. – Hovercraft Full Of Eels Aug 26 '23 at 13:06
  • 1
    Also, please don't post "junk" text in your question. The site requires more text so that you can fully explain your code and your problem, not for you to try to get around with junk text. Doing this can increase the risk of your question's being closed and/or down-voted. – Hovercraft Full Of Eels Aug 26 '23 at 13:18

0 Answers0