Unit 2 Notes Oops
Unit 2 Notes Oops
UNIT – II - NOTES
INHERITANCE, PACKAGES AND INTERFACES
Overloading Methods – Objects as Parameters – Returning Objects –Static, Nested and
Inner Classes. Inheritance: Basics– Types of Inheritance -Super keyword -Method
Overriding – Dynamic Method Dispatch –Abstract Classes – final with Inheritance.
Packages and Interfaces: Packages – Packages and Member Access –Importing Packages –
Interfaces.
OVERLOADING METHODS
Outline method overloading in java with code fragments. Nov 2023
METHOD OVERLOADING (COMPILE TIME POLYMORPHISM)
Method Overloading is a Compile time polymorphism. Method Overloading is a feature in Java that
allows a class to have more than one methods having same name, but with different signatures
(Each method must have different number of parameters or parameters having different types
and orders). Method overloading is one of the ways that Java supports polymorphism.
Three ways to overload a method:
In order to overload a method, the argument lists of the methods must differ in either of these:
1. Number of parameters. (Different number of parameters in argument list)
For example: add(int, int)
add(int, int, int)
2. Data type of parameters. (Difference in data type of
parameters) For example: add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example: add(int, float)
add(float, int)
Page | 1
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Program
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a: " + a);
}
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
double test(double a)
{
System.out.println("double a: " + a);
return a * a;
}
}
public class Overload
{
public static void main(String[] args)
{
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
C:\Program Files\Java\jdk-19\bin>java Overload
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
Page | 2
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Note:-
Method overloading is not possible by changing the return type of the method because of
ambiguity that may arise while calling the method with same parameter list with different
return type.
Example:
static int sum(int a, int b)
static float sum(int a, int b)
OBJECT AS PARAMETER
Java is strictly pass-by-value. But the scenario may change when the parameter passed is of
primitive type or reference type.
If we pass a primitive type to a method, then it is called pass-by-value or call-by-value.
If we pass an object to a method, then it is called pass-by-reference or call-by-reference.
Object as a parameter is a way to establish communication between two or more objects of
the same class or different class as well.
Pass-by-value (Value as parameter) Pass-by-reference (Object as parameter)
Only values are passes to the function
Reference to the object is passed. So any
parameters. So any modifications done in
modifications done through the object will affect
the formal parameter will not affect the
the actual object.
value of
actual parameter.
Caller and Callee method will have two Caller and Callee methods use the same reference
independent variables with same value. for the object.
Callee method will not have any access to the Callee method will have the direct reference to the
actual parameter. actual object.
Requires more memory. Requires less memory.
Example:
class CallByVal
{
void Increment(int count)
{
count=count+10;
}
}
public class CallByValueDemo
{
public static void main(String arg[])
{
CallByVal ob1=new CallByVal();
int count=100;
System.out.println("Value of Count before method call = "+count);
Page | 3
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
ob1.Increment(count);
Page | 4
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
class CallByRef
{
int count=0;
CallByRef(int c)
{
count=c;
}
static void Increment(CallByRef obj)
{
obj.count=obj.count+10;
}
public static void main(String arg[])
{
CallByRef ob1=new CallByRef(10);
System.out.println("Value of Count (Object 1) before method call = "+ob1.count);
Increment(ob1);
System.out.println("Value of Count (Object 1) after method call = "+ob1.count);
}
}
Output:
Value of Count (Object 1) before method call = 10
Value of Count (Object 1) after method call = 20
RETURNING OBJECTS
In Java, a method can return any type of data. Return type may any primitive data type or class
type (i.e. object). As a method takes objects as parameters, it can also return objects as return
value.
Program
class Add
{
int num1,num2,sum;
static Add calculateSum(Add a1,Add a2)
{
Add a3=new Add();
Page | 5
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
a3.num1=a1.num1+a1.num2;
a3.num2=a2.num1+a2.num2;
a3.sum=a3.num1+a3.num2;
return a3;
}
public static void main(String arg[])
{
Add ob1=new Add();
ob1.num1=10;
ob1.num2=15;
Add ob2=new Add();
ob2.num1=100;
ob2.num2=150;
Add ob3=calculateSum(ob1,ob2);
System.out.println("Object 1 -> Sum = "+ob1.sum);
System.out.println("Object 2 -> Sum = "+ob2.sum);
System.out.println("Object 3 -> Sum = "+ob3.sum);
}
}
Output:
Object 1 -> Sum = 0
Object 2 -> Sum = 0
Object 3 -> Sum = 275
INNER CLASS
Java inner class or nested class is a class that is declared inside the class or interface.
The syntax of defining the inner class is -
Access modifier class OuterClass
{
//code
Access modifier class InnerClass
Page | 6
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
{
//code
}
}
Type Description
Member Inner Class A class created within class and outside method.
A class created for implementing an interface or
Anonymous Inner Class
extending class. The java compiler decides its name.
Local Inner Class A class was created within the method.
Static Nested Class A static class was created within the class.
Nested Interface An interface created within class or interface.
Output:
data is 30
{
void msg()
{
System.out.println(data);
System.out.println(value);
}
}
Local l=new Local();
l.msg();
}
public static void main(String args[])
{
localInner1 obj=new localInner1();
obj.display();
}
}
Output:
30
50
Page | 10
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
3. Private data members of a super class are local only to that class. Therefore, they can’t be accessed
outside the super class, even sub classes can’t access the private members.
4. Protected features in Java are visible to all subclasses as well as all other classes in the same package.
CONCEPT OF SUPER AND SUB CLASSES / BASE CLASS AND DERIVED CLASS
Briefly discuss about subclasses. Apr 2022
We use the terms like parent class, child class, base class, derived class, superclass, and subclass.
The Parent class is the class which provides features to another class. The parent class is also
known as Base class or Superclass.
The Child class is the class which receives features from another class. The child class is also
known as the Derived Class or Subclass
For example: Creating Base Class in java
class <ParentClassName>
{
//Implementation of Parent class
}
Creating Child Class in java
class <ChildClassName> extends <ParentClassName>
{
//Implementation of child class
}
Types of Inheritance
Note
The following inheritance types are not directly supported in Java. (Multiple & Hybrid Inheritance)
Page | 11
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
1. Single inheritance
When a class is extended by only one class, it is called single-level inheritance in java or simply
single inheritance.
In other words, creating a subclass from a single superclass is called single inheritance. In single-
level inheritance, there is only one base class and can be one derived class.
It is represented as shown in the below figure:
Program
Example
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
$java TestInheritance
Page | 12
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
barking...
eating...
2. Multilevel inheritance
The process of creating a new sub class from an already inherited sub class is known as Multilevel
Inheritance.
In multilevel inheritance, there is one base class and one derived class at one level. At the next
level, the derived class becomes the base class for the next derived class and so on. This is as
shown below in the diagram.
Program
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
Page | 13
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
$java TestInheritance2
weeping...
barking...
eating..
3. Hierarchical inheritance
The process of creating more than one sub classes from one super class is called Hierarchical
Inheritance.
Program
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
Page | 14
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Output:
meowing...
eating...
4. Multiple inheritance
Deriving subclasses from more than one superclass is known as multiple inheritance in java.
Java does not support multiple inheritance through class. It supports only single inheritance
through class.
A class cannot extend more than one class but a class can implement more than one interface.
Page | 15
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Page | 16
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
5. Hybrid inheritance
A hybrid inheritance in Java is a combination of single and multiple inheritance.
It can be achieved in the same way as multiple inheritance using interfaces in Java.
By using interfaces, we can achieve multiple as well as a hybrid inheritance in Java.
More Example:
class Box
{
private double width;
private double height;
private double depth;
Box(Box ob)
{
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = -1;
height = -1;
depth = -1;
Page | 17
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
class BoxWeight extends Box
{
double weight;
BoxWeight(BoxWeight ob)
{
super(ob);
weight = ob.weight;
}
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
System.out.println("Weight of mybox1 is " + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
System.out.println("Weight of mybox2 is " + mybox2.weight);
System.out.println();
}
}
C:\Program Files\Java\jdk-19\bin>java DemoBoxWeight
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
KEYWORD SUPER
Super is a special keyword that directs the compiler to invoke the superclass members. It is
used to refer to the parent class of the class in which the keyword is used.
Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.
Page | 19
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
class Test
{
public static void main(String[] args)
{
Student s = new Student();
s.display();
}
}
Output:
Mark: 100
2. Use of super with methods
This is used when we want to call parent class method. Whenever a parent and child class have
same named methods then to resolve ambiguity we use super keyword.
Program
class Person
{
void message()
{
System.out.println("This is person class");
}
}
class Student extends Person
{
void message()
{
System.out.println("This is student class");
}
void display()
{
message();
super.message();
}
}
class Test
{
public static void main(String args[])
{
Student s = new Student();
s.display();
}
}
Page | 20
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Output:
This is student class
This is person class
3. Use of super with constructors
super keyword can also be used to access the parent class constructor.
Program
class Person
{
Person()
{
System.out.println("Person class Constructor");
}
}
class Student extends Person
{
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
class Test
{
public static void main(String[] args)
{
Student s = new Student();
}
}
Output:
Person class Constructor
Student class Constructor
METHOD OVERRIDING
Outline method overloading in java with code fragments. Nov 2023
It is also known as Dynamic Polymorphism. The process of a subclass redefining a method
contained in the superclass (with the same method signature) is called Method Overriding.
When a method in a subclass has the same name and type signature as a method in its superclass,
then the method in the subclass is said to override the method in the superclass.
It is a process in which a function call to the overridden method is resolved at Runtime.
Page | 21
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Program
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{
int k;
B(int a, int b, int c)
{
super(a, b);
k = c;
}
void show()
{
System.out.println("k: " + k);
}
}
public class Override
{
public static void main(String[] args)
{
B subOb = new B(1, 2, 3);
subOb.show();
}
}
C:\Program Files\Java\jdk-19\bin>java Override
k: 3
Rules for Method Overriding:
The method signature must be same for all overridden methods.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
Page | 22
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
void callme()
{
System.out.println("Inside B's callme method");
}
}
class C extends A
{
void callme()
{
System.out.println("Inside C's callme method");
}
}
public class Dispatch
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
A r;
r = a;
r.callme();
r = b;
r.callme();
r = c;
r.callme();
}
}
C:\Program Files\Java\jdk-19\bin>java Dispatch
Inside A's callme method
Inside B's callme method
Inside C's callme method
Page | 24
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
1. Final Variables
Any variable either member variable or local variable (declared inside method or block) modified
by final keyword is called final variable.
Example
public class FinalVariableDemo
{
final int speedlimit = 90; //final variable
void run ()
{
speedlimit = 400; // compile time Error
}
public static void main (String args[])
{
FinalVariableDemo obj = new FinalVariableDemo ();
obj.run ();
}
}
Output
Compile-Time Error
2. Final classes
Java class with final modifier is called final class in Java and they cannot be sub-classed or
inherited.
Example
final class A
Page | 27
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
{
// methods and fields
}
// The following class is illegal
class B extends A
{
// COMPILE-ERROR! Can't subclass A
}
3. Final Methods
Final keyword in java can also be applied to methods.
A java method with final keyword is called final method and it cannot be overridden in sub-class.
If a method is defined with final keyword, it cannot be overridden in the subclass and its
behaviour should remain constant in sub-classes.
Example
class A
{
final void m1()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void m1()
{
// Compile-error! We can not override
System.out.println("Illegal!");
}
}
Output
Compile-Time Error
Page | 28
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
PACKAGES
What are the advantages of packages? Nov 2022 Illustrate how to add classes in a package and
how to access these classes in another package. Nov 2022 what is a package? Explain in detail
about how the packages provide access control to various categories of visibility of class
members. Apr 2023 what is user defined package? How to create and import user defined package
with an example. Apr 2024
A Package can be defined as a collection of classes, interfaces, enumerations and annotations,
providing access protection and name space management. (or)
Package is a mechanism in which variety of classes and interfaces can be grouped together.
The name of the package must be written as the first statement in the Java source program.
Advantages / Purpose
In java, packages are used to achieve the code reusability. That means, the classes from other
programs can be easily used by a class without physically copying it to current location.
Types of Packages:
1. Built-in Packages
Page | 29
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
2. User-defined packages
Defining a Package
To create a package is quite easy: simply include a package keyword as the first statement in a
Java source file.
The syntax for create package
package package_name.[sub_package_name];
public class classname
{
……..
……..
}
Creating user-defined package
Step 1: Create a folder named My_Package.
Step 2: Create one class which contains two methods. We will store this class in a file named A.java. This
file will be stored in a folder My_Package.
The code for this class will be as follows-
Java Program[A.java]
package My_Package;
public class A
{
int a;
public void set val(int n)
{
a=n;
}
public void display()
Page | 30
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
{ .
System,out.println("The value of a is: "+a);
}
}
Import Package
Step 3: Now we will write another java program named PackageDemo.java. This program will use the
methods defined in class A. This source file is also stored in the subdirectory My_Package.
Syntax
import package1[.package2][.package3].classname or *;
There are two ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
The java code for this file is-
Java Program[PackageDemo.java]
import My_Package.A;
class PackageDemo
{
public static void main(String args[])
{ .
A obj=new A();
obj.set_val(10);
obj.display() ;
}
}
Output
10
Step 4:
Now, open the command prompt and issue the following commands in order to run the package
programs
D:\>set CLASSPATH=
D:\
D:\>cd My_Package D:\
My_Package>javac A.java D:\My_Package
>javac PackageDemo.java D:\
My_Package>java PackageDemo
The value of a is: 10
CLASSPATH
The packages are nothing but the directories. For locating the specified package the java run time
system makes use of current working directory as its starting point. Thus if the required packages
is in the current working directory then it will found. Otherwise you can specify the directory path
setting the CLASSPATH statement.
Page | 31
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
For instance- if the package name My_Package is present at prompt D: \ > then we can specify
set CLASSPATH= D:\
D:\>cd My_Package
D:\My_Package\> Now you can execute the required class files from this location
Example:
package mypack;
public class Balance
{
String name;
double bal;
public Balance(String n, double b)
{
name = n;
bal = b;
}
public void show()
{
if (bal < 0)
{
System.out.print("--> ");
}
System.out.println(name + ": $" + bal);
}
}
import mypack.*;
public class AccountBalance
{
public static void main(String[] args)
{
Balance current[] = new Balance[3];
current[0] = new Balance("Dakshitha", 123.33);
current[1] = new Balance("VetriMaaran", 123.33);
current[2] = new Balance("Harshitha", 12.33);
for (int i = 0; i < 3; i++)
current[i].show();
}
}
C:\Program Files\Java\jdk-19\bin>java AccountBalance
Dakshitha: $123.33
VetriMaaran: $123.33
Harshitha: $12.33
Page | 32
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Example:
package MyPack;
public class FirstClass
{
public String i="I am public variable";
protected String j="I am protected variable";
private String k="I am private variable";
String r="I dont have any modifier";
}
package MyPack2;
import MyPack.FirstClass;
class SecondClass extends FirstClass
{
void method()
{
System.out.println(i); // No Error: Will print "I am public variable".
System.out.println(j); // No Error: Will print “I am protected variable”.
System.out.println(k); // Error: k has private access in FirstClass
System.out.println(r); // Error: r is not public in FirstClass; cannot be accessed // from
outside package
Page | 33
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
}
public static void main(String arg[])
{
SecondClass obj=new SecondClass();
obj.method();
}
}
Output:
I am public variable
I am protected variable
INTERFACE
What is interface? With an example explain how to define and implement interface. May 2021
Write a short note on interfaces and present the syntax for defining an interface. Nov 2021
Outline how interfaces are implemented in java with an example. Nov 2021 What is an interface?
How to define an interface? How one or more class can implement an interface? Outline the code
fragments. Nov 2023
An interface is similar to a class. “interface” is a keyword which is used to achieve full abstraction.
Using interface, we can specify what the class must do but not how it does.
An interface is a collection of method definitions (without implementations) and constant values.
It is a blueprint of a class. It has static constants and abstract methods.
How to Declare Interface in Java?
In Java, an interface is declared syntactically much like a class.
It is declared by using the keyword interface followed by interface name.
Why use Interface?
It is used to achieve fully abstraction. By interface, we can support the functionality of multiple
inheritance. Writing flexible and maintainable code. Declaring methods that one or more classes
are expected to implement.
Defining Interfaces:
An interface is defined much like a class. The keyword “interface” is used to define an interface.
Page | 34
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Page | 35
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
IMPLEMENTING INTERFACES
A class can implement one or more than one interface by using a keyword implements followed by
a list of interfaces separated by commas.
Syntax:
accessModifier class className implements interfaceName1, interfaceName2...
{
// method implementations; // member declaration of class;
}
Example:
interface A
{
void funcA();
}
interface B extends A
{
void funcB();
}
class C implements B
{
public void funcA()
{
System.out.println("This is funcA");
Page | 36
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
}
public void funcB()
{
System.out.println("This is funcB");
}
}
public class Demo
{
public static void main(String args[])
{
C obj = new C();
obj.funcA();
obj.funcB();
}
}
C:\Program Files\Java\jdk-19\bin>java Demo
This is funcA This is funcB
CLASS INTERFACE
The class is denoted by a keyword class. The interface is denoted by a keyword interface.
By creating an instance of a class the class
You cannot create an instance of an interface.
members can be accessed.
The class can use various access specifiers like The interface makes use of only public access
public, private or protected. specifier.
Page | 37
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
13. What is the concept of super class and sub class in inheritance?
We use the terms like parent class, child class, base class, derived class, superclass, and
subclass.
The Parent class is the class which provides features to another class. The parent class is also
known as Base class or Superclass.
The Child class is the class which receives features from another class. The child class is also
known as the Derived Class or Subclass
14. Define Single inheritance.
When a class is extended by only one class, it is called single-level inheritance in java or
simply single inheritance.
In other words, creating a subclass from a single superclass is called single inheritance. In
single-level inheritance, there is only one base class and can be one derived class.
Page | 40
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Page | 41
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Page | 43
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
Uses
It is used to achieve fully abstraction. By interface, we can support the functionality of
multiple inheritance. Writing flexible and maintainable code. Declaring methods that one or
more classes are expected to implement.
39. Write the syntax of interface.
It has the following general form:
accessModifier interface interfaceName
{
// declare constant fields.
// declare methods that abstract by default.
}
40. What modifiers may be used with in interface declaration?
An interface may be declared as public or abstract.
41. What is the difference between an interface and an abstract class? Dec 2012
Abstract class Interface
Interface can have only abstract methods.
Abstract class can have abstract and non-
Since Java 8, it can have default and static
abstract methods.
methods also.
Abstract class doesn't support multiple
Interface supports multiple inheritance.
inheritance.
Abstract class can have final, non-final, static
Interface has only static and final variables.
and non-static variables.
Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
An abstract class can extend another Java class An interface can extend another Java interface
and implement multiple Java interfaces. only.
An abstract class can be extended using An interface can be implemented using
keyword "extends". keyword "implements".
A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.
Page | 47