i am confuse about paint() method in applet .The confusion is that paint method is getting object as parameter of Graphics class like this public void paint(Graphics g) and i read that the Graphics class is an abstract class then how we create object of Graphics class and
now you will say that g in Graphics g is reference not actual object and we can create reference of abstract class i know this but g is a reference not object then how can we call a class member with reference object same like this

`g.drawString("hello world",15,30)`

this is out of my senseif that is possible then write a very simple code in which a method get object as parameters of abstract class and call the method of abstract class please help me about this my english is very poor but i hope i completely explain my problem.

The basic answer is, "Don't worry about it. It will be fine."

A more detailed answer is that the system code that calls your code includes an implementation class that is a child class of the given abstract Graphics class. The calling system code creates whatever instance(es) of child class(es) are needed, and uses them to call your method(s) as needed. You don't need to know what actual child implementation class(es) are used. If you're tricky, you can find out, by debugging at run time. But really, you don't need to know. Just trust that the calling code "does the right thing," and that you will somehow receive an instance of some child class that works. That is, when you call the methods defined in the base class, "the right things" will happen on the screen.

The actual parameter that is passed to your paint method will be an instance of Graphics2D - which is a concrete subclass of Graphics.

ps: If you previous posts have been answered then please mark them "solved"

sir Graphics2D is also abstract so we can not create the instance of Graphics2D

Yes, you're right, It's really a subclass of Graphics2D

i think there will be some structure in classes like this

class Object
{

    //class members
}
public abstract class Graphics extends Object
{

    //class members
}
public abstract class Graphics2D extends Graphics
{

    //class members
}
public class (unknown) extends Graphics2D
{

    //class members
}

and last class is unknown

You can print the actual class of the Graphics parameter inside your paint method, eg

System.out.println(g.getClass());

and you will see something like (using a JFrame under Windows) "class sun.java2d.SunGraphics2D", which extends Graphics2D
http://www.docjar.com/html/api/sun/java2d/SunGraphics2D.java.html

what are you talking about
which class object is g in below code

    System.out.println(g.getClass());

sir problem is only about abstract class we can not create object of abstract class this is problem

moaz.amin.37

you are the one who brought up the variable g. just check your initial post:

public void paint(Graphics g)

My Question is simple how we create object of abstract class my question is very simple i know that question is stupid but what can i do i am confuse about it plz understand my problem and solve it

in order to create an object of an abstract class, there are two ways to go:

  1. create a non-abstract class that extends the abstract class and instantiate that.
  2. create an anonymous implementation.

let's say, you have this abstract class:

public abstract class Abby {

    public abstract void doThis();
}

then it is possible to do this:

public static void main(String[] args){
        Abby a = new Abby(){
            public void doThis(){
                System.out.println("done");
            }
        };
        a.doThis();
    }

It's still an implementation of the class, you just don't specifically create a new class that extends it and provides an implementation, you provide it through an 'anonymous' (since you don't give the class an own name) implementation.

You cannot create an onbect of an abstarct class.
The code I showed you earlier prints the actual class of the "g" parameter that was passed to you. In my case that was sun.java2d.SunGraphics2D which is a concrete class that extends Graphics2D (did you look at the source code thatI linked to?)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.