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

1681total visits,2visits today


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

Related articles

One response to “Packages with inheritance”