Results 1 to 3 of 3
Thread: Inheritance using extend class
- 11-16-2012, 12:54 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
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
- 11-16-2012, 05:16 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-16-2012, 07:37 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
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
-
Can a class with the main method in it extend?
By Army in forum New To JavaReplies: 2Last Post: 04-12-2012, 04:15 PM -
Does an anonymous class have to extend a class or implement an interface ?
By fatabass in forum New To JavaReplies: 15Last Post: 02-04-2012, 11:15 PM -
help instantiate a java class that extend applet
By geosa in forum Java AppletsReplies: 0Last Post: 12-04-2010, 09:14 AM -
Trying to extend class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-29-2009, 06:28 PM -
Override/Extend Eclipse Class Loading
By arun.ranganathan in forum EclipseReplies: 0Last Post: 07-15-2009, 10:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks