- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get super class of an object in Java
The immediate superclass of any entity such as an object, class, primitive type, interface etc. can be obtained using the method java.lang.Class.getSuperclass(). This method contains no parameters.
A program that demonstrates this is given as follows −
Example
public class Main { public static void main(String[] args) { Object obj1 = new String("Hello"); Object obj2 = new Integer(15); Class c1 = obj1.getClass().getSuperclass(); System.out.println("Super Class = " + c1); Class c2 = obj2.getClass().getSuperclass(); System.out.println("Super Class = " + c2); } }
Output
Super Class = class java.lang.Object Super Class = class java.lang.Number
Now let us understand the above program.
In the method main(), getClass() is used to obtain the class and getSuperclass() is used to obtain the super class of objects obj1 and obj2. Then the super class is printed. A code snippet which demonstrates this is as follows −
Object obj1 = new String("Hello"); Object obj2 = new Integer(15); Class c1 = obj1.getClass().getSuperclass(); System.out.println("Super Class = " + c1); Class c2 = obj2.getClass().getSuperclass(); System.out.println("Super Class = " + c2);
- Related Articles
- Get class from an object in Java
- Why Object class is the super class for all classes in Java?
- Can super class reference variable hold sub class's object in java?
- What is the super class of every class in Java?
- Can we create an object of an abstract class in Java?
- Can we throw an object of generic class in java?
- How to ensure that child class overrides a super class method in java?
- Method of Object class in Java
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- Object class in Java
- How to create an object from class in Java?
- Get the Component Type of an Array Object in Java
- Variable in subclass hides the variable in the super class in Java
- Get the fully-qualified name of an inner class in Java

Advertisements