Abstract Class
• serves as a blueprint for other classes.
• It cannot be instantiated directly (you cannot create objects from it), but it
can define both abstract methods (without implementation) and concrete
methods (with implementation).
Features
• Declared using the keyword.
• Cannot be instantiated directly.
• Can contain:
• Abstract methods
methods without a body, meant to be implemented by subclasses.
• Concrete methods → methods with full implementation.
• Constructors → used when subclasses are created.
• Instance variables → fields that subclasses can inherit.
• Static and final methods.
• Supports partial abstraction (unlike interfaces, which provide full abstraction).
• Subclasses must implement all abstract methods unless they are also
declared abstract.
abstract class Shape
{
abstract void draw(); // abstract method
void display()
{ // concrete method
[Link]("This is a shape.");
}
}
class Circle extends Shape
{
void draw()
{
[Link]("Drawing a circle");
}
}
public class Main
{
public static void main(String[] args)
{
Shape s = new Circle();
[Link]();
[Link]();
}
}
abstract class Subject {
Subject() {
[Link]("Learning Subject");
}
abstract void syllabus();
void learn() {
[Link]("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus() {
[Link]("C, Java, C++");
}
}
public class Main {
public static void main(String[] args) {
Subject sub = new IT();
[Link]();
[Link]();
}
}
Interfaces
• An interface in Java is a blueprint for a class,
• serving as a contract that defines a set of methods a class must
implement, without providing the implementation details itself.
• This mechanism is used to achieve total abstraction and enable the
functionality of multiple inheritance in Java
Features of interface
•Abstraction:
Interfaces hide the internal workings of the methods, showing only what methods are available (the signature), which simplifies the code and makes it easier to
manage.
•Multiple Inheritance:
Java classes can only inherit from one superclass, but they can implement multiple interfaces, allowing a single class to have many different behaviors.
•Loose Coupling:
Interfaces reduce the dependencies between different parts of a program, as classes rely on the interface's contract rather than specific class implementations.
•Cannot be Instantiated:
Like abstract classes, you cannot create an object directly from an interface. A class must implement the interface to be used.
•Members are Implicitly Public:
All members (methods and fields) declared in an interface are automatically
// Interface Declared
interface testInterface {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// Class implementing interface
class TestClass implements testInterface {
// Implementing the capabilities of Interface
public void display(){
[Link](“Hi");
}
}
class ABC{
public static void main(String[] args){
TestClass t = new TestClass();
[Link]();
[Link](t.a);
}
}
Inner Class
• An inner class is a class declared inside the body of another class.
• The inner class has access to all members (including private) of the
outer class
• but the outer class can access the inner class members only through
an object of the inner class.
public class OuterClass {
class InnerClass {
void display() {
[Link]("Hello from Inner Class!");
}
}
public static void main(String[] args) {
OuterClass outer = new OuterClass();
InnerClass inner = [Link] InnerClass();
[Link]();
}
}
member inner class
class Outer {
private int outerVar = 100;
// Member inner class
class Inner {
void display() {
[Link]("Outer variable: " + outerVar);
}
}
}
class Main {
public static void main(String[] args) {
// Creating inner class instance
[Link] inner = new Outer().new Inner();
[Link]();
}
}
Method-Local inner class
A method-local inner class is defined inside a method of the outer class. It can only be
instantiated within the method where it is defined
class Outer {
void outerMethod() {
[Link]("Inside outerMethod");
// Method-local inner class
class Inner {
void innerMethod() {
[Link]("Inside innerMethod");
}
}
Inner inner = new Inner();
[Link]();
}
}
class Main {
public static void main(String[] args) {
Outer outer = new Outer();
[Link]();
}
}
Static Nested class
class Outer {
private static int staticVar = 50;
• A static nested
class is a static // Static nested class
static class Inner {
class defined void display() {
inside another [Link]("Static variable: " + staticVar);
}
class. It does not }
have access to }
instance members class Main {
of the outer class public static void main(String[] args) {
but can access // No need for outer class instance
[Link] inner = new [Link]();
static members. [Link]();
}
}