Arun Kumar A
Java Classes and Interfaces
Date : 02-06-2021
Presentation : Vyom Labs
What is Java and Why Java
• Java is platform independent Object Oriented programming language
• Java code is converted into bytecode meant for JVM
• Write Once Run Any Where
• Java is Secured
• JDK, JRE, JVM are platform dependent for every platforms we need its own flavor
• JDK -> Java Development Kit -> to compile and execute -> Development tools + JRE
• JRE -> Java Runtime Environment -> to execute byte code -> Libraries + JVM
• JVM -> Java Virtual Machine -> Specification of API
2
What is Java
Collection of Libraries
Collection of Packages
Collection of classes and interfaces
Collection of data and methods
Naming Convention is there in java 3
What is class and object
Class
• Core of Object Oriented Programming language it describes the state
and behavior of particular ent entiity
ty
• Once we have class also a data type we can create any number of
objects just like how
how we crea
createte variables
• Class is logical entity and obj
objeect
ct is physica
sicall entity whi ch exi sts
which exists
• Class is an abstract thing ex: animal, flow er
flower
• Object is lion, tiger, cat, etc.
etc. lily, lotus, jasmine, etc. physica lly exists
sically
4
Lion
Objects of type Animal
Animal
Panda
abstract thing
Class
Deer
5
Access Modifiers
• public -> accessible everywhere within the package and its sub package
• protected -> accessible within the package and outside through sub class
or child class mostly used in inheritance
• default -> if we don’t provide access modifier it is considered as default,
accessible only within the package
• private -> only within the class
6
RULES FOR CLASSES
Classes cannot be private only inner class can be made private
Class can have only public or default(no modifiers)
Only one class in a file can be public and that will be name of the file
final classes are not inherited
Abstract class for which we cannot create instance(objects)
By default it extends Object class
Class contain these in order -> modifiers, class keyword, class name
extends another class implements interface
ex: public class Sample extends Test implements inter{} 7
Sample Program
public class Sample{
String name;//instance variable
int id;//instance variable
void display(){//instance method
System.out.println(“Name : “ + name +” ID : “+id);
}
public static void main(String args[]){
Sample s1 = new Sample();
s1.display();
}
}
Class contains variables(instance variable, static variable), constructor, methods, blocks
8
Program Explanation
Object Creation Different ways of object creation
new keyword which dynamically allocates memory
Using new keyword
for object in heap area and returns the reference
• Sample s = new Sample();
new Sample() calls constructor if there are no
Using Class.forName(String s);
constructor compiler provides default constructor
Class c = Class.forName(“com.pack.Sample”);
Constructor are special member methods which has
Sample s = (Sample)c.newInstance();
same name as that of the class name
Using clone method
After object creation we can access the members of
• Sample s = (Sample)s1.clone();
the class and do processing
Deserialization
Each instance(object) maintains its own state and
behavior Sample s = (Sample)s.readObject();
Factory methods
Connection con = DriverManager.getConnection()
9
Interface In Java
Java does not support multiple Inheritance with classes due to
ambiguity this will cause serious issue, second picture Dog class
extends Animal class and implements Pet interface
10
Interface in java
100% abstraction by default all methods are public abstract and all variables are
public static final
Interface we cannot create instance but a reference of it can be used to hold
instance of its implementation class
Ex: MyInter mi = new MyClass();
Interface can extend another interface or multiple interface
Class can implement one or more interface and must provide implementation of
all methods else class has to be declared abstract
Multiple inheritance not possible with classes in java but possible with interface 11
Sample Program interface Test{
int a = 10;
void display();
}
public class Sample implements Test{
public void display(){
System.out.println(“Hai”);
}
public static void main(String args[]){
Test t = new Sample();
t.display();
System.out.println(“a = “+Test.a);
}
}
12
Difference between class and interface
Class Interface
Instance for class for possible Instance for interface not possible
Class can contain concrete methods Interface all methods public abstract, no body
Only public access modifier allowed
All access modifiers allowed within class `
Interface can extend another interface but
Class implements interface and extends Cannot implement
Other class
13
Abstract class
Abstract method : method without body its implementation provided by
subclass Inheriting it
Abstract class :
which contains at least one abstract method
can have concrete methods, constructor and instance variables
we cannot instantiate abstract class
acts as super class
50% abstraction
14
Difference between abstract class and interface
Abstract class Interface
A class declared with abstract keyword An interface declared with interface keyword
Can contain concrete method providing common Contains only public abstract method and
Functionality to all subclasses public static final variables
Can contain constructor and instance variable Can contain static, private and default method
50% abstraction 100% abstraction
15
Types of interface
Marker or tag interface -> Serializable or Cloneable
Functional interface -> Autocloseable and Runnable
Lambda expressions used for functional interface
FuncInter fi = ()->{System.out.println(“lambda expressions”);};
16
Thank
You
17