Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Single level inheritance in Java language

Posted on February 2, 2017December 27, 2019

Single level inheritance in Java language

In this tutorial, we will discuss the concept of single-level inheritance in Java language

Single level inheritance is one of the techniques in Java. It is easy to understand, a class acquires the properties of (data members and methods) another class. The diagram below indicates that class B extends only from class A. Here, A is the parent class of B, therefore, B is the child class of A.
Single inheritance
single level inheritance in Java language
single level inheritance

 

single level inheritance in java
single level inheritance
The diagram above shows that the school class is the base class or parent class. Data members and methods are properties of the school class. Student class is the child class or derived class and this class acquires the properties of the school class.


Syntex

class Super{
…………
…………

}

class Sub extends Super{
…………
…………
 
}



 Example
program 1
 
class Base
{
void displayb()
{
System.out.println(“thnis is Base class display”);
}
}
class drived extends Base
{
void displayc()
{
System.out.println(“thnis is drived class display”);
}
public static void main(String args[]){
drived d=new drived();
d.displayb();
    d.displayc();
}
}
 
Program2
 
class Base1
{
void displayb()
{
System.out.println(“thnis is Base class display”);
}
}
class drived extends Base
{
void displayc()
{
System.out.println(“thnis is drived class display”);
}
}
class simpleinherit{
public static void main(String args[]){
drived d=new drived();
d.displayb();
    d.displayc();
}
}
 
 

Program 3


class teacher{            //class teacher
float salary=40000;
}
//………………….
class principal extends teacher{   // class principal
float bonus=10000;
public static void main (String args[]){  //main method
principal p=new principal();   //create object
System.out.println(“teacher salary is:”+p.salary);  
//access variable of teacher class from this class
System.out.println(“principal bonus is:”+p.bonus);
//access variable of this class
}
}

program 4

class maths2
{
int a;  // global variable 
public void add(int x, int y){ // create method add
//parameter passing to method add as int a, int b
a=x+y;
System.out.println(“The sum of the x,y:”+a);
}
public void sub(int x, int y){//create method sub
//parameter passing to method sub as int a, int b
a=x-y;
System.out.println(“The different of the x,y:”+a);
}
}
public class calc extends maths2{
public void mul(int x, int y){
a=x*y;
System.out.println(“The multiplication of the x,y:”+a);
}
public static void main (String args[])//main method
{
calc demo=new calc(); //create object
demo.add(34,56);  // argument passing
demo.sub(78,54);
demo.mul(78,79);
}
}
Related Links
 
Inheritance in Java                     Inheritance in C++
 
Multi levelinheritance in Java    Multilevel inheritance in C++
Single level inheritance in C++   Multiple Inheritance in C++
 
Hierarchical Inheritance in Java
 
Interface in Java
Hybrid inheritance in C++

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes