Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This document explains how to Work with inheritance in java.

You can work on 07Inheritance-2-Start project which is in 01-programming-fundamentals Workset to complete this lab.


Step 1 – Know predefined classes:
1. Take a look at predefined classes and project structure of this project.



2. Three predefined classes Product, FileManager and DatabaseManager. Take a look at the classes and their methods.
3. Demo class also given with empty main method.

Step 2 – Write the code in predefined classes:
1. Expand com.way2learnonline.service package.
2. Open FileManager and DatabaseManager classes.
3. Remove the common method displayAll() from both of the classes and keep it in Manager class.
4. For this create Manager class and keep the below code inside it.
Java
public class Manager{
		public Product[] getAll() {
			return null;
		}
		public void displayAll(){
			Product[] products=getAll();
			for(Product product:products){
				product.displayData();
			}
		}	
	}

Why is getAll() required in Manager class eventhought it is not having common code?

5. Modify FileManager and DatabaseManager to extend Manager
6. In Demo class keep the below code and run it.
Java
public static void main(String[] args) {
		Manager manager=null;
		if(args[0].equals("file")){
			manager =new FileManager("products.txt");
		}else if(args[0].equals("db")){
			manager =new DatabaseManager();
		}
		manager.displayAll();
		
	}

Hint: To run the code successfully, a command line argument either ‘file’ or ‘db’ should be passed.


Step 2 – Making class as abstract class:

1.
Can you call get(int) ans write(product) methods on Manager reference? why?

What can we do so that we can call these methods on Manager reference?

In Manager class keep all methods of FileManager and DatabaseManager with no logic inside of them like as shown below:

Java
public class Manager {
	
		public Product get(int productId) {
			return null;
		}
		public Product[] getAll() {
			return null;
		}
		public void write(Product product) {}
	
		public void displayAll(){
			Product[] products=getAll();
			for(Product product:products){
				product.displayData();
			}
		}
	}


2. Remove body part for dummy methods and make them abstract then make the class also abstract as below:
Java
public abstract class Manager {
	
		public abstract Product get(int productId);
		public abstract Product[] getAll();
		public abstract void write(Product product);
		public void displayAll(){
			Product[] products=getAll();
			for(Product product:products){
				product.displayData();
			}
		}
	}



3. Now keep the below code in Demo class where all methods can be called on Manager reference variable, then run the class:


Java
public static void main(String[] args) {
		Manager manager=null;
		if(args[0].equals("file")){
			manager=new FileManager("products.txt");
		}else if(args[0].equals("db")){
			manager=new DatabaseManager();
		}
		manager.displayAll();
		Product product= manager.get(123);
		manager.write(new Product(111, "Pendrive", 1122));
		Product[] products=abstractManager.getAll();
	}


Hint: To run the code successfully, a command line argument either ‘file’ or ‘db’ should be passed.

Step 3 – Making 100% abstract class as interface:

1. We want to create another class called NetworkManager similar to FileManager and DatabaseManager .

But we dont want display() method in it.

Rename the Manager class to AbstractManager.

Hint : Click on Manager file and press F2.

Create one more class called Manager and keep the abstract methods of AbstractManager class in the Manager class as below:

Java
public abstract class Manager {
	
		public abstract Product[] getAll();
	
		public abstract void write(Product product);
	
		public abstract Product get(int productId);
	}


2. This Manager class is 100% abstract class so make it interface as below:

Java
public interface Manager {
	
		Product[] getAll();
	
		void write(Product product);
	
		Product get(int productId);
	}


3. Make AbstractManager to implement Manager interface.


4. Create NetworkManager class by implementing Manager interface.

Java
public class NetworkManager implements Manager{
	
		@Override
		public Product get(int productId) {
			return null;
		}

		@Override
		public void write(Product product) {}
		@Override
		public Product[] getAll() {
			return null;
		}
	}


What I have tried:

I tired but I can't 😫 I don't know 😫
Posted
Updated 9-Mar-22 22:03pm
v2
Comments
Afzaal Ahmad Zeeshan 9-Mar-22 14:24pm    
What is the problem that you are facing?

1 solution

Well you cannot expect anyone here to do your work for you. If you do not understand the questions then I suggest you go to Java Tutorials Learning Paths[^] and look at the examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900