Packages with inheritance

In this blog, we will see how to customize our inheritance example in different packages.


In this example, we will see three different packages pack1, package1 and package2.

Package1 with A class

Package2 with B Class

Pack1 with example2 Class


package package1;

public class A


            public void methodA()


                        System.out.println(“Welcome in A Class”);




package package2;

public class B extends package1.A


            public void methodB()


                        System.out.println(“Welcome in B Class”);




package pack1; //            set path of package

class example2


                public static void main(String args[])


                                package2.B obj = new package2.B();      






How to compile packages program in command prompt

Javac pack1/

How to run this program in command prompt

Java pack1.example2



Welcome in A Class

Welcome in B Class


Video available on YouTube.


If you have any query on blog example, you can directly comment us or subscribe our website

Earn Money Online

1549total visits,7visits today


Hello, I am Maddy a Java blogger, trainer and developer

Related articles

1 Comment

  1. October 21, 2016 - 3:26 am

    I love this website – its so usefull and helpfull

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.


Link back if you want.

Enter Captcha Here :*

Reload Image