-1

How to create object for abstract class with out creating sub class object and referring its own object

For example :

public class abstract student{
    private int sno;
    private String sname;    
}

public class Test{
    public static void main(String[] rs )
    {
        Student s= new Student();// it is not working
    }
}

Without creating sub class or implementation class I need solution

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • you can't, that's the whole point of the class being abstract. The closest you can get, is create an anonymous class – Stultuske Apr 17 '19 at 05:57

2 Answers2

0

The idea of an abstract class is to explicitly prevent it being instantiated. That's really the point of it being abstract.

However you can subclass it anonymously:

Student student = new Student() { };

This creates a new, nameless, extension of Student.

sprinter
  • 27,148
  • 6
  • 47
  • 78
-1
<code>
package com.glen;

public abstract class student {

    private final int sno;
    private final String sname;

    public int getSno() {
        return sno;
    }

    public String getSname() {
        return sname;
    }


    public student(int sno, String sname) {
        super();
        this.sno = sno;
        this.sname = sname;
    }

    @Override
    public String toString() {
        return "student [sno=" + sno + ", sname=" + sname + "]";
    }
}



-----------------------
package com.glen;

public class Test {
    public static void main(String[] args) {
        student s=new student(10,"raja") {};
        System.out.println(s);

    }
}
</code>