1
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 ob1.f(); ob1.fa(); ob2.f(); ob2.fa(); ob2.fb(); ob3.f(); ob3.fa(); //ob3.fb(); NOT ALLOWED } } run: f In class A fa In class A f In class B fa In class A fb In class B f In class B fa In class A (total time: 0 seconds) package exercise; public class a { void fa() { System.out.println("fa In class A"); } void f() { System.out.println("f In class A"); } package exercise; public class b extends a{ void fb() { System.out.println("fb In class B"); } void f() { System.out.println("f In class B"); }

Object Creation in Java

Embed Size (px)

DESCRIPTION

ihkgjkgb

Citation preview

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.pacage exercise!public class "xercise #public static void main$%tring&' args( #

a ob)*new a$(!b ob+*new b$(!a ob,*new b$(! -- a ob.*new b$(!/OT 011O2"3

ob).f$(!ob).fa$(!

ob+.f$(!ob+.fa$(!ob+.fb$(!

ob,.f$(!ob,.fa$(!--ob,.fb$(! /OT 011O2"3

4

4run5f In class 0fa In class 0f In class 6fa In class 0fb In class 6f In class 6fa In class 0 $total time5 7 seconds(pacage exercise!public class a #void fa$(#%ystem.out.println$8fa In class 08(!4

void f$(#%ystem.out.println$8f In class 08(!44pacage exercise!public class b extends a#void fb$(#%ystem.out.println$8fb In class 68(!4

void f$(#%ystem.out.println$8f In class 68(!44