OOP – ASSIGNMENT XIII

Lab Manual Download: Write a program in Java to create a player class. Inherit the classes Cricket_player, Football_player and Hockey_player from player class. The objective of this assignment is to learn the concepts of inheritance in Java

ASSIGNMENT NO 13

 Title: Demonstrate Inheritance in Java

Objectives: To learn and understand concept of Inheritance in Java

Problem Statement:  Write a program in Java to create a player class. Inherit the classes Cricket_player, Football_player and Hockey_player from player class. The objective of this assignment is to learn the concepts of inheritance in Java

Outcomes: 1) Students will be able to learn and understand inheritance in Java.

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

Theory:

In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.

extends Keyword : extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax

class Super {   …..   …..}

class Sub extends Super {   …..   …..}

The super keyword: The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.

Invoking Superclass Constructor: If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.

super(values);

Types of Inheritance: There are various types of inheritance as demonstrated below.
A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class.

A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:

  • The inherited fields can be used directly, just like any other fields.
  • You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
  • You can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can be used directly as they are.
  • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
  • You can declare new methods in the subclass that are not in the superclass.
  • You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Private Members in a Superclass: 

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.

 Conclusion : In this assignment, we have studied and demonstrated complex number operations in Java.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty

PROGRAM

public class PlayerInherit {
 public static void main(String args[])
 {
 new Cricket_Player().display();
 new Cricket_Player("Sachin Tendulkar").display();
 new Football_Player().display();
 new Football_Player("Bhaichang Bhutia").display();
 new Hockey_Player().display();
 new Hockey_Player("Dhyan Chand").display();
 }
}
class Player
{
 String name;
 Player()
 {
 name="Indian";
 System.out.println("Default Constructor of Player Class");
 }
 Player(String name)
 {
 this.name=name;
 System.out.println("Parameterized constructor of Player class");
 }
 void display()
 {
 System.out.println("Name of Player is "+name);
 }
}
class Cricket_Player extends Player
{
 Cricket_Player()
 {
 System.out.println("Default Constructor of Cricket_Player Class");
 }
 Cricket_Player(String str)
 {
 super(str);
 System.out.println("Parameterized Constructor of Cricket_Player Class");
 }
}
class Hockey_Player extends Player
{
 Hockey_Player()
 {
 System.out.println("Default Constructor of Hockey_Player Class");
 }
 Hockey_Player(String str)
 {
 super(str);
 System.out.println("Parameterized Constructor of Hockey_Player Class");
 }
}
class Football_Player extends Player
{
 Football_Player()
 {
 System.out.println("Default Constructor of Football_Player Class");
 }
 Football_Player(String str)
 {
 super(str);
 System.out.println("Parameterized Constructor of Football_Player Class");
 }
}

Output:

Default Constructor of Player Class

Default Constructor of Cricket_Player Class

Name of Player is Indian

Parameterized constructor of Player class

Parameterized Constructor of Cricket_Player Class

Name of Player is Sachin Tendulkar

Default Constructor of Player Class

Default Constructor of Football_Player Class

Name of Player is Indian

Parameterized constructor of Player class

Parameterized Constructor of Football_Player Class

Name of Player is Bhaichang Bhutia

Default Constructor of Player Class

Default Constructor of Hockey_Player Class

Name of Player is Indian

Parameterized constructor of Player class

Parameterized Constructor of Hockey_Player Class

Name of Player is Dhyan Chand

Leave a Reply

Your email address will not be published. Required fields are marked *