0% found this document useful (0 votes)
29 views5 pages

Lab 8

Define a final class called ImmutableData. Add private fields data1 and data2. Provide a constructor to initialize these fields. Add public getter methods for these fields. Try to create a subclass of ImmutableData and observe the results. 2. Define a class called BaseClass. Add a final method called displayMessage. Add a non final method called showInfo. Create a subclass called DerivedClass that attempts to override both methods. Observe the results.

Uploaded by

rayanraza155
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)
29 views5 pages

Lab 8

Define a final class called ImmutableData. Add private fields data1 and data2. Provide a constructor to initialize these fields. Add public getter methods for these fields. Try to create a subclass of ImmutableData and observe the results. 2. Define a class called BaseClass. Add a final method called displayMessage. Add a non final method called showInfo. Create a subclass called DerivedClass that attempts to override both methods. Observe the results.

Uploaded by

rayanraza155
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/ 5

Task # 1:

final class ImmutableData{

private int data1;

private int data2;

public ImmutableData(int data1, int data2){

this.data1 = data1;

this.data2 = data2;

public int getDat1(){

return data1;

public int getDat2(){

return data2;

/*

class ExtendedClass extends ImmutableData{

private int data3;

private int data4;

public ExtendedClass(int data1, int data2, int data3, int data4){

super(data1, data2);

this.data3 = data3;
this.data4 = data4;

public int getDat3(){

return data3;

public int getDat4(){

return data4;

*/

public class Main {

public static void main(String[] args) {

ImmutableData data = new ImmutableData(46, 50);

System.out.println("Data 1: "+data.getDat1());

System.out.println("Data 2: "+data.getDat2());

Output without extending:


Output with extending:

Task # 2:
class BaseClass{

public final void displayMessage(){

System.out.println("Hello, World!");

public void showInfo(){

System.out.println("This is some Info.");

class DerivedClass extends BaseClass{

/*

@Override

public void displayMessage(){

System.out.println("Overridden Message");

*/

@Override
public void showInfo(){

System.out.println("This is also some Info.");

public class Main {

public static void main(String[] args) {

BaseClass base = new BaseClass();

DerivedClass derived = new DerivedClass();

base.displayMessage();

base.showInfo();

System.out.println("");

derived.displayMessage();

derived.showInfo();

Output with overriding:


Output without overriding:

You might also like