Sei sulla pagina 1di 6

SHRI VISHNU ENGINEERING COLLEGE

FOR WOMEN

II-B.Tech I-Semester

Assignment-2

Object Oriented Programming (JAVA)

SUBMITTED BY:

Name: A.Varshitha
Class: CSE-A
Regd no.: 18B01A0535

SUBMITTED TO:

Mrs.P.R.Sudha Rani

Dateofsubmission:
Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the


properties and behaviors of a parent object. It is an important part of OOPs
(Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class, you
can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as


a parent-child relationship.

Why use inheritance in java


• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.

Terms used in Inheritance

• Class: A class is a group of objects which have common properties.


It is a template or blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other
class. It is also called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a
subclass inherits the features. It is also called a base class or a
parent class.

The syntax of Java Inheritance

1. Class Subclass-name extends


2. {
3. //methods and fields
4. }
Types of inheritance in java

On the basis of class, there can be three types of inheritance in java:


single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through
interface only.

Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO


technology where one can inherit from a derived class, thereby making
this derived class the base class for the new class. As you can see in
below flow diagram C is subclass or child class of B and B is a child class
of A.

When multiple classes are involved and their parent-child relation is formed in
a chained way then such formation is known as multi-level inheritance.

• In multilevel inheritance, a parent a class has a maximum of one direct


child class only.
• In multi-level inheritance, the inheritance linkage is formed in a linear
way and minimum 3 classes are involved.
• Code re-usability can be extended with multi-level inheritance.
AIM: Write a program in Java to create a Player class. Inherit the
classes Cricket _Player, Football _Player and Hockey_ Player from
Player class.

PROGRAM:
class player
{
String name;
int age;
player(String n,int a){
name=n; age=a;
}
void show(){
System.out.println("\n");
System.out.println("Player name : "+name);
System.out.println("Age : "+age);
}
}
class criket_player extends player{
String type;
criket_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class football_player extends player
{
String type;
football_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class hockey_player extends player
{
String type;
hockey_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class Demo{
public static void main(String args[])
{
criket_player c=new criket_player("Ravi","criket",
football_player f=new football_player("Ram","foot ball",23);
hockey_player h=new hockey_player("Lucky","hockey"
c.show();
f.show();
h.show();
}
}

OUTPUT:
user@svecw000:~$ javac Demo.java
user@svecw000:~$ java Demo
Player name : Ravi
Age : 22
Player type : criket

Player name : Ram


Age : 23
Player type : foot bal

Player name : Lucky


Age : 25
Player type : hockey

Potrebbero piacerti anche