Try โ€‚โ€‰HackMD

Programming II Lab 3

Introduction to Inheritance

The Islamic University of Gaza
Engineering Faculty
Department of Computer Engineering
Author: Mohammed Nafiz ALMadhoun2021/02/26

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.

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!

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.

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.

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!

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:

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:

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
End Of Lab 3