Inheritance in Java

The use of "this " keyword in Java

Inheritance in Java is a mechanism that is used to pass properties and behaviours of a parent object to child objects.

In inheritance, you can create new classes that are built using existing classes.Inheritance represents IS_A relationship i.e parent-child relationship.
A child class can use methods and fields of the parent class.

Importance of Inheritance in Java

1.For method overriding .
2.Code reuse.

Inheritance Syntax

extends keyword is used to make a child class from the parent.


public class JButtonListener  extends JFrame {
}

Java Inheritance Example

Parent Class

public class Students {
    int id;
    String name;
    String class_name;
    String school_name="Ikuu Boys";
    
    
}

Child Class
public class FormOneStudents extends Students {
    String  form_name="Form one West";
    public static void main(String args[])
    {
    FormOneStudents f=new FormOneStudents();
    System.out.println("School name " +f.school_name);
     System.out.println("Form name " +f.form_name);
    }
    
}

Types of Inheritance in Java

There are 3 types of inheritance in java 
  • Single 
  • Multilevel 
  •  hierarchical.

  • 1.Example of a single Inheritance java example.
public class Animal {
   
    void eat()
    {
        System.out.println("All Animals eat");
    }
    
   
}


Child Class
public class Cat extends Animal
        {
 void Meow()
 {
 System.out.println("Cats meows");
 }
 


    public static void main(String args[])
    {
   Cat c= new Cat();
   c.Meow();
   c.eat();
    
    }}

  • 2.Multilevel Java Example
public class Animal {
   
    void eat()
    {
        System.out.println("All Animals eat");
    }
    
   
}



public class Cat extends Animal
        {
 void Meow()
 {
 System.out.println("Cats meows");
 }
 }
 
public class BabyCat  extends Cat{
    
  public static void main(String args[])
    {
   BabyCat c= new BabyCat();
   c.Meow();
   c.eat();
    
    }
}



  • 3.Hierarchical Inheritance Example

class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3{  
public static void main(String args[]){  
Cat c=new Cat();  
c.meow();  
c.eat();  
//c.bark();//C.T.Error  
}} 

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Inheritance in Java
Inheritance in Java
The use of "this " keyword in Java
Java Codes
http://www.download-all.net/2017/09/inheritance-in-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/09/inheritance-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy