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");
		a.display();
	}

}

Recommended Answers

All 5 Replies

what exactly is your question?

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?

Error: Could not find or load main class Phone$Mobile

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.