You are on page 1of 30

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Fundamentals

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• About classes and objects,
Encapsulation / Abstraction
• About inheritance and
polymorphism.

2
Classes
A class contains variable declarations and method definitions
Object
• An entity that has state and behavior is known as an object e.g., chair, bike,
marker, pen, table, car, etc. It can be physical or logical (tangible and intangible).
The example of an intangible object is the banking system.
• An object has three characteristics:
a) State: represents the data (value) of an object.
b) Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
c) Identity: An object identity is typically implemented via a unique ID. The value
of the ID is not visible to the external user. However, it is used internally by the
JVM to identify each object uniquely.
Object
How do objects communicate in Java?
Select the one correct answer.
(a) They communicate by modifying each other’s fields.
(b) They communicate by modifying the static variables of each other’s classes.
(c) They communicate by calling each other’s instance methods.
(d) They communicate by calling static methods of each other’s classes.
How to declare a class in java??
Quick test
 Is the below code written correctly?
class A
{
    static
    {
        static
        {
            System.out.println(1);
        }
    }
}
Answer
No. Static Initialization Blocks can not be nested.
Constructors
• While designing a class, the class designer can define within the class, a special
method called ‘constructor’
• Constructor is automatically invoked whenever an object of the class is created
• Rules to define a constructor
A constructor has the same name as the class name
A constructor should not have a return type
A constructor can be defined with any access specifier (like private, public)
A class can contain more than one constructor, So it can be overloaded
Example of constructor
Java Encapsulation
• The meaning of Encapsulation, is to make sure that "sensitive" data is hidden
from users.
• To achieve this, you must: Declare class variables/attributes as private provide
public get and set methods to access and update the value of a private variable
Get and Set
• You learned from the previous chapter that private variables can only be accessed
within the same class (an outside class has no access to it).
• However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the
variable, with the first letter in upper case
Example of Encapsulation
Abstraction
• The process of hiding certain details and showing only essential information to
the user.
• Abstraction can be achieved with either abstract classes or interfaces (which you
will learn more about in the next chapter).
• The abstract keyword is a non-access modifier, used for classes and methods:
• Abstract class: is a restricted class that cannot be used to create objects (to
access it, it must be inherited from another class).
• Abstract method: can only be used in an abstract class, and it does not have a
body. The body is provided by the subclass (inherited from).
Example of abstraction
Inheritance in real World
Example of inheritance
Types Of Inheritance
Use of this and Super
Java this keyword:
• this keyword automatically holds the reference to current instance of a class.
• It can be used to inherit a method from parent class into child class, and want to
invoke method from child class specifically.
• this keyword to access static fields in the class as well, but recommended
approach to access static fields using class reference e.g. MyClass.STATIC_FIELD.
Java super keyword
• It holds the reference to parent class of any given class.
• Using super keyword, we can access the fields and methods of parent class in
any child class.
**Can we have this () and super () together?
• Both this() and super() are constructor calls. Constructor call must always be the
first statement. So we can not have two statements as first statement, hence
either we can call super() or we can call this() from the constructor, but not both.
Predict the output??
class Animal{  
String color="white";  
}  
class Dog extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color);//prints color of Dog class  
System.out.println(super.color);//prints color of Animal class  
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}}  
Output black white
POLYMORPHISM
• Polymorphism is one of the OOPs feature that allows us to perform a single
action in different ways.
• For example, lets say we have a class Animal that has a method sound(). Since
this is a generic class so we can’t give it a implementation like: Roar, Meow, Oink
et.
• Polymorphism is the capability of a method to do different things based on the
object that it is acting upon.
• It allows you define one interface and have multiple implementations.
Difference between Static and runtime polymorphism

• Static polymorphism • Runtime polymorphism



Advantages Method Overloading in Java
• Method overloading increases the readability of the program.
• Different ways to overload the method
• There are two ways to overload the method in java
• By changing number of arguments
• By changing the data type
In java, Method Overloading is not possible by changing the return
type of the method only.

**Can we overload main method???


Access Modifiers in Java
• The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class. We can change the access level of fields, constructors, methods,
and class by applying the access modifier on it.
• Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
• Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
• Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
• Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
Non-access modifiers
• Java provides a number of non-access modifiers to achieve many other
functionalities.
• The static modifier for creating class methods and variables.
• The final modifier for finalizing the implementations of classes, methods, and
variables.
• The abstract modifier for creating abstract classes and methods.
• The synchronized and volatile modifiers, which are used for threads
QUIZ TEST
Q1. Which concept of Java is achieved by combining methods and attribute into a
class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
 Q2. What is it called if an object has its own lifecycle and there is no owner?
a) Aggregation
b) Composition
c) Encapsulation
d) Association
QUIZ TEST
Q3. Which statement is true?
Select the one correct answer.
(a) A super() or this() call must always be provided explicitly as the first statement in
the body of a constructor.
(b) If both a subclass and its superclass do not have any declared constructors,the
implicit default constructor of the subclass will call super() when run.
(c) If neither super() nor this() is declared as the first statement in the body of a
constructor, this() will implicitly be inserted as the first statement.
(d) If super() is the first statement in the body of a constructor, this() can be
declared as the second statement.
(e) Calling super() as the first statement in the body of a constructor of a subclass
will always work, since all superclasses have a default constructor.
QUIZ TEST
Q3. Which statement is true?
Select the one correct answer.
(a) A super() or this() call must always be provided explicitly as the first statement in
the body of a constructor.
(b) If both a subclass and its superclass do not have any declared constructors,the
implicit default constructor of the subclass will call super() when run.
(c) If neither super() nor this() is declared as the first statement in the body of a
constructor, this() will implicitly be inserted as the first statement.
(d) If super() is the first statement in the body of a constructor, this() can be
declared as the second statement.
(e) Calling super() as the first statement in the body of a constructor of a subclass
will always work, since all superclasses have a default constructor.
QUIZ TEST
Q4. Which statements are true?
Select the two correct answers.
(a) A class can only be extended by one class.
(b) Every Java object has a public method named equals.
(c) Every Java object has a public method named length.
(d) A class can extend any number of classes.
(e) A non-final class can be extended by any number of classes.
Summary:

In this session, you were able to :


• Learn about various concepts of OOPS which include
abstraction, inheritance, polymorphism and other.
• Learn about various access and non access modifiers.
References:

Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://www.youtube.com/watch?v=-HafzawNlUo

https://www.youtube.com/watch?v=7GwptabrYyk&t=45s
THANK YOU

You might also like