0% found this document useful (0 votes)
7 views29 pages

Chapter 4-Inheritance Practice Programs: 4.1 Example of Single Inheritance

Chapter 4 covers various aspects of inheritance in Java, including single inheritance, method overriding, and access specifiers. It provides examples of class structures demonstrating these concepts, such as constructors in derived classes and the use of the super keyword. The chapter also discusses the implications of final variables and methods, as well as the creation of singleton classes.

Uploaded by

darsi3048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Chapter 4-Inheritance Practice Programs: 4.1 Example of Single Inheritance

Chapter 4 covers various aspects of inheritance in Java, including single inheritance, method overriding, and access specifiers. It provides examples of class structures demonstrating these concepts, such as constructors in derived classes and the use of the super keyword. The chapter also discusses the implications of final variables and methods, as well as the creation of singleton classes.

Uploaded by

darsi3048
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 4- Inheritance practice programs

4.1 Example of Single inheritance.


class Point2D {
int x;
int y;
void display2D() {
System.out.println("x : " + x + " y :" + y);
}
}
class Point3D extends Point2D {
int z;
void display3D() {
System.out.println("x :" + x + " y : " + y + " z
: " + z);
}
}
public class SingleInheritance {
public static void main(String arge[]) {
Point2D p1 = new Point2D();
Point3D p2 = new Point3D();
p1.x = 10;
p1.y = 20;
System.out.print("2D Point P1 is: ");
p1.display2D();
p2.x = 5;
p2.y = 6;
p2.z = 15;
System.out.print("3D Point P2 is: ");
p2.display3D();
}
}
4.2 Another illustration of single inheritance: deriving more than one child
classes from a parent class.
import java.util.Date;
class DOB {
int dd;
int mm;
int yy;
}
class Person {
String name;
Date dob;
long mobileNo;
void readData(String n, Date d, int m) {
name = n;
dob = d;
mobileNo = m;
}
void printData() {
System.out.println("Name : " + name);
System.out.println("Mobile : " + mobileNo);
}
}
class Student extends Person {
String institution;
int[] qualif = new int[20];
int rollNo;
int[] marks = new int[5];

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 Employee extends Person {


int empNo;
int[] salaryHistory = new int[12];
String organization;
String designation;
Date doj;
void printSalary() {
for (int s = 0; s < salaryHistory.length; s++) {
System.out.println("Salary " + s + ": " +
salaryHistory[s]);
}
}
}

class SimpleInheritance2 {
public static void main(String args[]) {
Person p = new Person();
Student[] s = new Student[100];
Employee[] e = new Employee[50];
}
}

4.3 The following program illustrates the method overriding in Java.


class Point2D {
int x = 0;
int y = 0;
Point2D(int a, int b) {
x = a;
y = b;
}
void display() {
System.out.println("x : " + x + " y : " + y);
}
}
class Point3D extends Point2D {
int z;
Point3D(int c) {
z = c;
}
void display() {
System.out.println("x : " + x + " y : " + y + " z
: " + z);
}
}
class MethodOverridingTest {
public static void main(String args[]) {
Point2D p = new Point2D(3, -4);
p.display();
Point3D q = new Point3D(0);
q.display();
}
}

4.4 The following program illustrates how to define constructors in a derived


class.
class Point2D {
int x;
int y;
Point2D(int a, int b) {
x = a;
y = b;
}
void display() {
System.out.println("x : " + x + "y : " + y);
}
}
class Point3D extends Point2D {
int z;
Point3D(int a, int b, int c) {
x = a;
y = b;
z = c;
}
Point3D(Point2D p, int c) {
x = p.x;
y = p.y;
z = c;
}
Point3D(int c) {
super(0, 0);
z = c;
}
void display() {
System.out.println("x :" + x + "y :" + y + "z : "
+ z);
}
}
class ConstructorOverridingTest {
public static void main(String args[]) {
Point2D p = new Point2D(3.0, -4.0);
p.display();
Point3D q = new Point3D(1, 2 3);
q.display();
Point3D r = new Point3D(p, 0);
r.display();
Point3D s = new Point3D(p, 0);
s.display();
}
}

4.5 Illustrates the order of execution of constructors in inheritance chain.


class A {
A() {
System.out.println("I am from class A ...");
}
}
class B extends A {
{
System.out.println("I am from class B ...");
}
}
class C extends B {
{
System.out.println("I am from class C ...");
}
}
class OrderingOfConstructors {
public static void main(String args[]) {
C c = new C();
}
}

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();
}
}

4.8 This program demonstrates the use of public access


specifier. Save class A in temp1 directory and class B in
temp2 directory.
public 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.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);
}
}

public class AccessTest1 {


public static void main(String args[]) {
A obj = new A();
System.out.println(obj.data);
obj.accessA();
}
}

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;

void accessA(int a, int b) {


i = a;
j = b;
}
private void foo() {};
}

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);
}
}

4.15 This program explains the access specification of a protected member in an


inheritance chain.
class A {
private int priA = 10;
protected int proA = 20;
}
class B extends A {
void B() {
System.out.println("In B : " + 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.16 This program demonstrates, if private and protected members can be


overridden in derived class.
class A {
private void testPrivate() {
System.out.println("I am private in super
class");
}
protected void testProtected() {
System.out.println("I am protected in super
class");
}
}
class B extends A {
private void testPrivate() {
System.out.println("I am private in derived
class");
}
protected void testProtected() {
System.out.println("I am protected in derived
class");
}
}

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.20 Calling an overloaded super class constructor from a derived class


constructor.
class Point2D {
double x, y;
Point2D() {
x = 0.0;
y = 0.0; //Default initialization }
Point2D(double a, double b){ x = a; y = b; } }
class Point3D extends Point2D { double z;
Point3D(){ super(); z = 0.0; }
Point3D(double x, double y, double z){
super(x, y); this.z = z; } } class
TestSuper4{ public static void main(String args[]){
Point3D p = new Point3D(2.0, 3.0, 4.0); }}
OutputPlease run the program....

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 {

final int ALPHA = 5;


final int BETA;

static final double PI = 3.141592653589793;

static final double GAMMA;


public FinalFields() {
BETA = -1;
}

public FinalFields(int constr) {


BETA = constr;
}

static {
GAMMA = 2.3e-10;
}

public void demoMethod() {


System.out.println("G value is " + 4.2345 *
GAMMA);
System.out.println("A-B value is " + (ALPHA -
BETA));
}

public static void main(String args[]) {


FinalFields x = new FinalFields(123);
x.demoMethod();
}
}

4.23 This program illustrates a final method declaration in a class.


class A {
final void methodA() {
System.out.println("This is a final method.");
}
}
class B extends A {
void methodA() {
System.out.println("Illegal!");
}
public static void main(String args[]) {
A a = new A();
B b = new B();
a.methodA();
b.methodA();
}
}
B.java10 error methodA() in B cannot override methodA() in A void methodA()
{

4.24 Program illustrating the use of final class.


final class Car {
int i = 234;
run(iny k) {
final int speed = k;
}
}

class Honda extends Car {


void run() {
System.out.println(“Running safely with 100 kmph
");
}

public static void main(String args[]) {


Car ford = new Car();
Ford.run();
Honda honda = new Honda();
honda.run();
}
}

4.25 Java program to demonstrate reference final variable.


class RefFinalVar {

public static void main(String[] args) {

final String s = new String("Java");

System.out.println(s);
s.append(" is interesting!");

System.out.println(s);

4.26 A java program to demonstrate the use of abstract keyword.


abstract class A {
abstract void m1();
void m2() {
System.out.println("This is a fully defined
method.");
}
}
class B extends A {
void m1() {
System.out.println("B's implementation of m1
...");
}
void m2() {
System.out.println("This is overridden in B.");
}
}
class AbstractDemo {
public static void main(String args[]) {
a = new A();
B b = new B();
b.m1();
b.m2();
}
}
AbstractDemo.java19 error A is abstract; cannot be instantiated A a = new A();
4.27 The following program illustrates the dynamic method dispatch in Java.
class A {
void callMe() {
System.out.println("I am from A");
}
}
class B extends A {
void callMe() {
System.out.println("I am from B");
}
}
class C extends B {
void callme() {
System.out.println("I am from C");
}
}
class WhoAmI {
public void static main(String args[]) {
A a[] = new A[3];
A x = new A();
a[0] = x;
B y = new B();
a[1] = y;
C z = new C();
a[2] = z;
for (A r: a) r.callMe();
}
}
I am from AI am from BI am from C

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 {

public static void main(String args[]) {

Object obj[] = new Object[3];

Object x = new Person();

Object y = new Point();

Object z = new Person();

Obj[0] = x;

Obj[1] = y;

Obj[2] = x;

}
Not Available

4.30 This program demonstrates the working of getClass() method.


public class GetClassTest {

public static void main(String[] args) {

Object obj = new String("Joy with Java");

Class c = obj.getClass();
System.out.println("Class of Object obj is : " + c.getName());

}
Class of Object obj is java.lang.String

4.31 Java program to demonstrate working of hashCode() method.


public class Student {

int rollNo;

public static void main(String args[]) {

Student s = new Student();

System.out.println(s.toString());

System.out.println(s.hashCode());

}
Student@15db9742366712642

4.32 The following program demonstrates the working of finalize() method.


public class FinalizeTest {
public static void main(String[] args) {
FinalizeTest t = new FinalizeTest();
System.out.println(t.hashCode());
t = null;
System.gc();
System.out.println("Space is freed up by Java’s
automatic GC");
}
protected void finalize() {
System.out.println("finalize method is called");
}
}
. 366712642Space is freed up by Java's automatic GCfinalize method is called

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

4.34 This program illustrates multiple type parameters in generic class.


class GC2 < T1, T2 > {
T1 obj1;T2 obj2;GC2(T1 obj1, T2 obj2) {
this.obj1 = obj1;
this.obj2 = obj2;
}
public void print() {
System.out.println(obj1);
System.out.println(obj2);
}
}
class GC2Test {
public static void main(String[] args) {
GC2 < String, Integer > obj = new GC2 < String,
Integer > ("GC", 9);
obj.print();
}
}
GC9

4.35 This program demonstrates a varargs method using an array to pass a


variable number of arguments to a method.
class VarargsMethod1 {
static void varargsMethod1(int v[]) {
System.out.print("Number of args: " + v.length +
" Elements: ");
for (int x: v) System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
int x[] = {
1,
3,
5,
7
};
int y[] = {
2,
4
};
int z[] = {};
varargsMethod1(x);
varargsMethod1(y);
varargsMethod1(z);
}
}
Number of args 4 Elements 1 3 5 7Number of args 2 Elements 2 4Number of args
0 Elements
4.36 This program demonstrates varargs method with ellipses.
class VarargsMethod2 {

static void varargsMethod2(int...v) {

System.out.println("Number of arguments: " + v.length);

for (int i: v) System.out.print(i + " ");

System.out.println();

public static void main(String args[]) {

varargsMethod2(9);

varargsMethod2(1, -2, 3, -4);

varargsMethod2();

}
Number of arguments 19Number of arguments 41 -2 3 -4Number of arguments 0

4.37 This program demonstrates the overloading of a varargs method.


class OverloadingVarargs {
static void varTest(int...v) {
System.out.print("Arg(int ...): " + "Number of
args: " + v.length + " Contents: ");
for (int x: v) System.out.print(x + " ");
System.out.println();
}
static void varTest(boolean...v) {
System.out.print("Arg(boolean ...) " + "Number of
args: " + v.length + " Contents: ");
for (boolean x: v) System.out.print(x + " ");
System.out.println();
}
static void varTest(String msg, int...v) {
System.out.print("Arg(String, int ...): " + msg +
v.length + " Contents: ");
for (int x: v) System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
varTest(1, 2, 3);
varTest("Testing: ", 10, 20);
varTest(true, false, false);
}
}
Arg(int ...) Number of args 3 Contents 1 2 3Arg(String, int ...) Testing 2 Contents
10 20Arg(boolean ...) Number of args 3 Contents true false false

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);
}

public static void main(String[] args) {


int i = 456;

IntegerWrapper x = new IntegerWrapper(i);


System.out.println("Value of i before call" +
x.i);
x = increment(x);
System.out.println("Incremented value of i "
+ x.i);
}
}
Not Available

4.43 This program demonstrates the working of swap function in Java.


class Student {
int rollNo;
Student(int rollNo) {
this.rollNo = rollNo;
}
}
class SwapTest1 {
public static void swap(Student s1, Student s2) {
int temp = s1.rollNo;
s1.rollNo = s2.rollNo;
s2.rollNo = temp;
}
public static void main(String[] args) {
Student s1 = new Student(101);
Student s2 = new Student(202);
swap(s1, s2);
System.out.println("Roll No. of s1 " +
s1.rollNo);
System.out.println("Roll No. of s2 " +
s2.rollNo);
}
}
Roll No. of s1 202Roll No. of s2 101

4.44 This program demonstrates the working of swap method in Java.


class Student {
int rollNo;
float marks;
Student(int rollNo, float marks) {
this.rollNo = rollNo;
this.marks = marks;
}
}
class SwapTest2 {
public static void swap(Student s1, Student s2) {
Student temp = s1;
s1 = s2;
s2 = temp;
}
public static void main(String[] args) {
Student s1 = new Student(101, 99.96);
Student s2 = new Student(202, 71.45);
swap(s1, s2);
System.out.println("Student1 : " + s1.rollNo + "
" + s1.marks);
System.out.println("Student2 : " + s2.rollNo + "
" + s2.marks);
}
}
4.45 This program demonstrates the working of swap method
in Java.
class Student {
int rollNo;
float marks;
Student(int rollNo, float marks) {
this.rollNo = rollNo;
this.marks = marks;
}
}
class WrapperObject {
Student s;
WrapperObject(Student s) {
this.s = s;
}
}
class SwapTest3 {
public static void swap(WrapperObject ws1,
WrapperObject ws2) {
Student temp = ws1.s;
ws1.s = ws2.s;
ws2.s = temp;
}
public static void main(String[] args) {
Student s1 = new Student(101, 99.96 f);
Student s2 = new Student(202, 71.45 f);
WrapperObject ws1 = new WrapperObject(s1);
WrapperObject ws2 = new WrapperObject(s2);
swap(ws1, ws2);
System.out.println("Student1 : " + ws1.s.rollNo +
" " + ws1.s.marks);
System.out.println("Student2 : " + ws2.s.rollNo +
" " + ws2.s.marks);
}
}
Student1 202 71.45Student2 101 99.96

You might also like