CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 8 min · free

Abstract Classes, Interfaces and Polymorphism

8 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Why a Shape class only promises area(), how interfaces work as a contract, run time polymorphism, dynamic dispatch and the Object class.

Follows the syllabus of: ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications, ISC Class 12 Computer Science (868), GSEB Std 11 Computer Studies

On screen in this lesson

Why an abstract class?

Every shape has an area
But a plain "Shape" has no formula
Circle and Square each know their own formula
So Shape only promises area(); children write it

Rules of abstract classes

new Shape() gives a compile error
An abstract method has no body, only a semicolon
A child must override every abstract method
Or the child must itself be declared abstract
It can still have variables, constructors, normal methods

What is an interface?

A list of methods a class promises to have
Written with the keyword interface
A class uses it with the keyword implements
Methods are public and abstract by default
Variables are public, static and final: constants

Abstract class vs interface

PointAbstract classInterface
Keyword to useextendsimplements
How many?only onemany at once
Variablesany kindconstants only
Constructorsallowednot allowed
Method bodiesnormal methodsdefault, static

Polymorphism

Poly means many, morph means forms
Compile time: method overloading
Run time: method overriding
Parent reference can hold a child object

The Object class

Object is the root of every Java class
No extends written? The class extends Object
toString(): the object as text
equals(): are two objects equal?
getClass() and hashCode() also come from Object

Quick answers

Why does new Shape() fail?

An abstract class is incomplete, so it cannot create objects.

A x = new B(); which hi() runs?

B's version, because Java looks at the actual object at run time.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. One line of code says s dot area. Sometimes it finds the area of a circle, and sometimes of a square. How does Java know which one to run? Today we learn abstract classes, interfaces, run time polymorphism and the Object class.

Let us start with a real problem. Every shape, like a circle or a square, has an area. But a general shape has no formula, so you cannot find its area. A circle knows its own formula, and so does a square. So the parent class Shape should only promise an area method, and each child class should write the body.

This is how Java writes that promise. The keyword abstract before class makes Shape an abstract class. An abstract class is an incomplete class, so we cannot create its objects. The line abstract double area has no body, only a semicolon. That is an abstract method, which means a method with a heading but no body. The method show is a normal method with a body, and an abstract class may have both kinds.

Exams often ask these rules, so learn them well. First, writing new Shape gives a compile error, because an abstract class cannot make objects. Second, an abstract method ends with a semicolon and has no curly brackets. Third, a child class must override every abstract method, which means it writes its own body for it. If it does not, the child class must also be declared abstract. Finally, an abstract class can still have variables, constructors and normal methods.

Here is a complete program. To fit the screen, S is short for Shape, and Sq is short for Square. S is abstract, with an abstract method area. Sq extends S and overrides area, returning four into four. In main, we cannot write new S, but a variable of type S can hold a Sq object. So s dot area runs the square's version and prints sixteen.

Now, a second tool, called an interface. An interface is a list of methods that a class promises to have, like a contract. We write it with the keyword interface instead of class. A class accepts the contract with the keyword implements. Its plain methods are public and abstract by default, so we do not write those words. Any variable in an interface is public, static and final, which means it is a fixed constant.

Think of paying at the school canteen. Cash, card and UPI all pay money, but in different ways. The interface Pay says every payment method must have a pay method. The class Upi implements Pay and writes the body. Notice the word public before void pay. It is compulsory, because interface methods are public, and we cannot make them less visible. The program prints Paid fifty.

This comparison is a favourite exam question. A class uses extends for an abstract class, but implements for an interface. A class can extend only one class, but it can implement many interfaces at once. An abstract class can have any variables, while an interface has only constants. An abstract class can have constructors, but an interface cannot. From Java eight onwards, an interface may also have default and static methods with bodies.

Now the big idea that ties it together. Polymorphism means one name, many forms, from Greek words for many and forms. Compile time polymorphism is overloading, where methods share a name but take different parameters. Run time polymorphism is overriding, where a child class rewrites a parent method. It works because a parent type variable can hold a child object, which is called upcasting.

Here, the variable b has type Bird, but the object inside is a Crow. Crow overrides the sound method. When we call b dot sound, Java does not look at the variable type. At run time, it looks at the actual object, which is a Crow. So it prints Caw caw. This choice of method at run time is called dynamic method dispatch.

Pause the video and predict the output. The variable x is declared as type A. The object created is new B. Class B overrides the method hi. So which hi runs? The object is a B, so Java runs the B version, and it prints B. Many students wrongly answer A, because they look only at the variable type.

Finally, a class you use without knowing it. Object is the top parent of every class in Java, in the package java dot lang. If you write no extends, Java quietly makes your class extend Object. That is why every object has a to String method, which gives the object as text. Every object also has an equals method, which checks whether two objects are equal. The get Class and hash Code methods come from Object too.

When you print an object, Java calls its to String method. The version from Object prints the class name, an at sign and a code, which is not very useful. So class Stu, short for Student, overrides to String. It must be public, because it is public in Object. Now printing the object gives Student Riya. This is run time polymorphism again.

Some exams ask for this idea in pseudocode. One loop asks every shape for its area. A circle and a square each answer with their own formula.

Let us revise. An abstract class cannot make objects, and its abstract methods have no body. An interface is a contract, and a class implements it. A class can extend only one class, but it can implement many interfaces. In run time polymorphism, Java runs the method of the actual object. And every class extends Object, which gives to String and equals.

Courses that teach this

CourseUnit
ICSE Class 9 Computer ApplicationsIntroduction to Object Oriented Programming Concepts
ICSE Class 10 Computer ApplicationsUser-Defined Methods
ISC Class 12 Computer Science (868)Data Structures
GSEB Std 11 Computer StudiesAnimation Tool: Synfig
GSEB Std 12 Computer StudiesObject-Oriented Concepts and Java Basics
Edexcel GCSE GCSE Computer Science (1CP2)Topic 1: Computational thinking
Programming All levels JavaData Structures and Collections
Programming All levels C++Object-Oriented Programming

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →