- 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
Can we call methods of the superclass from a static method in java?
Inheritance can be defined as the process where one (parent/super) class acquires the properties (methods and fields) of another (child/sub). With the use of inheritance, the information is made manageable in a hierarchical order. The class which inherits the properties is known as a subclass and the class whose properties are inherited is called superclass. In short, in inheritance, you can access the members (variables and methods) of the superclass using the subclass object.
Example
class SuperClass { public void display(){ System.out.println("Hello this is the method of the superclass"); } } public class SubClass extends SuperClass { public void greet(){ System.out.println("Hello this is the method of the subclass"); } public static void main(String args[]){ SubClass obj = new SubClass(); obj.display(); obj.greet(); } }
Output
Hello this is the method of the superclass Hello this is the method of the subclass
Calling superclass methods from a static context
Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).
Example
class SuperClass{ public void display() { System.out.println("This is a static method of the superclass"); } } public class SubClass extends SuperClass{ public static void main(String args[]){ //Calling methods of the superclass new SuperClass().display(); //superclass constructor new SubClass().display(); //subclass constructor } }
Output
This is a static method of the superclass This is a static method of the superclass
- Related Articles
- Can we call Superclass’s static method from subclass in Java?
- Can we call a constructor directly from a method in java?
- Can we access the instance variables from a static method in Java?
- How to call a non-static method of an abstract class from a static method in java?
- How to call the constructor of a superclass from a constructor in java?
- Can we call methods using this keyword in java?
- Can we override the static method in Java?
- Can we Overload or Override static methods in Java?
- Can we call a method on "this" keyword from a constructor in java?
- When can we call wait() and wait(long) methods of a Thread in Java?
- Can we override a private or static method in Java
- How can we call the invokeLater() method in Java?\n
- Can we declare a static variable within a method in java?
- Can we use "this" keyword in a static method in java?
- Can we call run() method directly instead of start() in Java

Advertisements