0% found this document useful (0 votes)
5 views19 pages

Comp3111_Lab2_Briefing_F2024

Uploaded by

pakninlpn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Comp3111_Lab2_Briefing_F2024

Uploaded by

pakninlpn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lab 2: Java Programming

Basics
Bowen Zhang, [email protected]
Learning Outcomes
1) Be able to write a Java program and compile it with Gradle

2) Be able to describe the concept interface and inheritance in


Java
Java Basics
Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its
attributes and methods.
Attribute: An attribute is a variable within a class that describes the characteristics or state of an object. Attributes
store data related to an object and can be accessed and modified using accessor (getter) and mutator (setter) methods.

Constructor: A constructor is a special type of method used to create and initialize objects of a class. When a new
instance of a class is created, the constructor is automatically called. Its main purpose is to allocate memory for the object
and set initial values.
Method: A method is a function within a class that defines the behavior or functionality of the class. Methods
encapsulate a series of operations and can accept parameters and return values. They define the actions that a class
can perform
Now you need to think about if we create another package like [Lab2b] under
[src.main.java], can we have another [mainApp2b] that can call the [Score] class
under [Lab2a]?
Yes, for sure we can work with cross package classes within a project.
But, how?

(This is an example in ex3)


Exercise 2: Fill in the blank to
complete an OOP Java application
Create an Object
Access Attributes/Methods With an Object

Access method with an object


Access Modifiers
For classes, you can use either public or default.
For attributes, methods and constructors:

within the class

everywhere

within the package /


outside the package through
a child class
The final Keyword

final variable can only be assigned once, after that


they become read-only.
Using java.util.Arrays.toString
Without using java.util.Arrays.toString Using java.util.Arrays.toString
Exercise 3: Learning and practicing
Interface and Inheritance in Java
Inheritance

In Java, it is possible to inherit attributes and methods from one


class to another.

We group the "inheritance concept" into two categories:


•subclass (child) - the class that inherits from another class
•superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
An example of Inheritance

a) We use the keyword extends to inherit a


base class.
b) @Override is an annotation.
This annotation explicitly tells the compiler that
we are overriding the parent’s method (or
member function in C++ terminology).
Interface
Why Use Interfaces?
1) To achieve security - hide certain details and only show the important details of an
object (interface).
2) Java does not support "multiple inheritance“. However, it can be achieved with
interfaces, because the class can implement multiple interfaces.

To access the interface methods,


the interface must be "implemented" by another class with the implements keyword.
Submission

You might also like