Results 1 to 3 of 3
  1. #1
    vigalegiadom is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Inheritance using extend class

    Hello forum,

    I have been trying to run the code below using inheritance keyword "extends" from command prompt in windows xp, but I get the error message
    C:\Program Files\Java\jdk1.7.0_03\bin>javac rect.java
    rect.java:65: error: non-static variable this cannot be referenced from a static
    context
    triangle tri=new triangle();

    Can someone debug this using command prompt in windows xp, the complete program is below:

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package inheritprog;
    class rect
    {
    protected double h;
    protected double b;
    public void seth(double h)
    {
    this.h=h;
    }
    public void setb(double b)
    {
    this.b=b;
    }
    public double geth()
    {
    return h;
    }
    public double getb()
    {
    return b;
    }
    public double arearect()
    {
    return h*b;
    }

    class triangle extends rect
    {
    public double areatri()
    {
    return(1./2.*h*b);
    }

    }

    /**
    *
    * @author HP
    */


    /**
    *
    * @author user
    */
    //public class Inheritprog {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    /**
    * @param args the command line arguments
    */

    // TODO code application logic here
    rect rec=new rect();
    rec.seth(10);
    rec.setb(5);
    System.out.println("Area of a rectangle= "+rec.arearect());
    triangle tri=new triangle();
    tri.seth(10);
    tri.setb(8);
    System.out.println("Area of a triangle= "+tri.areatri());
    }


    }





    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Inheritance using extend class

    Moved from Advanced Java.

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jingoism is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: Inheritance using extend class

    You had a few problems.

    First off when you extend a class you must have the method in your subclass implemented in the superclass.

    Secondly I just moved your triangle class outside the rect class, and everything seems to work fine now.

    Take my advice with a grain of salt though as I am still a newbie to Java... might wanna have an actual expert chime in.


    Java Code:
    class rect {
    	protected double h;
    	protected double b;
    	public void seth(double h)
    	{
    		this.h=h;
    	}
    	public void setb(double b)
    	{
    		this.b=b;
    	}
    	public double geth()
    	{
    		return h;
    	}
    	public double getb()
    	{
    		return b;
    	}
    	public double arearect()
    	{
    		return h*b;
    	}
    	public double areatri()
    	{
    		//This must exist in the superclass
    		return(1./2.*h*b);
    	}
    	
    
    	/**
    	 *
    	 * @author HP
    	 */
    
    
    	/**
    	 *
    	 * @author user
    	 */
    	//public class Inheritprog {
    
    	/**
    	 * @param args the command line arguments
    	 */
    	public static void main(String[] args) {
    		/**
    		 * @param args the command line arguments
    		 */
    
    		// TODO code application logic here
    		rect rec=new rect();
    		rec.seth(10);
    		rec.setb(5);
    		System.out.println("Area of a rectangle= "+rec.arearect());
    		triangle tri=new triangle();
    		tri.seth(10);
    		tri.setb(8);
    		System.out.println("Area of a triangle= "+tri.areatri());
    	}
    	
    }
    
    class triangle extends rect {
    	@Override
    	public double areatri()
    	{
    		System.out.println("Calling from triangle");
    		return(1./2.*h*b);
    	}
    
    }
    Last edited by Jingoism; 11-16-2012 at 08:14 AM.

Similar Threads

  1. Can a class with the main method in it extend?
    By Army in forum New To Java
    Replies: 2
    Last Post: 04-12-2012, 04:15 PM
  2. Replies: 15
    Last Post: 02-04-2012, 11:15 PM
  3. help instantiate a java class that extend applet
    By geosa in forum Java Applets
    Replies: 0
    Last Post: 12-04-2010, 09:14 AM
  4. Trying to extend class
    By ribbs2521 in forum New To Java
    Replies: 4
    Last Post: 10-29-2009, 06:28 PM
  5. Override/Extend Eclipse Class Loading
    By arun.ranganathan in forum Eclipse
    Replies: 0
    Last Post: 07-15-2009, 10:57 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
  •