OBJECT CREATION IN JAVA.
In the following example we have classes a and b. b extends a. In other words a is
the super class and b is the subclass. The following program shows FOUR
possibilities of creating objects of the classes a and b and the accessibility of the
objects.
package exercise;
public class Exercise {
public static void main(String[] args) {
a ob1=new a();
b ob2=new b();
a ob3=new b();
// a ob4=new b(); NOT ALLOWED
run:
ob1.f();
ob1.fa();
f In class A
fa In class A
ob2.f();
ob2.fa();
ob2.fb();
f In class B
fa In class A
fb In class B
ob3.f();
ob3.fa();
//ob3.fb();
f In class B
fa In class A
NOT ALLOWED
(total time: 0 seconds)
}
package exercise;
package exercise;
public class a {
void fa()
{
System.out.println("fa In class
A");
}
public class b extends a{
void fb()
{
System.out.println("fb In class B");
}
void f()
{
System.out.println("f In class
A");
}
}
void f()
{
System.out.println("f In class B");
}
}