Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism Objectives of today’s lab: Through this lab, students will explore a hierarchical model for object-oriented design and examine the capabilities of the Java language provides for inheritance and polymorphism. Inheritance is a Java (OO) language feature by which a new type can be declared that extends the capabilities of an existing type. Polymorphism is a language feature that allows the same function call to be associated with different definitions during the same program's execution, by delaying the binding of the call to run-time. [More on this in the next lab] Create a Java project named week9 Create a package named basicoop2 in the project week9
15
Embed
Create a Java project named week9 · Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism Exercise 1: Understanding the basic concept of Inheritance When you inherit
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism
Objectives of today’s lab:
Through this lab, students will explore a hierarchical model for object-oriented design and examine
the capabilities of the Java language provides for inheritance and polymorphism.
Inheritance is a Java (OO) language feature by which a
new type can be declared that extends the capabilities
of an existing type.
Polymorphism is a language feature that allows the
same function call to be associated with different
definitions during the same program's execution, by
delaying the binding of the call to run-time. [More on
this in the next lab]
Create a Java project named week9
Create a package named basicoop2 in the
project week9
Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism
Exercise 1: Understanding the basic concept of
Inheritance
When you inherit from an existing class, you can reuse methods and fields of parent
class, and further you can add new methods and fields also to your derived class.
Task 1.1. Write the following program, save, compile, and run it.
/* file name should be TestInheritance.java in the basicoop2 package*/
class Animal{
public void eat(){
System.out.println("Eating!");
}
}
class Cat extends Animal{
public void meow (){
System.out.println("Meowing!");
}
}
public class TestInheritance{
public static void main(String args[]){
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
If you can successfully run your program, you should see the following output:
Eating!
Meowing!
Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism
Task 1.2. Modify the above program as highlighted below, save, compile,
and run it.
class Animal{
public Animal(){
System.out.println("Animal Constructor called!");
}
public void eat(){
System.out.println("Eating!");
}
}
class Cat extends Animal{
public Cat(){
System.out.println("Cat Constructor called!");
}
public void meow (){
System.out.println("Meowing!");
}
}
public class TestInheritance{
public static void main(String args[]){
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
If you can successfully run your program, you should see the following output:
Animal Constructor called!
Cat Constructor called!
Eating!
Meowing!
Task 1.3: Can you explain what’s happening here and why? (Just write as a comment in the
code).
(Note that, you have not created any object of the Animal class but its constructor has been called)
Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism
Task 1.4. (Use of super) Modify the above program as highlighted below,
save, compile, and run it.
class Animal{
int animalAge;
public Animal(){
System.out.println("Animal Constructor called!");
}
public Animal(int animalAge){
this.animalAge = animalAge;
System.out.println("Animal Constructor called using super!");
}
public int getAnimalAge() {
return animalAge;
}
public void eat(){
System.out.println("Eating!");
}
}
class Cat extends Animal{
public Cat(int catAge){
super(catAge);
}
public Cat(){
System.out.println("Cat Constructor called!");
}
public void meow (){
System.out.println("Meowing!");
}
public int getCatAge() {
return this.animalAge;
}
}
public class TestInheritance{
public static void main(String args[]){
Cat cat = new Cat(2);
cat.eat();
cat.meow();
Week 9(18) OOSD1 Java lab exercises: Java Inheritance and Polymorphism