University of Information Technology & Sciences
University of Information Technology & Sciences
University of Information Technology & Sciences
University of Information
Technology & Sciences
Course Code : IT-203
Course Title : s
Object Oriented Programming Language
Submitted To : Tanzir Mehedi Shawon
Lecturer, Department of IT,UITS
Submitted By : Musharrat Tasnim
ID: 1914555003
Department of IT, UITS
Page 2 of 20
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.
Page 3 of 20
You can use the same fields and methods already defined in
the previous class.
The extends keyword indicates that you are making a new class
that derives from an existing class. The meaning of "extends" is
to increase the functionality.
Page 5 of 20
Code:
Page 7 of 20
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Result:
Programmer salary is:40000.0
Bonus of programmer is:10000
Example:
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
File: TestInheritance2.java
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
void weep(){System.out.println("weeping...");}
class TestInheritance2{
d.weep();
d.bark();
d.eat();
}}
File: TestInheritance3.java
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
void meow(){System.out.println("meowing...");}
class TestInheritance3{
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and
B classes. If A and B classes have the same method and you call it from child class
object, there will be ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time
error if you inherit 2 classes. So whether you have same method or different, there
will be compile time error.
class A{
void msg(){System.out.println("Hello");}
class B{
void msg(){System.out.println("Welcome");}
C obj=new C();
Polymorphism in Java
Page 12 of 20
public MountainBike(
int startCadence,
int startSpeed,
int startGear,
String suspensionType){
super(startCadence,
startSpeed,
startGear);
this.setSuspension(suspensionType);
}
this.tireWidth = newTireWidth;
}
bike01.printDescription();
bike02.printDescription();
bike03.printDescription();
}
}
The following is the output from the test program: