Java Notes - Unit 1 Chapter 3
Java Notes - Unit 1 Chapter 3
Course Outcomes :
Course Prerequisites
Basic Programming Concepts
Procedural Programming
Understanding Algorithms and Problem Solving
Memory Management
Data Structures
Basic Command-Line Usage
Understanding of Databases
Syllabus
Unit - 1
• Introduction: Why Java, History of Java, JVM, JRE, Java Environment, Java
Source File Structure, and Compilation. Fundamental,
• Programming Structures in Java: Defining Classes in Java, Constructors, Methods,
Access Specifies, Static Members, Final Members, Comments, Data types,
Variables, Operators, Control Flow, Arrays & String.
• Object Oriented Programming: Class, Object, Inheritance Super Class, Sub Class,
Overriding, Overloading, Encapsulation, Polymorphism, Abstraction, Interfaces,
and Abstract Class.
• Packages: Defining Package, CLASSPATH Setting for Packages, Making JAR
Files for Library Packages, Import and Static Import Naming Convention For
Packages
• Lecture Objectives
To understand Java definition, Java environment, Java program
structure and compilation of java program
Department of Applied Computational Science & Engg.
Course Code : Course Name:
• Lecture Outcomes
Develop the object-oriented programming concept using Java
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Inheritance in Java is a mechanism in which one object acquires all the properties and behaviours of a
parent object. It is an important part of OOPs (Object Oriented programming system).
• The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover,
you can add new methods and fields in your current class also.
• Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
• Why use inheritance in java
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
• Terms used in Inheritance
• Class: A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class,
extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also
called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and
methods of the existing class when you create a new class. You can use the same fields and methods
already defined in the previous class.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• The syntax of Java Inheritance
1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4. }
• The extends keyword indicates that you are
making a new class that derives from an
existing class. The meaning of "extends" is to
increase the functionality.
• In the terminology of Java, a class which is
inherited is called a parent or superclass, and
the new class is called child or subclass.
Inheritance in Java
1. class Employee{
2. float salary=40000;
3. }
4. class Programmer extends Employee{
5. int bonus=10000;
6. public static void main(String args[]){
7. Programmer p=new Programmer();
8. System.out.println("Programmer salary is:"+p.salary);
9. System.out.println("Bonus of Programmer is:"+p.bonus);
10. }
11. }
• OUTPUT
• Programmer salary is:40000.0
• Bonus of programmer is:10000
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Types of inheritance in java
• On the basis of class, there
can be three types of
inheritance in java: single,
multilevel and hierarchical.
• In java programming,
multiple and hybrid
inheritance is supported
through interface only. We
will learn about interfaces
later.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Types of inheritance in java
• On the basis of class, there
can be three types of
inheritance in java: single,
multilevel and hierarchical.
• In java programming,
multiple and hybrid
inheritance is supported
through interface only. We
will learn about interfaces
later.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Single Inheritance Example
• When a class inherits another class, it is known as a single inheritance. In the example given below, Dog
class inherits the Animal class, so there is the single inheritance.
• File: TestInheritance.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}
• Output:
• barking...
• eating...
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Multilevel Inheritance Example
• When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below,
BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
• File: TestInheritance2.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}
• Output:
• weeping...
• barking...
• eating...
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Hierarchical Inheritance Example
• When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog
and Cat classes inherits the Animal class, so there is hierarchical inheritance.
• File: TestInheritance3.java
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10. class TestInheritance3{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. c.meow();
14. c.eat();
15. //c.bark();//C.T.Error
16. }}
• Output:
• meowing...
• eating...
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Inheritance in Java
• Why multiple inheritance is not supported in java?
• To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
• Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B
classes have the same method and you call it from child class object, there will be ambiguity to call the
method of A or B class.
• Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2
classes. So whether you have same method or different, there will be compile time error.
1. class A{
2. void msg(){System.out.println("Hello");}
3. }
4. class B{
5. void msg(){System.out.println("Welcome");}
6. }
7. class C extends A,B{//suppose if it were
8.
9. public static void main(String args[]){
10. C obj=new C();
11. obj.msg();//Now which msg() method would be invoked?
12. }
13. }
Department of Applied Computational Science & Engg.
Course Code : Course Name:
• Output:
• Bike is running safely
Department of Applied Computational Science & Engg.
Course Code : Course Name:
• Output:
• 22
• 24.9
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a
capsule which is mixed of several medicines.
• We can create a fully encapsulated class in Java by making all the data members of the class private. Now
we can use setter and getter methods to set and get the data in it.
• The Java Bean class is the example of a fully encapsulated class.
• Advantage of Encapsulation in Java
• By providing only a setter or getter method, you can make the class read-only or write-only. In other
words, you can skip the getter or setter methods.
• It provides you the control over the data. Suppose you want to set the value of id which should be greater
than 100 only, you can write the logic inside the setter method. You can write the logic not to store the
negative numbers in the setter methods.
• It is a way to achieve data hiding in Java because other class will not be able to access the data through the
private data members.
• The encapsulate class is easy to test. So, it is better for unit testing.
• The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to
create an encapsulated class in Java.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Encapsulation in Java
• Simple Example of Encapsulation in Java
1. //A Java class to test the encapsulated class.
1. //
2. package com.javatpoint;
A Java class which is a fully encapsulated class.
3. class Test{
2. //
It has a private data member and getter and setter 4. public static void main(String[] args){
methods. 5. //creating instance of the encapsulated class
3. package com.javatpoint; 6. Student s=new Student();
4. public class Student{ 7. //setting value in the name member
5. //private data member 8. s.setName("vijay");
6. private String name; 9. //getting value of the name member
7. //getter method for name 10. System.out.println(s.getName());
8. public String getName(){ 11. }
9. return name; 12. }
10. }
11. //setter method for name
12. public void setName(String name){ • Compile By: javac -d . Test.java Run By: java
13. this.name=name com.javatpoint.Test
14. } • Output:
15. } • vijay
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Polymorphism in Java
• Polymorphism in Java
• Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means many forms.
• There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We
can perform polymorphism in java by method overloading and method overriding.
• If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will
focus on runtime polymorphism in java.
• Runtime Polymorphism in Java
• Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden
method is resolved at runtime rather than compile-time.
• In this process, an overridden method is called through the reference variable of a superclass. The
determination of the method to be called is based on the object being referred to by the reference variable.
• Let's first understand the upcasting before Runtime Polymorphism.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Polymorphism in Java
• In this example, we are creating two classes Bike and Splendor. Splendor class extends Bike class and
overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it
refers to the subclass object and subclass method overrides the Parent class method, the subclass method is
invoked at runtime.
• Since method invocation is determined by the JVM not compiler, it is known as runtime polymorphism.
1. class Bike{
2. void run(){System.out.println("running");}
3. }
4. class Splendor extends Bike{
5. void run(){System.out.println("running safely with 60km");}
6.
7. public static void main(String args[]){
8. Bike b = new Splendor();//upcasting
9. b.run();
10. }
11. }
• Output:
• running safely with 60km.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Abstraction in Java
• Abstraction is a process of hiding the implementation details and showing only functionality to the user.
• Another way, it shows only essential things to the user and hides the internal details, for example, sending
SMS where you type the text and send the message. You don't know the internal processing about the
message delivery.
• Abstraction lets you focus on what the object does instead of how it does it.
• Ways to achieve Abstraction
• There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
• Abstract class in Java
• A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract
methods. It needs to be extended and its method implemented. It cannot be instantiated.
• Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the subclass not to change the body of the method.
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Abstraction in Java
• Example of Abstract class that has an abstract method
• In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is
provided by the Honda class.
1. abstract class Bike{
2. abstract void run();
3. }
4. class Honda4 extends Bike{
5. void run(){System.out.println("running safely");}
6. public static void main(String args[]){
7. Bike obj = new Honda4();
8. obj.run();
9. }
10. }
OUTPUT:
running safely
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Interface in Java
• An interface in Java is a blueprint of a class. It has static constants and abstract methods.
• The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
• In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
• Since Java 8, we can have default and static methods in an interface.
• Since Java 9, we can have private methods in an interface.
• Why use Java interface?
• There are mainly three reasons to use interface. They are given below.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
• Syntax:
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.
6. }
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Interface in Java
1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10. class TestInterface2{
11. public static void main(String[] args){
12. Bank b=new SBI();
13. System.out.println("ROI: "+b.rateOfInterest());
14. }}
• Output:
• ROI: 9.15
Department of Applied Computational Science & Engg.
Course Code : Course Name:
Recommended Books
Text books
Reference Book