3. 12/5/19
What is Class?
A class is an entity that determines how an object will behave and what the
object will contain. In other words, it is a blueprint or a set of instruction to build a
specific type of object {exam_1}.
3
class <class_name>{
private field1;
public field2;
protected field3;
private method1() {
}
public method1() {
}
protected method1() {
}
}
Syntax Access modifiers
4. 12/5/19
What is an Object?
An object is nothing but a self-contained component which consists of methods
and properties to make a particular type of data useful. Object determines the
behavior of the class{exam_2}.
4
student1
student2
Stack
name = Ivan
age=23
name = Nikola
age=34
Heap
5. 12/5/19
Abstraction
Abstraction is the mechanism to hiding the unnecessary things from the users
and provide only functionality {exam_3}.
5
There is a two way to achieve Abstraction:
1. By Abstract Class - Through “abstract” keyword, we can declare a class is abstract. The
abstract class cannot be instantiated.
2. By Interface - In an interface, each method is public and abstract, but it does not contain
any constructor.
@An abstract method is a method that is declared without an implementation!
7. 12/5/19
Inheritance
When properties of one class inherited by other class, this concept is known as
Inheritance. The concept of inheritance provides code reusability feature. When
a class inherits the properties is known as a child class whereas a class whose
properties are inherited is known as Parent class {exam_4}.
7
There are 4 different types of inheritance which is supported by Java:
1.Single Inheritance:
Class A
Class B
11. 12/5/19
Polymorphism
Polymorphism is the concept which has the ability of an object to take on many
forms. Polymorphism a concept by which we can perform a single action by with
different behaviors {exam_5}.
Polymorphism in Java is of two types:
1. Run time polymorphism (static binding or method overloading)
2. Compile time polymorphism (dynamic binding or method overriding)
11
12. 12/5/19
Compile Time Polymorphism (static binding or method
overloading)
As the meaning is implicit, this is used to write the program in such a way, that
flow of control is decided in compile time itself. It is achieved using method
overloading. In method overloading, an object can have two or more methods
with same name, BUT, with their method parameters different. These
parameters may be different on two bases
12
public static double Math.max(double a, double b){..}
public static float Math.max(float a, float b){..}
public static int Math.max(int a, int b){..}
public static long Math.max(long a, long b){..}
13. 12/5/19
Runtime Polymorphism (dynamic binding or method
overriding)
Method overriding is a feature which you get when you implement inheritance in
your program {exam_6}.
13
14. 12/5/19
Runtime Polymorphism (dynamic binding or method
overriding)
14
a0 a1 a2
makeNoise() makeNoise() makeNoise()
makeNoise() makeNoise()
Copy of
Animal
Copy of
Animal
Animal object Cat object Dog object
The reference identity of an object can
be stored in any number of reference
variables.
16. 12/5/19
Questions
1. Make a class Car. Make several(at least 4) subtypes of Car, representing the different brands of
cars - Audi, BMW, Wolkswagen.
2. Make a class Time to hold information about the current time and the current date . Make an easy-
to-use constructor (from client perspective), that would represent the time and date the client
wants to hold in your class.
3. Create a friendly interactive calculator in java Bundle, execute and test your program as a jar file
4. Create a PhoneBook class that stores names and phone numbers and allows the operations of
look-up, insertion and deletion. For each new class that you've created above, create an interface
that defines its behavior, then require your class to implement it.
Bonus points: JavaDoc & good git practice
https://dev.liferay.com/en/participate/advanced-javadoc-guidelines
https://www.git-tower.com/learn/git/ebook/en/command-line/appendix/best-practices
16