Results 1 to 6 of 6
  1. #1
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Arrow Main class and subclasses

    I have a mainclass A, with 3 subclasses:B,C,D. THese subclasses have functions that use objects that of the main class. After coding,Im getting the error, cannot find the function. Is there something missing. All this function override thing is confusing me. I have not done any override. Is any override necessary?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Lets use the stereotypical GeometricObject class. You have 2 sub classes Circle and Polygon that both inherit GeometricObject. area() is a method of GeometricObject that will be overridden by each subclass.

    Java Code:
    class GeometricObject{
    	int length, width;
    	public int area(int length, int width){
    		return length*width;
    	}
    }
    class Polygon extends GeometricObject{
    	int length, width;
    	
    	//this doesn't need to override the method because it uses the same parameters and can just call the parents method
    	public int area(int length, int width){
    		super.area(length, width);
    	}
    }
    class Circle extends GeometricObject{
    	int radius;
    	
    	//this overrides the GeometricObject method area() to work with parameters of a circle instead. 
    	public int area(int radius){
    		return Math.PI*radius*radius;
    	}
    }
    It's early so if I didn't make it clear just say so

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I'm not sure how far along you are in Java but eventually you will be re-writing certain methods(toString() being the main one) and that is overriding the toString method of the String class.

  5. #5
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Posts
    58
    Rep Power
    0

    Default

    Hmmm.....
    Didnt understand. What is the function of super? Im just a beginner. I tried using super in my program code. But doesnt seem to be much help. I dont want to print anything yet.

    Let me give you an example of what I want to do with my code.

    Java Code:
    class A
    {
         costructor()
         Function  1()
         Function 2()
    }
    
    class B extends class B
    {
         Function X() // This function uses functions from class A to      
    
                              return objects.
    }
    
    class  C extends class A
    { 
         function Y()// This function uses functions from class A to      
    
                              return objects.
    }
    
    etc///
    Im yet not ready to post my code yet.
    Please suggest.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    super uses the function within the parent class.

    so for Function X() say it's supposed to be using Function 1 then you would just say super.Function 1.

    No need to create a new method to do whats already in the parent(parent and super are the same thing) class, UNLESS, your specifically overriding it.

    Say class A is class Person
    class B is class Student

    class Student extends Person and your trying to use a method of person(say setName())

    Java Code:
    class Person{
    	String newName;
    	public void setName(String name){
    		newName = name;
    	}
    }
    class Student extends Person{
    	String name = "MyName";
    	
    	super.setName(name);
    }
    is that what you mean by using the superclasses method?

Similar Threads

  1. Could not find main class - JDK 1.6
    By Syranno in forum New To Java
    Replies: 1
    Last Post: 07-25-2008, 04:07 PM
  2. executing a method in main class
    By rangers27 in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 05:04 AM
  3. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  4. JVM Could not find main class
    By banduskank in forum Advanced Java
    Replies: 2
    Last Post: 06-24-2008, 08:05 AM
  5. Main startup class
    By j0h@nb in forum New To Java
    Replies: 13
    Last Post: 11-30-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •