Chapter 4-Inheritance Practice Programs: 4.1 Example of Single Inheritance
Chapter 4-Inheritance Practice Programs: 4.1 Example of Single Inheritance
void printBioData() {
printData();
System.out.println("Institution : " +
institution);
System.out.println("Roll : " + rollNo);
for (int q = 0; q < qualif.length; q++) {
System.out.println("Marks " + q + ": " +
qualif[q]);
}
for (int m = 0; m < marks.length; m++) {
System.out.print("Result " + m + ": " +
marks[m]);
}
}
}
class SimpleInheritance2 {
public static void main(String args[]) {
Person p = new Person();
Student[] s = new Student[100];
Employee[] e = new Employee[50];
}
}
4.6 This program demonstrates the use of default access specifier. Case 1: All
classes in one file.
class A {
void msg() {
System.out.println("Hi! I am in Class A");
}
}
class B {
public static void main(String args[]) {
A obj = new A();
obj.msg();
}
}
4.7 This program demonstrates the use of default access specifier. Case 3: Save
class A in temp1 directory and class B in temp2 directory.
class A {
void msg() {
System.out.println("Hi! I am in Class A");
}
}
class B {
public static void main(String args[]) {
A obj = new A();
obj.msg();
}
}
class B {
public static void main(String args[]) {
A obj = new A();
obj.msg();
}
}
4.9 This program demonstrates the use of private access specifier. Save class A in
temp1 directory and class B in temp2 directory.
public class A {
int i;
private int x;
void accessA() {
System.out.println("i :" + i + "x = :" + x);
}
}
public class B {
public static void main(String args[]) {
A obj = new A();
obj.i = 555;
obj.x = 999;
}
}
4.10 This program demonstrates how a private member
indirectly can be accessed from an outside class. Save
class A in temp1 directory and class B in temp2
directory.
public class A {
private int data = 40;
public void accessA() {
System.out.println("Hello Java!" + data);
}
}
4.11 A private member in a super class cannot be accessed in its derived class.
This program illustrates the access protection of private members.
class A {
int i;
private int j;
class B extends A {
int k;
void add() {
k = i + j;
}
}
class AccessTest2 {
public static void main(String args[]) {
B b = new B();
b.accessA(55, 99);
b.add();
b.foo();
System.out.println("k is : " + b.k);
}
}
4.12 This program demonstrates the idea of creating a singleton class. Also, it
shows if you make any class constructor private, you cannot create an instance of
that class from outside the class.
class Singleton {
private static Singleton s = null;
public String x;
private Singleton() {
x = "Joy with Java";
}
public static Singleton createS() {
if (s == null) s = new Singleton();
return s;
}
}
public class TestSingleton {
public static void main(String args[]) {
s = new Singleton();
Singleton a = Singleton.createS();
Singleton b = Singleton.createS();
Singleton c = Singleton.createS();
a.x = (a.x).toUpperCase();
System.out.println("From a : " + a.x);
System.out.println("From b : " + b.x);
System.out.println("From c : " + c.x);
c.x = (c.x).toLowerCase();
System.out.println("From a : " + a.x);
System.out.println("From b : " + b.x);
System.out.println("From c : " + c.x);
}
}
4.13 This program demonstrates the accessing of protected members. Keep all
the classes in separate directories.
public class A {
int d = 0;
public int i = 1;
private int j = 22;
protected int k = 333;
public void msg() {
System.out.println("Class A: Default!" + d);
}
public void msg1() {
System.out.println("Class A: Public!" + i);
}
private void msg2() {
System.out.println("Class A: Private!" + j);
}
protected void msg3() {
System.out.println("Class A: Protected!" + k);
}
}
class B {
public static void main(String args[]) {
A a = new A();
a.msg();
a.msg1();
a.msg2();
a.msg3();
}
}
class C extends A {
public static void main(String args[]) {
A a = new A();
a.msg();
a.msg1();
a.msg2();
a.msg3();
}
}
4.14 This program explains the access specification of a protected member in the
same package. Keep all the classes in different files but in the same directory.
class A {
public int pubA = 10;
protected int proA = 20;
}
class B {
void foo() {
System.out.println(†œIn B: †œ + pubA + proA);
}
}
class C extends B {
void C() {
System.out.println("In C : " + proA);
}
}
class ProtectedTest {
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
System.out.println(a.priA);
System.out.println(b.priA);
System.out.println(b.proA);
System.out.println(c.priA);
System.out.println(c.proA);
}
}
4.17 In this program, both super class and derived class have a member color.
We refer color of super class in derived class using super keyword.
class Flower {
String color = "white";
}
class Rose extends Flower {
String color = "red";
void printColor() {
System.out.println(color);
System.out.println(super.color);
}
}
class TestSuper1 {
public static void main(String args[]) {
Rose r = new Rose();
r.printColor();
}
}
4.18 This program explain the use of super keyword to refer the method of a
super class.
class Animal {
void eat() {
System.out.println("I can eat you!");
}
}
class Dog extends Animal {
void eat() {
System.out.println("Eating my bread...");
}
void bark() {
System.out.println("Vowww...Vooke ...");
}
void work() {
super.eat();
eat();
bark();
}
}
class TestSuper2 {
public static void main(String args[]) {
Dog d = new Dog();
d.work();
}
}
4.19 This program demonstrates the call of a super class
constructor using super().
class Animal {
String color = "White";
Animal() {
System.out.println(" Bommm Bomm !");
}
Animal(String skin) {
color = skin;
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("I am " + color + " dog!");
}
}
class TestSuper3 {
public static void main(String args[]) {
Dog d = new Dog();
}
}
4.21 This program illustrates the difference between super() and this()
class Parent {
String expt;
Parent() {
System.out.println("Calling parent class's
constructor.");
}
}
class Child extends Parent {
Child() {
super();
System.out.println("Child class’s first
constructor");
}
Child(String s) {
expt = s;
System.out.println("Child class’s second
constructor");
}
Child(int n) {
this("Java experiment");
System.out.println("Child class’s third
constructor");
}
public static void main(String[] args) {
new Child();
System.out.println("Inside Main");
}
}
Calling parent class's constructor.Child class's first constructorInside Main
4.22 This program illustrates the different ways of declaring variables as final
and restrictions of final variables.
class FinalFields {
static {
GAMMA = 2.3e-10;
}
System.out.println(s);
s.append(" is interesting!");
System.out.println(s);
4.28 This Java program illustrates the fact that dynamic binding is not
applicable to data members.
class A {
int x = 55;
}
class B extends A {
int x = -99;
}
class BindingTest {
public static void main(String args[]) {
A a = new B();
System.out.println(a.x);
}
}
55
4.29 This program illustrates how upcasting can help to maintain an array with
heterogeneous object types.
class Person {}
class HeterogeneousArray {
Obj[0] = x;
Obj[1] = y;
Obj[2] = x;
}
Not Available
Class c = obj.getClass();
System.out.println("Class of Object obj is : " + c.getName());
}
Class of Object obj is java.lang.String
int rollNo;
System.out.println(s.toString());
System.out.println(s.hashCode());
}
Student@15db9742366712642
4.33 A Simple Java program to show working of user defined generic class.
class GenericClass < Type > {
Type obj;GenericClass(Type obj) {
this.obj = obj;
}
public Type getObject() {
return this.obj;
}
}
class GenericClassTest {
public static void main(String[] args) {
GenericClass < Integer > iObj = new GenericClass
< Integer > (15);
System.out.println(iObj.getObject());
GenericClass < String > sObj = new GenericClass
< String > ("Java");
System.out.println(sObj.getObject());
}
}
15Java
System.out.println();
varargsMethod2(9);
varargsMethod2();
}
Number of arguments 19Number of arguments 41 -2 3 -4Number of arguments 0
4.38 This program demonstrates the varargs method taking any type of
arguments as well as any number.
class VarargsMethod3 {
public static void varargsMethod3(Object...obj) {
for (Object o: obj) System.out.print(" " + o);
System.out.println();
}
public static void main(String[] args) {
varargsMethod3(1, "String", 2.3, true);
varargsMethod3();
varargsMethod3(15, 25, 35, 45, 55);
}
}
1 String 2.3 true 15 25 35 45 55
4.40 The following program demonstrates the call by value passing in Java.
class CallByValuePass {
int i = 456;
public static int increment(int x) {
return (x++);
}
public static void main(String[] args) {
int a = 3;
System.out.println("Value of a before call " +
a);
increment(a);
System.out.println("Value of a after call " + a);
}
}
Value of a before call 3Value of a after call 3
4.41 The following program investigate the feasibility of call by reference passing
using wrapper class.
class CallByReferencePass {
int i = 456;
public static int increment(Integer x) {
return (x++);
}
public static void main(String[] args) {
int a = 3;
Integer i = new Integer(a);
System.out.println("Value of a before call " +
i.intValue());
increment(i);
System.out.println("Value of i after call " +
i.intValue());
}
}
Value of a before call 3Value of i after call 3
4.42 The following program demonstrates the call by reference using wrapper
class.
class IntegerWrapper {
int i;
IntegerWrapper(i) {
this.i = i;
}
}
class CallByReferenceTest {
public static IntegerWrapper increment(IntegerWrapper
x) {
x.i = x.i + 1;
return ((x);
}