---
description: In this lab, we are going to explore the concepts of inheritance, and how you could create a new class from an existing class, which will allow you to modify a certain class, and to change some behaviour in classes without fully understanding or reimplementing them.
---
<h1 style='border: none'><center>Programming II Lab 3</center></h1>
<h2 style='border: none'><center>Introduction to Inheritance</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/02/26</span></h6>
---
<p style='text-align:justify'>
In this lab, we are going to explore the concepts of inheritance, and how you could create a new class from an existing class, which will allow you to modify a certain class, and to change some behaviour in classes without fully understanding or reimplementing them.
</p>
## Superclasses & Subclasses
You should know that every class in Java extends the `Object` class, but what does this mean?
It means that `Object` is our superclass, which our class (The subclass) will have all its functionality.
And we can expand this concept, so a superclass in a context could be the subclass in another context!
```java=
class Shape {
int x, y;
public Shape (int x, int y) {
this.x = x;
this.y = y;
}
}
class Circle extends Shape {
// Notice that circle will also have x and y.
double r;
public Circle (int x, int y, double r) {
super(x, y);
this.r = r;
}
}
class Rectangle extends Shape {
int w, h;
public Rectangle (int x, int y, int w, int h) {
super (x, y);
this.w = w;
this.h = h;
}
}
class Square extends Rectangle {
public Square (int x, int y, int d) {
super (x, y, d, d);
}
}
```
Notice that `Rectangle` is the subclass for `Shape`, but it's the superclass for `Square`.
## Overriding methods
One of the most important things about inheritance is the ability to override methods, this will allow us to change the behaviour of certain methods in the superclass if we instantiated the subclass.
```java=
class Shape {
int x, y;
public Shape (int x, int y) {
this.x = x;
this.y = y;
}
public int getArea () {
return 0;
}
}
class Rectangle extends Shape {
int w, h;
public Rectangle (int x, int y, int w, int h) {
super (x, y);
this.w = w;
this.h = h;
}
@Override
public int getArea () {
return w * h;
}
}
```
So in this example, we have the `getArea` method, which will return zero if we created a shape, but if we created a rectangle it will return `w*h`, notice that you won't find this very useful until you understand Polymorphism.
But here is another simple example, the method `toString` is a method that converts an object to a string value, and you can find it in the `Object` class, which by default will return the type and address of the instance, but we could modify it, so whenever we print a certain class it will return a useful data.
```java=
public class Test {
public static void main(String[] args) {
Student s1 = new Student("Mohammed ALMadhoun", 25);
System.out.println(s1); // Try it without overriding toString.
}
}
class Student {
private String name;
private int age;
public Student (String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Name: " + name + " - Age: "+age;
}
}
```
## Introduction to Polymorphism
The main concept of Polymorphism is that you could store an instance of a subclass, in a superclass typed variable, which will allow us to do magical things!
```java=
public class Test{
public static void main(String[] args) {
Shape s1 = new Square(0, 0, 10);
}
}
```
Notice that `s1` contains an instance of `Square`, but its type is `Shape`, but notice that you will be able to call only `Shape` methods.
To check the type of the instance we could use `instanceof` keyboard, for example:
```java=
System.out.println(s1 instanceof Square); // this will print true
System.out.println(s1 instanceof Circle); // this will print false
```
And you can use casting to change if you want to consider s1 as `Square` again, for example:
```java=
if (s1 instanceof Square) {
Square temp = (Square) s1;
}
```
## Lab Tasks
### Task 1: Extend the Post class.
Return to Task 1 in Lab 1, and extend the `Post` class to create a new class called `SecretPost`, this post will have a password, so you couldn't see the content of it without entering the correct password.
Don't forget to allow the user to create secret posts!
###### tags: `Programming` `Java` `IUG`
<center>End Of Lab 3</center>