instantiating an abstract class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    instantiating an abstract class

    I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class.

    The task to do is gathering data from a textfile where data is in this format..
    P Charles Galea 134

    Where my class needs to check if the first letter is a P it do the stuff in the professors class and if it is an S it do the stuff in the Students class. The professor and student class inherits the SchoolMember abstract class which is follows.


    Code:
    public abstract class SchoolMember {
    	
    	//These are the attributes for a SchoolMember class
    	
    	String typeOfMember;
    	String name;
    	String surname;
    	Integer score;
    	
    	public abstract boolean outstanding(String s);
    	
    }
    and this class
    Code:
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    /* This class is going to be used to parse the file
     * and gets data from this file to check if the student or the 
     * professor is outstanding or not
    */
    public class FileParser {
    
    	public SchoolMember[] convert(String s) {
    	
    	FileReader f = null;
    	BufferedReader buf = null;
    		
    	try {
    		f = new FileReader(s);
    		buf = new BufferedReader(f);
    
    		String line = buf.readLine();
    			
    		while (line != null) {
    				
    			String[] datum = line.split(" ");
    				
    		//	Student stud = new Student();
    		//	Professor prof = new Professor();
    				
    		  //  if (SchoolMember.typeOfMember.equals("S") ) {
    				
    		    //	stud.outstanding(s);
    				  
    			}
    			
    		 //   else if (SchoolMember.typeOfMember.equals("P")) {
    		    	
    		   // 	prof.outstanding(s);
    	//	    }
    	
    		}	
    	}
    			catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} finally {
    
    				try {
    					if (buf != null) {
    						buf.close();
    					}
    					buf.close();
    					f.close();
    				} catch (IOException e) {
    					// just ignore
    				}
    			}
    
    			return null;
    
    	}
    }
    Can please some1 help me correct the commemted part..? where i need to check if the first item in the textfile is a 'p' or an 's' and according to what it is do the specified method?

    thank you
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Your thread title does not make any sense. You cannot instantiate an abstract class. The problem discussed has nothing to do with abstract classes. If you want to know how to read files you can read this article
    Last edited by r035198x; Sep 25 '07, 02:49 PM. Reason: code tags bug had struck again

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by sugard
      I need to access some data which is in an abstract class. Though you cannot inistialise an abstract class.

      The task to do is gathering data from a textfile where data is in this format..
      P Charles Galea 134

      Where my class needs to check if the first letter is a P it do the stuff in the professors class and if it is an S it do the stuff in the Students class. The professor and student class inherits the SchoolMember abstract class which is follows.
      [CODE=java]public abstract class SchoolMember {

      //These are the attributes for a SchoolMember class

      String typeOfMember;
      String name;
      String surname;
      Integer score;

      public abstract boolean outstanding(Str ing s);

      }[/CODE]

      and this class
      [CODE=java]
      import java.io.Buffere dReader;
      import java.io.FileNot FoundException;
      import java.io.FileRea der;
      import java.io.IOExcep tion;

      /* This class is going to be used to parse the file
      * and gets data from this file to check if the student or the
      * professor is outstanding or not
      */
      public class FileParser {

      public SchoolMember[] convert(String s) {

      FileReader f = null;
      BufferedReader buf = null;

      try {
      f = new FileReader(s);
      buf = new BufferedReader( f);

      String line = buf.readLine();

      while (line != null) {

      String[] datum = line.split(" ");

      // Student stud = new Student();
      // Professor prof = new Professor();

      // if (SchoolMember.t ypeOfMember.equ als("S") ) {

      // stud.outstandin g(s);

      }

      // else if (SchoolMember.t ypeOfMember.equ als("P")) {

      // prof.outstandin g(s);
      // }

      }
      }
      catch (FileNotFoundEx ception e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTra ce();
      } finally {

      try {
      if (buf != null) {
      buf.close();
      }
      buf.close();
      f.close();
      } catch (IOException e) {
      // just ignore
      }
      }

      return null;

      }
      }[/CODE]
      Originally posted by sugard
      Can please some1 help me correct the commemted part..? where i need to check if the first item in the textfile is a 'p' or an 's' and according to what it is do the specified method?

      thank you
      OK, you have an array of Strings called datum and the first Element of this array should be either a "P" or an "S", correct? So just check, which of both it is.

      Next, the commented part seems to create both a Student and a Professor for each line - don't do that, it's a waste of space. Create one of both, depending on the earlier test.

      Also, you don't seem to read that line more than once. Either add a
      [CODE=java]
      line = buf.readLine();
      [/CODE]at the end of the while-loop or have the while loop doing it itself with
      [CODE=java]
      while ((String line = buf.readLine()) != null)
      // ...
      [/CODE]Does that help?

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        I'd say: a static factory method in the abstract base class.

        kind regards,

        Jos

        Comment

        • sugard
          New Member
          • Aug 2007
          • 50

          #5
          yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
          As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

          I dont know if u can understand what i am saying.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            I'd say: a static factory method in the abstract base class.

            kind regards,

            Jos
            Thanks for the correction.
            Today is national no defenestration day so I get away with that.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by sugard
              yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
              As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

              I dont know if u can understand what i am saying.
              Read my reply #4: the static method can determine which of the two to instantiate.
              Pass that method the line you just have read and let it figure out the details.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by sugard
                yes it helped thanks! but my problem is still how am I going to combine them both the student and the professor so that i will use only 1?
                As i cant use the abstract class. How am i going to check if the first element is a 'P' or an 'S'? which class am i going to instantiate?

                I dont know if u can understand what i am saying.
                You can do
                [CODE=java]SchoolMember member;
                if(element.equa ls("P")) {
                member = new Professor();
                }
                else if (element.equals ("S")) {
                member = new Student();
                }[/CODE]

                Edit: But Jos' approach is much better of course.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by r035198x
                  You can do
                  [CODE=java]SchoolMember member;
                  if(element.equa ls("P")) {
                  member = new Professor();
                  }
                  else if (element.equals ("S")) {
                  member = new Student();
                  }[/CODE]

                  Edit: But Jos' approach is much better of course.
                  Mais naturellement mon ami ;-) btw, the method would be similar but it's the
                  place where that method belongs; the abstract base class is the place where
                  the method should be and figure out what exactly to instantiate; it's the factory
                  method pattern thingy. If more and more different SchoolMembers see the light,
                  that little pattern should be abandoned asap.

                  kind regards,

                  Jos

                  Comment

                  • sugard
                    New Member
                    • Aug 2007
                    • 50

                    #10
                    Thanks very much for your help.
                    I deceided to do the last one since it is easier for me.

                    Code:
                    String[] datum = line.split(" ");
                    				
                    			SchoolMember member;
                    				
                    		    if (typeOMember.equals("S") ) {
                    		    	member = new Student();	  
                    			}
                    			
                    		    else if (typeOfMember.equals("S")) {
                    		    	
                    		    	member = new Professor();
                    		    }
                    I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.eq uals since there is an error saying that the type typeOfMember cannot be solved.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by sugard
                      Thanks very much for your help.
                      I deceided to do the last one since it is easier for me.

                      Code:
                      String[] datum = line.split(" ");
                      				
                      			SchoolMember member;
                      				
                      		    if (typeOMember.equals("S") ) {
                      		    	member = new Student();	  
                      			}
                      			
                      		    else if (typeOfMember.equals("S")) {
                      		    	
                      		    	member = new Professor();
                      		    }
                      I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.eq uals since there is an error saying that the type typeOfMember cannot be solved.
                      A basic rule of Java is that you have to declare variables before you can use them.
                      P.S The other approach is not difficult, you simply move that code to the abstract class in a method called, say, getInstance.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by sugard
                        Thanks very much for your help.
                        I deceided to do the last one since it is easier for me.

                        Code:
                        String[] datum = line.split(" ");
                        				
                        			SchoolMember member;
                        				
                        		    if (typeOMember.equals("S") ) {
                        		    	member = new Student();	  
                        			}
                        			
                        		    else if (typeOfMember.equals("S")) {
                        		    	
                        		    	member = new Professor();
                        		    }
                        I' ve altered the code to that. But I am still encountering a difficulty in this part the typeOfMember.eq uals since there is an error saying that the type typeOfMember cannot be solved.
                        Where does that typeOfMember come from? Did you define a String like that?
                        You're splitting a line; the line parts are stored in the 'datum' String array; that's
                        where you should look for an "S" or "P" not in a made up 'typeOfMember' thing.

                        Please try to understand what those code snippets are all about before you just
                        blindly copy and paste them. btw that piece of code could easily be placed in
                        a static method in that abstract base class in which case you have defined your
                        factory method; that's how it's called.

                        kind regards,

                        typofKindaSorto fJos

                        Comment

                        • sugard
                          New Member
                          • Aug 2007
                          • 50

                          #13
                          typeOfMember is a variable in the SchoolMember class.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by sugard
                            typeOfMember is a variable in the SchoolMember class.
                            So you now see why that code belongs in that abstract class?

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by sugard
                              typeOfMember is a variable in the SchoolMember class.
                              Sure and where did you store that piece of code? In your FileParser?

                              kind regards,

                              Jos

                              Comment

                              Working...