hi,
here is some very simple questions.....
Code:
class Book2 {
protected int pages;
public Book2(int pages)
{
this.pages = pages;
}
public void pageMessage() {
System.out.println("Number of pages: " +
pages);
}
}
in this code i simply want to know what is the use of "this" keyword.
i could simply do it by...
pages = pages; // i removed "this"
i am not saying that code is wrong.....but where should i use "this"... without "this" it can be done ..is not it?
is it just a fancy way of programming?
question
-------------
Code:
class Student { ... }
class Undergraduate extends Student { ... }
class Graduate extends Student { ... }
Student student1, student2;
student1 = new Undergraduate(); // ok
student2 = new Graduate(); // ok
how this is ok?
student1 = new Undergraduate();
i would have satisfied with
Student student1= new Student ();
or
Undergraduate x = new Undergraduate();
but you notice above declaration has mixed classes.....how is it possible??
its calling the subclasses " constructor" and using" new" keyword to get an object of the superclass.....is not it peculiar??