0% found this document useful (0 votes)
5 views

Unit 2 Notes Oops

The document provides notes on Object Oriented Programming, specifically focusing on inheritance, method overloading, and the use of packages and interfaces in Java. It explains concepts such as method overloading, passing objects as parameters, returning objects, and different types of classes including static, nested, and inner classes. Additionally, it covers the basics of inheritance, its types, and the relationship between base and derived classes in Java.

Uploaded by

sharmila11121311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 2 Notes Oops

The document provides notes on Object Oriented Programming, specifically focusing on inheritance, method overloading, and the use of packages and interfaces in Java. It explains concepts such as method overloading, passing objects as parameters, returning objects, and different types of classes including static, nested, and inner classes. Additionally, it covers the basics of inheritance, its types, and the relationship between base and derived classes in Java.

Uploaded by

sharmila11121311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

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)

System.out.println("Value of Count after method call = "+count);


}
}
Output:
Value of Count before method call = 100
Value of Count after method call = 100

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

STATIC, NESTED AND INNER CLASSES


 In Java, inner class refers to the class that is declared inside class or interface.

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.

1. Java Member inner class


 A non-static class that is created inside a class but outside a method is called member inner class.
Syntax:
class Outer
{
//code
class Inner
{
//code
}
}
Program
class TestMemberOuter1
{
private int data=30;
class Inner
{
void msg()
{
System.out.println("data is "+data);
}
}
public static void main(String args[])
{
TestMemberOuter1 obj=new TestMemberOuter1();
TestMemberOuter1.Inner in=obj.new Inner();
in.msg();
}
}
Page | 7
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

Output:
data is 30

2. Java Anonymous inner class


 A class that have no name is known as anonymous inner class in java. It should be used if you
have to override method of class or interface.
Java Anonymous inner class can be created by two ways:
1. Class (may be abstract or concrete).
2. Interface
Program – Using Class
abstract class Person
{
abstract void eat();
}
class TestAnonymousInner
{
public static void main(String args[])
{
Person p=new Person()
{
void eat()
{
System.out.println("nice fruits");
}
};
p.eat();
}
}
Output:
nice fruits

3. Java Local inner class


 A class i.e. created inside a method is called local inner class in java. If you want to invoke the
methods of local inner class, you must instantiate this class inside the method.
public class localInner1
{
private int data=30;//instance variable
void display()
{
int value=50;
class Local
Page | 8
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

{
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

4. Java static Nested class


 A static class i.e. created inside a class is called static nested class in java. It cannot access non-
static data members and methods. It can be accessed by outer class name.
class TestOuter1
{
static int data=30;
static class Inner
{
void msg()
{
System.out.println("data is "+data);
}
}
public static void main(String args[])
{
TestOuter1.Inner obj=new TestOuter1.Inner();
obj.msg();
}
}
Output:
data is 30
Page | 9
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

INHERITANCE (IS A RELATIONSHIP)


Explain in detail about the basics of inheritance and elaborate on any two inheritance mechanism
in java. Apr 2023
 Inheritance is a process of deriving a new class from existing class, also called as “extending a
class”. When an existing class is extended, the new (inherited) class has all the properties and
methods of the existing class and also possesses its own characteristics.
 To inherit a class, you simply incorporate the definition of one class into another by using the
extends keyword.
 The class whose property is being inherited by another class is called “base class” (or) “parent
class” (or) “super class”. The class that inherits a particular property or a set of properties from
the base class is called “derived class” (or) “child class” (or) “sub class”.
How to use inheritance in Java - The keyword used for inheritance is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}

What is Is-A relationship in Java?


 Is-A relationship in java represents Inheritance.
 It is implemented in Java through keywords extends (for class inheritance) and implements (for
interface implementation).
Advantages of Inheritance
1. Reusability: The base class code can be used by derived class without any need to rewrite the code.
2. Extensibility: The base class logic can be extended in the derived classes.
3. Data hiding: Base class can decide to keep some data private so that it cannot be altered by the
derived class.
4. Overriding: With inheritance, we will be able to override the methods of the base class so that
meaningful implementation of the base class method can be designed in the derived class

Characteristics of Class Inheritance:


1. A class cannot be inherited from more than one base class. Java does not support the inheritance of
multiple super classes into a single subclass.
2. Sub class can access only the non-private members of the super class.

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)

Why multiple inheritance is not supported in java?


 To reduce the complexity and simplify the language, multiple inheritances 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 same method and you call it from child class object, there will be ambiguity to
call method of A or B class.
How does Multiple inheritance implement in Java?
 Multiple inheritance can be implemented in Java by using interfaces.
Program
interface Printable
{
void print();
}
interface Showable
{
void show();
}
class A implements Printable, Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[])
{
A obj = new A();
obj.print();
obj.show();
}
}
Output:
Hello
Welcome

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;
}

BoxWeight(double w, double h, double d, double m) {


super(w, h, d);
weight = m;
}
BoxWeight()
{
super();
weight = -1;
}
BoxWeight(double len, double m)
{
super(len);
weight = m;
}
}
public class DemoBoxWeight
{
public static void main(String[] args)
{
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
Page | 18
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

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

Volume of mybox2 is 24.0


Weight of mybox2 is 0.076

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.

1. Use of super with variables


 This scenario occurs when a derived class and base class has same data members.
Program
class Person
{
int mark = 100;
}
class Student extends Person
{
int mark = 90;
void display()
{
System.out.println("Mark: " + super.mark);
}
}

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)

 A method declared static cannot be overridden but can be re-declared.


 If a method cannot be inherited, then it cannot be overridden.
 Constructors cannot be overridden.

Advantage of Java Method Overriding


 Method Overriding is used to provide specific implementation of a method that is already
provided by its super class.
 Method Overriding is used for Runtime Polymorphism

Differentiate method overloading and Method overriding. Nov 2022


Method Overloading Method Overriding
Method overloading is a compile-time
Method overriding is a run-time polymorphism.
polymorphism.
It provides specific implementation of the
It helps to increase the readability of
method which is already provided by its parent
the program.
class or superclass.
It is performed in two classes with inheritance
It occurs within the class.
relationships.
Method overloading may or may not require
Method overriding always needs inheritance.
inheritance.
In method overloading, methods must have the In method overriding, methods must have the
same name and different signatures. same name and same signature.
In method overloading, the return type can or
In method overriding, the return type must be
cannot be the same, but we just have to change
the same.
the parameter.
It gives better performance. Poor performance.

DYNAMIC METHOD DISPATCH


 Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved
at run time, rather than compile time. Dynamic method dispatch is important because this is how
Java implements run-time polymorphism.
Example:
class A
{
void callme()
{
System.out.println("Inside A's callme method");
}
}
class B extends A
{
Page | 23
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)

ABSTRACT CLASSES AND METHODS


What is an abstract class? Illustrate with an example. Nov 2019 Briefly discuss about Abstract
classes. Apr 2022 with an example explain the use of abstract class in java. Apr 2024
 Abstraction is a process of hiding the implementation details and showing only the essential
features to the user. An abstract is a java modifier applicable for classes and methods in java but
not for Variables.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
1. Abstract Classes & Methods in Java
Abstract Classes
 A class that is declared as abstract is known as abstract class. Abstract classes cannot be
instantiated, but they can be subclassed.
Syntax
abstract class <class_name>
{
Member variables;
Concrete methods
{
}
Abstract methods();
}
Properties of abstract class:
 Abstract keyword is used to make a class abstract. Abstract class can’t be instantiated.
 If a class has abstract methods, then the class also needs to be made abstract using abstract
keyword, else it will not compile.
 Abstract classes can have both concrete methods and abstract methods.
 The subclass of abstract class must implement all the abstract methods unless the subclass is also
an abstract class.
 A constructor of an abstract class can be defined and can be invoked by the subclasses.
 We can run abstract class like any other class if it has main() method.
Abstract Method
 A method that is declared as abstract and does not have implementation is known as abstract
method. It acts as placeholder methods that are implemented in the subclasses.
Syntax:
abstract class classname
{
abstract return_type <method_name>(parameter_list);//no braces{} // no implementation required
……..
}
Page | 25
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

Properties of abstract methods:


 The abstract keyword is also used to declare a method as abstract.
 An abstract method consists of a method signature, but no method body.
 If a class includes abstract methods, the class itself must be declared abstract.
 Abstract method would have no definition, and its signature is followed by a semicolon, not curly
braces as follows. Any child class must either override the abstract method or declare itself
abstract.
abstract class A
{
abstract void callme();
void callmetoo()
{
System.out.println("This is a concrete method.");
}
}
class B extends A
{
void callme()
{
System.out.println("B's implementation of callme.");
}
}
class AbstractDemo
{
public static void main(String[] args)
{
B b = new B();
b.callme();
b.callmetoo();
}
}
C:\Program Files\Java\jdk-19\bin>java AbstractDemo
B's implementation of callme.
This is a concrete method.
Abstract Class Concrete Class
An abstract class is declared using abstract A concrete class is not declared using abstract
modifier. modifier.
An abstract class cannot be directly instantiated A concrete class can be directly instantiated using
using the new keyword. the new keyword.
An abstract class may or may not contain abstract A concrete class cannot contain an abstract
methods. method.
An abstract class cannot be declared as final. A concrete class can be declared as final.
Page | 26
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

FINAL METHODS AND CLASSES


Discuss the Final Keyword in Java with example. Nov 2018
 Final is a keyword or reserved word in java used for restricting some functionality. It can be
applied to member variables, methods, class and local variables in Java.
 Java final keyword is a non-access specifier that is used to restrict a class, variable, and method.
 If we initialize a variable with the final keyword, then we cannot modify its value.
 If we declare a method as final, then it cannot be overridden by any subclasses.
 And, if we declare a class as final, we restrict the other classes to inherit or extend it.

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)

MEMBER ACCESS IN PACKAGES


 Packages act as containers for classes and other subordinate packages.
 Classes act as containers for data and code. The class is Java’s smallest unit of abstraction. Because
of the interplay between classes and packages.
Java addresses four categories of visibility for class members:
1. Subclasses in the same package
2. Non-subclasses in the same package
3. Subclasses in different packages
4. Classes that are neither in the same package nor subclasses
The three access modifiers,
1. Private - The private modifier specifies that the member can only be accessed in its own class.
2. Public - if a member is declared with public, it is visible and accessible to all classes everywhere.
3. Protected - The protected modifier specifies that the member can only be accessed within its own
package and, in addition, by a subclass of its class in another package.
Access Level Private Default Protected Public
Same class Yes Yes Yes Yes
Class in Same package No Yes Yes Yes
Subclass in a Same package No Yes Yes Yes
Sub class outside the same package No No Yes Yes
Non-subclass outside the same package No No No Yes

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.

It has the following general form:


accessModifier interface interfaceName
{
// declare constant fields.
// declare methods that abstract by default.
}

Page | 34
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

Extending Interface in Java


 An interface can extend other interfaces, just as a class subclass or extend another class.
 It can be done by using the keyword “extends”.
Syntax:
interface interfaceName2 extends interfaceName1
{
// body of interfaceName2.
}
Example
interface A
{
void funcA();
}
interface B extends A
{
void funcB();
}

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)

UNIT – II – TWO MARKS


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.

1. Define method overloading.


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.
2. Define 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.
3. Define 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.
4. Define Inner class. What are the four types of inner class?
 In Java, inner class refers to the class that is declared inside class or interface.

5. What are the advantages of inner class?


 Nested classes are used to develop more readable and maintainable code because it logically
group classes and interfaces in one place only.
 Code Optimization: It requires less code to write.
6. State the difference between inner class and anonymous class. Nov 2011
 The inner class is a kind of class within the body of a method. An inner class can have any
accessibility including private.
 Anonymous inner class is a local class without any name Because it has no name it can only be
used once at place of declaration.
Page | 38
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

7. Define Static Nested class and Inner classes.


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.
8. Define Inheritance. or Define Extended class. Nov 2018 Nov 2023
Inheritance is a process of deriving a new class from existing class, also called as “extending a class”.
When an existing class is extended, the new (inherited) class has all the properties and methods of
the existing class and also possesses its own characteristics.
 Keyword extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
9. What is Is-A relationship in Java?
 Is-A relationship in java represents Inheritance. It is implemented in Java through keywords
extends (for class inheritance) and implements (for interface implementation).
10. What are the advantages of inheritance?
1. Reusability: The base class code can be used by derived class without any need to rewrite the
code.
2. Extensibility: The base class logic can be extended in the derived classes.
3. Data hiding: Base class can decide to keep some data private so that it cannot be altered by the
derived class.
4. Overriding: With inheritance, we will be able to override the methods of the base class so that
meaningful implementation of the base class method can be designed in the derived class.
11. Enlist various forms of inheritance.
1) Single inheritance
2) Multilevel Inheritance
3) Hierarchical Inheritance
4) Multiple Inheritance
5) Hybrid Inheritance
12. Explain the use of extend keyword.
 The keyword used for inheritance is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
} When the child class is derived from its parent class then the keyword extends is used.
Page | 39
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.

15. Define Multilevel inheritance.


 The process of creating a new sub class from an already inherited sub class is known as
Multilevel Inheritance.

16. Define Hierarchical Inheritance.


 The process of creating more than one sub classes from one super class is called Hierarchical
Inheritance.

Page | 40
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

17. Define 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.
 Multiple inheritance can be implemented in Java by using interfaces.
 A class cannot extend more than one class but a class can implement more than one interface
 Look at the below figure where class C is derived from two superclasses A and B.

18. Why is multiple inheritance using classes a disadvantage in Java?


 In multiple inheritance, the child class is derived from two or more parent classes.
 It can lead to ambiguity when two base classes implement a method with the same name.
19. Define 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.

Page | 41
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

20. Differentiate between inheritance and polymorphism.


Inheritance Polymorphism
Inheritance is one in which a new class is
created (derived class) that inherits the Whereas polymorphism is that which can be
features from the already existing class(Base defined in multiple forms.
class).
Whereas it is basically applied to functions or
It is basically applied to classes.
methods.
Polymorphism allows the object to decide
Inheritance supports the concept of reusability
which form of the function to implement at
and reduces code length in object-oriented
compile-time (overloading) as well as run-time
programming.
(overriding).
Whereas it can be compiled-time
Inheritance can be single, hybrid, multiple,
polymorphism (overload) as well as run-time
hierarchical and multilevel inheritance. polymorphism (overriding).
21. Define Keyword super. What is the use of super keyword? May 2021
Can we access parent class variable in child class by using super keyword? Nov 2022
Yes How does a subclass can call a constructor defined by its super class? Nov 2023
 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.
22. What is meant by method overriding? Nov 2013
Illustrate method overriding with an example. Apr
2024
 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.
23. What are the rules / conditions to be followed for method overriding? Apr 2019
 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.
 A method declared static cannot be overridden but can be re-declared.
 If a method cannot be inherited, then it cannot be overridden.
 Constructors cannot be overridden.
24. Define Dynamic method dispatch. How dynamic method resolution is achieved in java. Apr
2023 How Dynamic method resolution is achieved in java? Apr 2023
 Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time. Dynamic method dispatch is important
Page | 42
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)
because this is how Java implements run-time polymorphism.

Page | 43
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

25. What is abstract class?


 A class that is declared as abstract is known as abstract class. Abstract classes cannot be
instantiated, but they can be subclassed.
26. Can an abstract class be final? Why? Nov 2010
 The abstract class cannot be final because once we declared the abstract base class with the
keyword final then it cannot be inherited.
27. Can an abstract class in Java be instantiated? Give the reason. Nov 2013
When a class must be declared as abstract. Nov 2022
 The abstract class cannot be instantiated (i.e. we cannot create the object of this class using
new operator) because the abstract class is very much general and less specific. It does
nothing and simply lists out only common features of other classes.
28. Difference abstract class and Concrete class.
Abstract Class Concrete Class
An abstract class is declared using abstract A concrete class is not declared using abstract
modifier. modifier.
An abstract class cannot be directly A concrete class can be directly instantiated using
instantiated using the new keyword. the new keyword.
An abstract class may or may not contain A concrete class cannot contain an abstract
abstract methods. method.
An abstract class cannot be declared as final. A concrete class can be declared as final.
29. What is the purpose of 'final' keyword? Nov 2011, 2012
What is the use of final keyword? Apr 2023
 Final is a keyword or reserved word in java used for restricting some functionality. It can be
applied to member variables, methods, class and local variables in Java.
 Java final keyword is a non-access specifier that is used to restrict a class, variable, and
method.
 If we initialize a variable with the final keyword, then we cannot modify its value.
 If we declare a method as final, then it cannot be overridden by any subclasses.
 And, if we declare a class as final, we restrict the other classes to inherit or extend it.
30. What is final variable?
 Any variable either member variable or local variable (declared inside method or block)
modified by final keyword is called final variable.
Example: final int THRESHOLD = 5;
31. What is final class?
 Java class with final modifier is called final class in Java and they cannot be sub-classed or
inherited.
32. What is final method?
 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.
Page | 44
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

33. Define package. May 2017


 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 syntax of package
package package_name.[sub_package_name];
public class classname
{
……..
……..
}
34. What is the purpose of package?
 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.
35. Mention the necessity for import statement in package. Apr 2012
 The import statement is used to refer the classes and methods that are present in particular
package. This statement is written at the beginning of any Java program.
For example-
importjava.io.*
36. Define Class path? May 2011, May 2012
 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.
 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
37. What are the member accesses in package?
The three access modifiers,
1. Private - The private modifier specifies that the member can only be accessed in its own class.
2. Public - if a member is declared with public, it is visible and accessible to all classes
everywhere.
3. Protected - The protected modifier specifies that the member can only be accessed within its
own package and, in addition, by a subclass of its class in another package.
38. Define interface. State its use. My 2011, May 2012, Dec 2012, May 2015, Nov 2019
 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.
Page | 45
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.

42. How to extending interface?


 An interface can extend other interfaces, just as a class subclass or extend another class.
 It can be done by using the keyword “extends”.
Syntax:
interface interfaceName2 extends interfaceName1
{
// body of interfaceName2.
}
Page | 46
II Year / III Semester – CSE CS3391 – OBJECT ORIENTED PROGRAMMING (R-2021)

43. How to implementing interface?


 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 interfaceName
{
// method implementations;
// member declaration of class;
}
44. Compare class and Interface.
CLASS INTERFACE
The interface is denoted by a
The class is denoted by a keyword class
keyword interface
The class contains data members and methods.
The interfaces may contain data members and
But the methods are defined in class
methods but the methods are not defined.
implementation.
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 | 47

You might also like