0% found this document useful (0 votes)
8 views3 pages

Polymorphism

The document demonstrates method overloading and overriding in Java using different classes. It shows how methods can be overloaded by having the same name but different parameters, and overridden by subclasses to change the implementation while maintaining the method signature. Various access modifiers, constructors, and polymorphism concepts are also displayed.

Uploaded by

bryantmontorio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Polymorphism

The document demonstrates method overloading and overriding in Java using different classes. It shows how methods can be overloaded by having the same name but different parameters, and overridden by subclasses to change the implementation while maintaining the method signature. Various access modifiers, constructors, and polymorphism concepts are also displayed.

Uploaded by

bryantmontorio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package FirstPackage;

public class MethodOverloading {

public static void main(String[] args) {


// TODO Auto-generated method stub
ClassA a = new ClassA();
System.out.println( a.sum( 25, 50 ) );
System.out.println( a.sum( 1.50, 5.75 ) );
System.out.println( ClassA.sum() );

// Groupie member1 = new Groupie();


SuperParent sp = new SuperParent();
Calculator cc = new Calculator();
cc.addition( 1, 2 );
cc.addition( 2.5, 2.5 );
cc.addition( 1, 2, 3, 4, 5, 6, 7, 8 );
System.out.println( "" );
ClassParent cp = new ClassParent();
// upcasting
ClassParent obj = new ClassChild();
ClassChild cc1 = new ClassChild();
cp.ageVerification(18);
cc1.ageVerification(18);
}
}
//class Groupie extends SuperParent {
//
//}
class ClassParent {
public void ageVerification( int a ) {
if ( a >= 18 ) {
System.out.println( "Adult" );
} else {
System.out.println( "Under Age" );
}
}
}

class ClassChild extends ClassParent {


// anotation
@Override
public void ageVerification( int a ) {
if ( a >= 21 ) {
System.out.println( "Adult" );
} else {
System.out.println( "Under Age" );
}
}

boolean ageVerification( int a, String name ) {


if ( a >= 21 ) {
return true;
} else {
return false;
}
}
}
class ClassA {
public int sum( int a, int b ) {
return a + b;
}

protected double sum( double a, double b ) {


return a + b;
}

static public int sum() {


return 5 + 5;
}
}

class SuperParent {
private String fname;
private String lname;
private int age;

SuperParent( String fname, String lname, int age ) {


this.setFname(fname);
this.setLname(lname);
this.setAge(age);
}

SuperParent( String fname, String lname ) {


this.setFname(fname);
this.setLname(lname);
}

SuperParent() {
this.setFname("John");
this.setFname("Doe");
this.setAge(0);
}

public String getFname() {


return fname;
}
public String getLname() {
return lname;
}

public void setFname(String fname) {


this.fname = fname;
}

public void setLname(String lname) {


this.lname = lname;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}
}
// access modifier - public > protected > default > private
// Polymorphism - methods
// Method Overloading - same name of methods, different parameter list, in the same
class
// - can be different return
// - can be static/final
// - can be any access modifier
// - cannot be same parameter list
// Method Overrriding - same name of methods same name, same count and type of
parameter list,
// - cannot override static/final private method
// - cannot override public with protected or default
// upcasting/downcasting
// Constructor with parameter list

// OOP
// Encapsulation
// Inheritance
// Polymorphism
// Abstraction

You might also like