Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Public Class A (Protected Int N Public A (N 0 ) Public A (Int I) (N I ) Public Void Setn (Int I) (N I ) Public Int Getn (Return N ) )

The document discusses inheritance, method overriding, abstract classes, and the final keyword in Java. It provides examples of defining subclasses that inherit properties from superclasses, overriding methods by changing the method body but not signature, creating abstract classes with abstract methods that have no body, and using the final keyword to prevent overriding of methods or inheritance of classes.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Public Class A (Protected Int N Public A (N 0 ) Public A (Int I) (N I ) Public Void Setn (Int I) (N I ) Public Int Getn (Return N ) )

The document discusses inheritance, method overriding, abstract classes, and the final keyword in Java. It provides examples of defining subclasses that inherit properties from superclasses, overriding methods by changing the method body but not signature, creating abstract classes with abstract methods that have no body, and using the final keyword to prevent overriding of methods or inheritance of classes.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

# Inheritance:

Rule:

Super class must be defined first.

Have to use the `extends’ keyword after the subclass name and before the
superclass name.

Multiple superclass for a particular subclass is not allowed.

Only the non-private properties of super class will inherit.

public class A {

protected int n;

public A(){

n=0;

public A(int i){

n=i;

public void setN(int i){

n=i;

public int getN(){

return n;

}
public class B extends A {

public double d;

public void setD(double d1){

d=d1;

public double getD(){

return d;

public int getBN(){

return n;

public class Main {

public static void main(String[] args) {

A a=new A(10);

B b=new B();

a.setN(200);

b.setN(400);

System.out.println("a.n: "+a.n+"\nb.n: "+b.n);

//System.out.println("b.n: "+b.getN());

b.setD(15.7);

System.out.println("b.D: "+b.getD());
}

# Super has two different forms . the first calls the superclass ‘constractor’. The
second is used to access a member of the superclass that has been hidden by a
member of a sub class.

# method overriding

When a method in a subclass has the same name and type signature as a method
in its super class , then the method in the subclass is said to override the method
in the superclass.

Rule:

No changes in the signature of the method but change in the body.

Method overriding allows Run time polymorphism

public class A {

int i,j;

public A(int a,int b){

i=a;

j=b;

void show(){

System.out.println("i and j: "+i+" "+j);

}
}

public class B extends A {

int k;

B(int a,int b,int c){

super(a,b);

k=c;

void show(){

System.out.println("k: "+k);

public class Main {

public static void main(String[] args) {

A a=new A(4,5);

a.show();

B b=new B(1,2,3);

b.show();

# Abstract :

By using ‘abstract’ key word a method or a class can be define as abstract .

Abstract class at least need one abstract method.

Abstract method does not have any body .

No object can be created from abstract class.


Reference can be created from abstract class.

public abstract class A {

abstract void callme();

void callmetoo(){

System.out.println("this is a concrete method");

public class B extends A {

void callme(){

System.out.println("B's implementetion of callme.");

public class Main {

public static void main(String[] args) {

B b=new B();

b.callme();

b.callmetoo();

}
}

# Final:

‘final’ keyword has three uses as given below :

1. To make a variable’s value constant,

int a;

final a=10;

a=100; // this will show as error

but if we print a then the value of a will still be 10.

2. Using final to prevent overriding,

To disallow a method from being overridden final have to be used as modifier at the start of its
declaration. Method declared as final cannot be overridden.

class A{

final void JJJ(){

System.out.println(“This can never be changed”);

class B extends A {

void JJJ(){ //ERROR

ILLIGAL!

3. Using final to prevent inheritance,


final class A{

//…..

Class B extends A{ //ERROR

//…..

It is illegal for B to inherit A since A is declared as final.

You might also like