Hi, I'm new to java but have some experience with C++ can anyone help me with a basic code to display inheritance? I'v chosen a Parent class Phone and a Child class Mobile and I want to simply use the display method from my parent class on the child class.

here is my code:

public class Phone {

	protected int num; // parent state
	public Phone(int a)
	{num = a;} // constructor
	public void display() {
		System.out.println("The Phone has the number " + num);

public class Mobile extends Phone {
	private String model;
	public Mobile(int a,String s){
		super(a);     //call parent constructor
		model = s;
	public static void main(String[] args)
		Mobile a = new Mobile(454666,"nokia");

6 Years
Discussion Span
Last Post by JamesCherrill

what exactly is your question?

Oh sorry, well the code I currently have will not compile is this code correct. I get an error with the main function I'v tried changing it a number of ways but still will not compile.


normally the error message the compiler gives you should tell you quite a lot about what is going on.
can you paste the message here?


Every public class must be in its own .java file with the same name as the class. You can't have 2 public classes in 1 file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.