0% found this document useful (0 votes)
2 views35 pages

Chapter 6

The document provides an overview of Java programming concepts, focusing on relationships among classes, including inheritance, interfaces, packaging, and inner classes. It explains inheritance mechanisms, method overriding, abstract classes, and the implementation of interfaces. Additionally, it covers class visibility, the use of the toString() method, and the organization of classes within packages.

Uploaded by

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

Chapter 6

The document provides an overview of Java programming concepts, focusing on relationships among classes, including inheritance, interfaces, packaging, and inner classes. It explains inheritance mechanisms, method overriding, abstract classes, and the implementation of interfaces. Additionally, it covers class visibility, the use of the toString() method, and the organization of classes within packages.

Uploaded by

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

Java Programming

Learning Java

Faculty of Computer Science


Chapter 6

Relationships Among Classes

Faculty of Computer Science


Kinds of Relationships
How a class inherits methods and variables from its parent class
I. Inheritance

How to declare that a class implements certain behavior and define a


type to refer to that behavior
II. Interfaces

How to organize objects into logical groups


III. Packaging

A generalization of classes that lets you nest a class definition


inside another class definition
IV. Inner classes
Faculty of Computer Science
I. Inheritance
Subclass of another class using the extends keyword

Inherits variables and methods not designated as private from its superclass

Can extend only one other class

Faculty of Computer Science


I. Inheritance (Cont’d)
Overriding Methods

 Define a method that has exactly the same method signature (name and argument types) as a method
in its superclass.
 Overriding methods to change the behavior of objects is called subtype polymorphism

@Override - intends to override a


method in the superclass

Faculty of Computer Science


Example
public class Employee
{
float salary=40000;
}

public class Programmer extends Employee


{
int bonus = 10000;
public static void main(String[] args)
{
Programmer p = new Programmer();
//p.salary=20000;
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Faculty of Computer Science


Inheritance (Cont’d)

Faculty of Computer Science


Types of inheritance in java

Faculty of Computer Science


Single Inheritance Example

Faculty of Computer Science


I. Inheritance (Cont’d)
Special References
• this public class Employee {
private String name, address;
• super private int number;
public Employee(String name, int number) {
this.name = name;
this.number = number;
}
public Employee(String name, String address, int num) {
this(name,num);
this.address = address;
}

}

public class Salary extends Employee{


private double salary; //Annual salary
public Salary(String name, String address, int num, double s) {
super(name, address, num);

}

}
Faculty of Computer Science
class Book3 { Title: Webster English Dictionary
protected String title; Number of pages: 1500
protected int pages; Number of definitions: 52500
public Book3(String title,int pages){ Definitions per page: 35
this.title=title;
class Dictionary3 extends Book3{
this.pages=pages;
private int definitions;
}
public Dictionary3(String title,int pages, int definitions)
public void info(){
{
System.out.println("Title: "+title);
super(title,pages);
System.out.println("Number of
this.definitions=definitions;
pages: "+pages);
}
}
Public void info(){
}
super.info();
System.out.println("Number of definitions:
"+definitions);
System.out.println("Definitions per page:
class Books {
"+definitions/pages);
Public static void main(String args[]){
}
Dictionary3 webster2=new
}
Dictionary3("Webster English Dictionary",
1500, 52500);
webster2.info();
}
Faculty of Computer Science
}
I. Inheritance
Example

Faculty of Computer Science


I. Inheritance
Abstract Methods and Classes

 Declare with abstract modifier to indicate that it’s just a prototype


 No body; it’s simply a signature declaration followed by a semicolon
 Can’t directly use a class that contains an abstract method
 Must be subclassed and its abstract methods must be overridden with
methods that implement a body
 Can contain other nonabstract methods and ordinary variable
declarations, but it can’t be instantiated

Faculty of Computer Science


I. Inheritance
abstract class Bike
{
abstract void run(); Abstract Class - Example
}

class Honda4 extends Bike


{
void run( )
{
System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
}

Faculty of Computer Science


Abstract Class - Example
public abstract class Figure public class Circle extends Figure
{ {
public abstract float getArea(); float radius=6f;
} public float getArea()
{
return (float)(3.14*radius*radius);
}

public static void main(String a[])


{
Circle c=new Circle();
//c.getArea();
System.out.println("Area = "+
c.getArea());

}
Faculty of Computer Science
I. Inheritance
Abstract - Example

Faculty of Computer Science


Assignment

void displayResult(String type, float area)

Faculty of Computer Science


II. Interfaces

Define a set of methods that a class must implement

Use implements keyword to declare that it implements an interface

Extend two or more interfaces, list them after the extends keyword, separated by
commas

ALL of the method headings listed in the interface definition must be implemented.

An interface does not contain any constructors.

An interface can extend multiple interfaces.

Faculty of Computer Science


II. Interfaces (Cont’d)

A Java interface is a collection of constants and abstract methods since all methods in an
interface are abstract, the abstract modifier is usually left off.

Faculty of Computer Science


II. Interfaces : Example public class MammalInt implements Animal
{
public interface Animal public void eat()
{ {
public void eat(); System.out.println("Mammal eats");
public void travel(); }
} public void travel()
{
System.out.println("Mammal travels");
}

public static void main(String args[])


{
MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

Faculty of Computer Science


II. Interfaces

Interface Variables

interface Scaleable {
static final int BIG = 0, MEDIUM = 1, SMALL = 2;
void setScale( int size );
}

Faculty of Computer Science


Extending Multiple Interfaces

 A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not
classes, however, and an interface can extend more than one parent interface.
 The implements keyword is used once, and the parent interfaces are declared in a comma-separated
list.
 An interface can extend multiple interfaces.
 A class can implement multiple interfaces.
 However, a class can only extend a single class.

Faculty of Computer Science


public interface Cal1
{
public int sub(int a,int b);
}
public class Caltest implements Cal,Cal1
public interface Cal {
{ public static void main(String[] args)
public int add(int a,int b); {
Caltest c=new Caltest();
} }
public int add(int a, int b)
{
return(a+b);
}

public int sub(int a, int b)


{
return (a-b);
}
}

Faculty of Computer Science


public interface Football
{
public void footballdisplay(); public class Test implements Hockey
} {
public static void main(String[] args)
{
public interface Sports Test t=new Test();
t.footballdisplay();
{ }
public void sportdisplay(); public void footballdisplay()
{
} System.out.println("football");
}
public interface Hockey extends public void sportdisplay()
Football,Sports {
System.out.println("sport");
{
}
public void Hokeydisplay(); public void Hokeydisplay()
{
} System.out.println("Hokey");
}
}

Faculty of Computer Science


II. Interfaces
Overlapping and Conflicting Methods

If the methods have the same name but differ


in return or exception types, the class cannot
implement both and compile-time errors occur

Faculty of Computer Science


II. Interfaces
Abstract vs Interface

Faculty of Computer Science


III. Packaging

Name for a group of related classes and interfaces

Provide more than just source-code-level organization

Hierarchical in nature, using a dot-separated naming convention

Example: package mytools.text.poetry

Faculty of Computer Science


III. Packaging

Class Visibility

 Classes within a package can refer to each other by their simple names
 To locate a class in another package, use import statements at the top of a compilation unit
Example: import java.io.InputStream;
 Import all the classes in a package using the * wildcard notation
Example: import java.io.*;

Faculty of Computer Science


III. Packaging

static import

Get an illusion of built-in math “functions” and constants


Example:

Faculty of Computer Science


III. Packaging

Visibility of Variables and Methods


private, default, protected, and public visibility

Faculty of Computer Science


IV. Inner Classes
inner classes belong to another class or method as a variable would and may
have their visibility limited to its scope
{
class Animal System.out.println("This is a function inside Animal outside
{ Brain“+a);
int a=2; }
class Brain public static void main(String agrs[])
{ {
public Brain() Animal monkey = new Animal();
{ Animal.Brain monkeyBrain = monkey.new Brain();
System.out.println("Brain works..."); monkey.performBehavior();
} monkeyBrain.restBrain();
public void restBrain()
{ }
System.out.println("This is a function inside Brain class");
} }
}
void performBehavior()

Faculty of Computer Science


IV. Inner Classes

Example
• In main function ,
Animal monkey = new Animal();
Animal.Brain monkeyBrain = monkey.new Brain();
monkey.performBehavior();
monkeyBrain.restBrain();

• The compiler generates two .class files:


• Animal.class
• Animal$Brain.class.

 Using inner classes strategically can make your code cleaner, more modular, and easier to
maintain.

Faculty of Computer Science


Java toString() method
 To represent any object as a string, toString() method comes into existence.
 The toString() method returns the string representation of the object.
 If you print any object, java compiler internally invokes the toString() method on the object.
 So overriding the toString() method, returns the desired output, it can be the state of an object
etc. depends on your implementation.

Faculty of Computer Science


class Student
{
int rollno;
String name;
String city;
Student(int rollno, String name, String city)
{
this.rollno=rollno;
this.name=name;
this.city=city;
Testing for
}
Employee.java
public static void main(String args[])
{
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()


System.out.println(s2);//compiler writes here s2.toString()
}
}
Faculty of Computer Science
Java toString() method
class Student{
int rollno;
String name;
String city;

Student(int rollno, String name, String city){


this.rollno=rollno;
this.name=name;
this.city=city;
}

@Override
public String toString(){

return rollno+" "+name+" "+city;


}
public static void main(String args[]){
Student s1=new Student(101,"Raj","lucknow");
Student s2=new Student(102,"Vijay","ghaziabad");

System.out.println(s1);//compiler writes here s1.toString()


System.out.println(s2);//compiler writes
Facultyhere s2.toString()
of Computer Science

You might also like