
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Parent and Child Classes Explained
Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another.
You can inherit the members of one class from another, use the extends keyword as:
class A extends B{}
The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).
Following is an example which demonstrates inheritance. Here, we have two classes namely Sample and MyClass. Where Sample is the parent class and the class named MyClass is the child class.
Example
class Sample{ public void display(){ // Java Arrays with Answers System.out.println("Hello this is the method of the super class"); } } public class MyClass extends Sample { public void greet(){ System.out.println("Hello this is the method of the sub class"); } public static void main(String args[]){ MyClass obj = new MyClass(); obj.display(); obj.greet(); } }
Output
Hello this is the method of the super class Hello this is the method of the sub class
Advertisements