class Base
{
// code
}
class Derive extends Base
{
// code
}
class SuperDemo
{
int result;
public void square(int a)
{
result = a * a;
System.out.println("The square of the "+a+" is: "+result);
}
}
public class SubDemo extends SuperDemo
{
public void cube(int a)
{
result = a * a * a;
System.out.println("The cube of the "+a+" is: "+result);
}
public static void main(String args[])
{
int a = 25;
SubDemo sub = new SubDemo();
sub.square(a);
sub.cube(a);
}
}
class A
{
//code
}
class B extends A
{
//code
}
class Parent
{
public void m1()
{
System.out.println("Class Parent method");
}
}
public class Child extends Parent
{
public void m2()
{
System.out.println("Class Child method");
}
public static void main(String args[])
{
Child obj = new Child();
obj.m1();
obj.m2();
}
}
class A
{
//code
}
class B extends A
{
//code
}
class C extends B
{
//code
}
class Grand
{
public void m1()
{
System.out.println("Class Grand method");
}
}
class Parent extends Grand
{
public void m2()
{
System.out.println("Class Parent method");
}
}
class Child extends Parent
{
public void m3()
{
System.out.println("Class Child method");
}
public static void main(String args[])
{
Child obj = new Child();
obj.m1();
obj.m2();
obj.m3();
}
}
class A
{
// code
}
class B extends A
{
// code
}
class C extends A
{
//code
}
class A
{
public void m1()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void m2()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void m3()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void m4()
{
System.out.println("method of Class D");
}
}
public class MainClass
{
public void m5()
{
System.out.println("method of Class MainClass");
}
public static void main(String args[])
{
A obj1 = new A();
B obj2 = new B();
C obj3 = new C();
D obj4 = new D();
obj1.m1();
obj2.m1();
obj3.m1();
obj4.m1();
}
}
class X
{
void display()
{
System.out.println("class X dispaly method ");
}
}
class Y
{
void display()
{
System.out.println("class Y display method ");
}
}
public class Z extends X,Y
{
public static void main(String args[])
{
Z obj=new Z();
obj.display();
}
}
public static void main(String args[])
{
// code
}
int a = 25;
String str = "Java";
boolean m1()
{
return true;
}
class Employee
{
protected int id = 101;
protected String name = "Jack";
}
public class ProtectedDemo extends Employee
{
private String dept = "Networking";
public void display()
{
System.out.println("Employee Id : "+id);
System.out.println("Employee name : "+name);
System.out.println("Employee Department : "+dept);
}
public static void main(String args[])
{
ProtectedDemo pd = new ProtectedDemo();
pd.display();
}
}
public class PrivateDemo
{
private int a = 101;
private String s = "TutorialRide";
public void show()
{
System.out.println("Private int a = "+a+"\nString s = "+s);
}
public static void main(String args[])
{
PrivateDemo pd = new PrivateDemo();
pd.show();
System.out.println(pd.a+" "+pd.s);
}
}
Access modifier | In class | In package | Outside package by subclass | Outside package |
---|---|---|---|---|
public | Yes | Yes | Yes | No |
protected | Yes | Yes | Yes | No |
default | Yes | Yes | No | No |
private | Yes | No | No | No |
class Animal
{
public Animal(String str)
{
System.out.println("Constructor of Animal: " + str);
}
}
class Lion extends Animal
{
public Lion()
{
super("super call Lion constructor");
System.out.println("Constructer of Lion.");
}
}
public class Test
{
public static void main(String[] a)
{
Lion lion = new Lion();
}
}
class Base
{
int a = 50;
}
public class Derive extends Base
{
int a = 100;
void display()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
Derive derive = new Derive();
derive.display();
}
}
class FinalVarDemo
{
final int a = 10;
void show()
{
a = 20;
System.out.println("a : "+a);
}
public static void main(String args[])
{
FinalVarDemo var = new FinalVarDemo();
var.show();
}
}
class Animal
{
final void eat()
{
System.out.println("Animals are eating");
}
}
public class Dear extends Animal
{
void eat()
{
System.out.println("Dear is eating");
}
public static void main(String args[])
{
Dear dear = new Dear();
dear.eat();
}
}
final class Animal
{
void eat()
{
System.out.println("Animals are eating");
}
}
class Dear extends Animal
{
void eat()
{
System.out.println("Dear is eating");
}
public static void main(String args[])
{
Dear dear = new Dear();
dear.eat();
}
}