Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:02:16+00:00 2026-05-31T12:02:16+00:00

Here is the problem I want to use a constructor that will simplify image

  • 0

Here is the problem I want to use a constructor that will simplify image loading process. All you neeed to do is specifty a file path.

Would it be possible to do this via inheritance or do i need to create a new buffered image object?
here is the idea for the code to make it easier to understand.

public class specialImage extends BufferedImage { 

    specialImage(String path) {
    super();
    this =  ImageIO.read(new File(Path)); // <--- how can i Do this in java. 
    }
 }

OR SHOULD I DO THE FOLLOWING

 class specialImage{

    BufferedImage manipulator;

    specialImage (String path) {
       manpiulator = ImageIO.read(new File(path));
    }
  }

————————EDIT 1:————————-
I am trying to avoid returning. I want to create a series of functions that will manipulate the specialImage class. like

specialImage objectName = new specialImage(FILE_PATH);
objectName.toGrayscale();
objectName.specialTransformation();
//While still using the BufferedImage class, i.e.,
objectName.getRaster(); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:02:17+00:00Added an answer on May 31, 2026 at 12:02 pm

    There is a general answer to your question, and a specific one. Pragmatically, we should cover the specific one first.

    You should do something along the lines of this.

    public class SpecialImage {
    
      // The thing isn't a manipulator, it's a BufferedImage
      // I would suggest renaming this for ease of maintenance
      BufferedImage manipulator;
    
      public SpecialImage(String path) {
        manpiulator = ImageIO.read(new File(path));
      }
    
    }
    

    Unless you know that your subclass will share the same “behavior” as the parent class, avoid sub-classing. By putting the BufferedImage into your class as a field, you are using a technique called “Composition” and in general, when in doubt, favor composition over inheritance.

    Inheritance works best when the classes follow the Liskov Substitution Principle, or in near-English terms, when they both have the same “behavior” but modified implementations of that “behavior”. For example, BufferedImages and VolatileImages both behave exactly like java.awt.Images, but one does so with an accessible buffer, and the other doesn’t guarantee buffer accessibility (or even buffer existence).

    The more general answer to your question follows.

    Objects are modeled as instances of “things”, where the “things” are modeled with a template called a Class. Construction is the process of creating a new “thing” form it’s template, or in Object Oriented terminology, creating an Object from it’s Class.

    With this understanding, you can see that constructing a “thing” to an existing “thing” is awkward English at best, and a near-nonsense statement at worst. What you probably meant was one of

    • How can I construct a copy of a “thing”?

    If you want a second copy of an Object, you need to provide a “copy constructor”, which is a general term for a constructor that returns a second, equivalent, copy of the first “thing”.

    public class Car {
    
      private int speed;
    
      public Car(int mph) {
        // this is an optional keyword.  I included it for clarity.
        this.speed = mph;
      }
    
      // This is the copy constructor
      public Car(Car other) {
        this.speed = other.speed;
      }
    
      public int getSpeed() {
        return speed;
      }
    
    }
    
    • How can I make a second reference to an existing “thing”?

    You create a new name, and assign it to an old name. The old name will (under the covers) yield a reference which will become the new value of the new name. References are mostly hidden, as all operations on names automatically “dereference”.

     Car myCar = new Car();
     Car yourCar = myCar;  // assuming it is a shared car.
    

    To have two parts of the code reference the same “thing”, you do not need to use a constructor, as it would create two things. Instead you use two different “names” or “references”. References in Java are sort of like pointers in other languages, except that they are restricted in a number of interesting ways.

    1. They do not refer to real memory locations. This allows garbage collection to compact memory without the burden of rewriting an unknown number of references. Memory compaction involves moving the data storage of an object from one starting address to another starting address, and by using a “memory reference id” instead of an actual address, the only physical address that needs changed is the one in the “memory reference id” to “physical address” table (this table cannot be reached from a user-written program).

    2. They cannot be assigned arbitrary locations. The named reference has a declared type, and the Object to be assigned has a declared type. The strong type checking of the compiler and run time environment guarantee that only compatible types can be assigned to a named reference. As such, it is not possible to cast an arbitrary object to be referenced by a non-compatible named reference.

    3. Math operations are forbidden, or more properly, math operations are not implemented. This last constraint prevents offsetting a named reference in such that it might reference a different object with a possibly different non-compatible type.

    With these items in mind, your original plan will work nicely; the real question is how you will achieve those goals. If your “GrayScale” image is the same thing as your non-GrayScale image, then using the same class (with a toGrayScale() method) is a good design choice. If your “GrayScale” image is a different thing than your non-GrayScale image, then having toGrayScale() return a second image is a good design choice.

    Where Object Oriented programming starts to fall apart is when you don’t make these design choices. You see your Object as a “memory pad” and you avoid creating new objects and discarding old objects in preference for maintaining a particular “memory pad”. Unless you are modeling memory pads, this is not object oriented design, and as such, you will be fighting your object oriented environment every step of the way to the solution that will get you your desired output.

    Good luck with you efforts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my Problem, I want to pass the integer 1 when this canvas
So here is my problem. I want to redirect name.domain.com/trips/1 to domain.com?username=name&trip=1 using modrewrite.
So here is my problem. I want to store 2-tuple (key, val) and want
I've got a problem here with an MSI deployment that I'm working on (using
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I'm new here so I hope I will write understandable question. My problem is,
I am having problems here if I want to check if eerste points to
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
I have a problem here. My Zend_Forms do not render in view script. Via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.