package oops;
class Superclass
{
// super class default m1 method
void m1()
{
System.out.println("Super class method");
}
// super class private m2 method
private void m2()
{
System.out.println("Super class method");
}
// super class public m3 method
public void m3()
{
System.out.println("Super class method");
}
// super class protected m4 method
protected void m4()
{
System.out.println("Super class method");
}
}
public class Subclass extends Superclass {
//If not provide proper allowed modifiers it leads compiler error
//cannot reduce the visibility of the inherited method from super class
//sub class default m1 method allowed default,public,protected
void m1()
{
System.out.println("Super class method");
}
//sub class private m2 allowed private,default,protected,public
private void m2()
{
System.out.println("Super class method");
}
//super class public m3 method allowed modifiers public only
public void m3()
{
System.out.println("Super class method");
}
// super class protected m4 method protected,public
protected void m4()
{
System.out.println("Super class method");
}
public static void main(String[] args) {
Subclass s=new Subclass();
s.m1();
s.m2();
s.m3();
s.m4();
}
}
output:
Super class method
Super class method
Super class method
Super class method
2 comments:
cool dude
private methods we can't override (i.e private void m2() gives compile time error)
Post a Comment