Skip to main content

Java Program to implement inheritance || Java Programming Lab Exercise 2.1

 

IMPLEMENTATION OF INHERITANCE


What is Inheritance ?

Ø  The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance.

Ø  The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class.

Aim:

            To write a java program to implement the concept of inheritance.

Procedure:

  1. Open text editor(notepad)
  2. Create a base class called “StudentBase”
    • Declare & define department, collegeName as private data members of string datatype.
    • Since the private data members cannot be  accessed directly, we are using public getter & setter methods  to access the private members of base class
  3. Create a derived class called “StudentDerive” that extends the base class “StudentBase”
    • Declare & define regNo, sName as default data members of string datatype.
  4.  Create main class “InheritEx” that extends “StudentDerive” class.
  5. Create an object (obj) for the class StudentDerive to access the data members & methods of “StudentBase& StudentDerive” classes.
  6. Call & display the member values of StudentBase & StudentDerive classes by using “obj” [Reference variable]
  7. Save the file as similar to the name of the main class “InheritEx.java”
  8. Open command prompt & set the path to compile & run java file.
  9. Compile the program using javac command.
  10. Run the program using java command.
  11. Exit

 

InheritEx.java

class StudentBase {

   private String department = "BCA";

   private String collegeName = "BHARATHI WOMEN'S COLLEGE";

   protected String getDept() {

            return department;

   }

   protected void setDept(String department) {

            this.department = department;

   }

   protected String getCollegeName() {

            return collegeName;

   }

   protected void setCollegeName(String collegeName) {

            this.collegeName = collegeName;

   }

  }

class StudentDerive extends StudentBase {

             String regNo = "402U09001";

             String sName="ANU";

}

public class InheritEx {

      public static void main(String args[]){

           StudentDerive obj=new StudentDerive();

            System.out.println("Implementation of Inheritance in JAVA");

    System.out.println("****************************************");

            System.out.println("Student's Name\t:"+obj.sName);

            System.out.println("Register No\t:"+obj.regNo);

            System.out.println("College Name\t:"+obj.getCollegeName());

            System.out.println("Department\t:"+obj.getDept());           

   }

}

Compile & Run

Open Command prompt

C:\Users\bharathi>cd\

C:\>e:

E:\ cd II BCA Java

E:\II BCA Java>cd Lab prgs

Set path to Java

E:\II BCA Java\Lab Prgs>set path=C:\Program Files\Java\jdk-17\bin

Compile the program using javac command to create Class File

E:\II BCA Java\Lab Prgs>javac InheritEx.java

Run the program using java command

E:\II BCA Java\Lab Prgs>java InheritEx

Implementation of Inheritance in JAVA

****************************************

Student's Name     :ANU

Register No            :402U09U18001

College Name        :BHARATHI WOMEN'S COLLEGE

Department           :BCA CS

Popular posts from this blog

EVOLVING ROLE OF SOFTWARE

EVOLVING ROLE OF SOFTWARE Software is both a product and a vehicle for delivering a product.

OPERATING SYSTEM || UNIT III || Memory Management - Basic Concept of Memory - Address Binding - Logical & Physical Address Space

  OPERATING SYSTEM UNIT III Memory Management ·          Memory Management is the process of coordinating and controlling the memory in a computer, Blocks are assigning portions that are assigned to various running programs in order to optimize the overall performance of the system. Computer Memory ·          Computer memory can be defined as a collection of some data represented in the binary format. ·          On the basis of various functions, memory can be classified into various categories. We will discuss each one of them later in detail. ·          A computer device that is capable to store any information or data temporally or permanently is called storage device. Need for Memory Management in OS Memory management technique is needed for the following reasons: Allocate and de-allocate memor...

Operating System || Unit I || Process Management

  Operating System Unit I Process Management Introduction to Process: A program in execution is called a process. The execution of a process must progress in a sequential fashion. In order to complete its task, process needs the computer resources. A process can be defined as an entity which represents the basic unit of work to be implemented in the system. The operating system has to manage all the processes which require the same resource at the same time , in a convenient and efficient way. The Process Management Involves, Scheduling processes and threads on the CPUs. Creating and deleting both user and system processes. Suspending and resuming processes. Providing mechanisms for process synchronization. Providing mechanisms for process communication. Process Architecture A program becomes a process, when it is loaded into the memory. A process in memory can be divided in 4 sect...