Hi everyone.
I am currently doing small exercises on classes.
Suppose you have 2 classes: Person (superclass) and Student (subclass)
the class Student is extending the class Person.
When creating new objects of type Student, when do use the following options:

option 1: student x= new student( ..);
option 2;( person x=new student( );

help me out please.(with explicit examples if possible)
thanks in advance.

Recommended Answers

All 6 Replies

Hi everyone.
I am currently doing small exercises on classes.
Suppose you have 2 classes: Person (superclass) and Student (subclass)
the class Student is extending the class Person.
When creating new objects of type Student, when do use the following options:

option 1: student x= new student( ..);
option 2;( person x=new student( );

help me out please.(with explicit examples if possible)
thanks in advance.

In order to explain this better there needs to be a difference between "Student" and "Person". For example lets assume the classes Person and Student look as follows:

public class Person
{
	private String name;
	private int age;
	
	public Person(String name, int age)
	{
		this.name = name;
		this.age = age;
	}
	
	...
}

public class Student extends Person
{
	double grade;
	public Student(String name, int age, double grade)
	{
		super(name,age);
		this.grade = grade;
	}
	
	public double getGrade()
	{
		return grade;
	}
	...
}

If you use option 1 (student x= new student( ..)) you would have access to the getGrade() method along with all the inherited methods. However if you used option 2 (Generic) (person x=new student( ); ), you will have access to all the method contained within the "Person" class but would not have access to methods defined within the Student class (ie getGrade()).

I hope that helps.

Cheers

Write a program to display the status/category of students based on the cgpa. Input for Student is
name, matrix number and cgpa.
The table shows the category based on the cgpa.
CGPA Category
CGPA >= 2.00 PASS
1.8 <= CGPA < 2.00 PROBITION
CGPA < 1.8 FAIL
Write the program by using inheritance concept if you are given the following class:
Superclass CGPA
Subclass STUDENT

I don't know to use it??? please help me...

Radhi94, Start a new thread for your question.

it is the coding?

radhi: extending a class => public class Subclass extends Superclass
and that's it.
no, it's not your coding, what M4trixSh4d0w tried to tell you, is that you are reviving a dead thread, which you shouldn't. your question has nothing at all to do with the OP's question.

if you have a question of your own: start your own thread. it's free and it annoys us a lot less then watching years old threads being brought back to life.

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.